1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 01:37:23 +01:00
psalm/tests/FileManipulation/ParamTypeManipulationTest.php

419 lines
14 KiB
PHP
Raw Normal View History

2019-05-02 18:15:38 +02:00
<?php
2019-05-02 18:15:38 +02:00
namespace Psalm\Tests\FileManipulation;
class ParamTypeManipulationTest extends FileManipulationTestCase
2019-05-02 18:15:38 +02:00
{
public function providerValidCodeParse(): array
2019-05-02 18:15:38 +02:00
{
return [
'fixMismatchingDocblockParamType70' => [
'input' => '<?php
2019-05-02 18:15:38 +02:00
/**
* @param int $s
*/
function foo(string $s): string {
return "hello";
}',
'output' => '<?php
2019-05-02 18:15:38 +02:00
/**
* @param string $s
*/
function foo(string $s): string {
return "hello";
}',
'php_version' => '7.0',
'issues_to_fix' => ['MismatchingDocblockParamType'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'fixMismatchingDocblockWithDescriptionParamType70' => [
'input' => '<?php
/**
* @param int $s the string
*/
function foo(string $s): string {
return "hello";
}',
'output' => '<?php
/**
* @param string $s the string
*/
function foo(string $s): string {
return "hello";
}',
'php_version' => '7.0',
'issues_to_fix' => ['MismatchingDocblockParamType'],
'safe_types' => true,
],
2019-05-02 18:15:38 +02:00
'fixNamespacedMismatchingDocblockParamsType70' => [
'input' => '<?php
2019-05-02 18:15:38 +02:00
namespace Foo\Bar {
class A {
/**
* @param \B $b
* @param \C $c
*/
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
class C {}
}',
'output' => '<?php
2019-05-02 18:15:38 +02:00
namespace Foo\Bar {
class A {
/**
* @param B $b
* @param C $c
*/
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
class C {}
}',
'php_version' => '7.0',
'issues_to_fix' => ['MismatchingDocblockParamType'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'noStringParamType' => [
'input' => '<?php
class C {
public function fooFoo($a): void {}
}
(new C)->fooFoo("hello");',
'output' => '<?php
class C {
/**
2021-10-04 01:01:28 +02:00
* @psalm-param \'hello\' $a
*/
public function fooFoo(string $a): void {}
}
(new C)->fooFoo("hello");',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
2019-12-03 07:59:36 +01:00
'noStringParamTypeWithVariableCall' => [
'input' => '<?php
2019-12-03 07:59:36 +01:00
class C {
public function fooFoo($a): void {}
}
/** @var mixed */
$c = null;
$c->fooFoo("hello");
(new C)->fooFoo("hello");',
'output' => '<?php
2019-12-03 07:59:36 +01:00
class C {
/**
* @param string $a
2021-10-04 01:01:28 +02:00
*
* @psalm-param \'hello\' $a
2019-12-03 07:59:36 +01:00
*/
public function fooFoo($a): void {}
}
/** @var mixed */
$c = null;
$c->fooFoo("hello");
(new C)->fooFoo("hello");',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2019-12-03 07:59:36 +01:00
],
'noStringParamTypeWithDocblockCall' => [
'input' => '<?php
class C {
public function fooFoo($a): void {}
}
/**
* @param string $a
*/
function callsWithString($a): void {
(new C)->fooFoo($a);
}',
'output' => '<?php
class C {
/**
* @param string $a
*/
public function fooFoo($a): void {}
}
/**
* @param string $a
*/
function callsWithString($a): void {
(new C)->fooFoo($a);
}',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
'noStringParamTypeWithDocblockAndDescriptionCall' => [
'input' => '<?php
class C {
/**
* @param $ab the string you pass in
*/
public function fooFoo($ab): void {}
}
/**
* @param string $ab
*/
function callsWithString($ab): void {
(new C)->fooFoo($ab);
}',
'output' => '<?php
class C {
/**
* @param string $ab the string you pass in
*/
public function fooFoo($ab): void {}
}
/**
* @param string $ab
*/
function callsWithString($ab): void {
(new C)->fooFoo($ab);
}',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
'noStringParamType56' => [
'input' => '<?php
class C {
public function fooFoo($a): void {}
2019-05-02 18:15:38 +02:00
}
(new C)->fooFoo("hello");',
'output' => '<?php
class C {
/**
* @param string $a
2021-10-04 01:01:28 +02:00
*
* @psalm-param \'hello\' $a
*/
public function fooFoo($a): void {}
2019-05-02 18:15:38 +02:00
}
(new C)->fooFoo("hello");',
'php_version' => '5.6',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'noBoolParamTypeWithDefault' => [
'input' => '<?php
class C {
public function fooFoo($a = true): void {}
}
(new C)->fooFoo(false);',
'output' => '<?php
class C {
public function fooFoo(bool $a = true): void {}
}
(new C)->fooFoo(false);',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
2019-05-31 16:37:26 +02:00
'noStringParamTypeParent' => [
'input' => '<?php
2019-05-31 16:37:26 +02:00
class C {
public function fooFoo($a): void {}
}
class D extends C {}
(new D)->fooFoo("hello");',
'output' => '<?php
2019-05-31 16:37:26 +02:00
class C {
/**
2021-10-04 01:01:28 +02:00
* @psalm-param \'hello\' $a
*/
public function fooFoo(string $a): void {}
2019-05-31 16:37:26 +02:00
}
class D extends C {}
(new D)->fooFoo("hello");',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2019-05-31 16:37:26 +02:00
],
'stringParamTypeNoOp' => [
'input' => '<?php
class C {
public function fooFoo(string $a): void {}
2019-05-02 18:15:38 +02:00
}
(new C)->fooFoo("hello");',
'output' => '<?php
class C {
public function fooFoo(string $a): void {}
2019-05-02 18:15:38 +02:00
}
(new C)->fooFoo("hello");',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'addMissingByRefParamType' => [
'input' => '<?php
class C {
public function foo(&$bar) : void {
$bar .= " me";
}
}
$a = "hello";
(new C)->foo($a);',
'output' => '<?php
class C {
/**
2021-10-04 01:01:28 +02:00
* @psalm-param \'hello\' $bar
*/
public function foo(string &$bar) : void {
$bar .= " me";
}
}
$a = "hello";
(new C)->foo($a);',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
2021-10-04 01:01:28 +02:00
'NamespacedParamNeeded' => [
'input' => '<?php
2021-10-04 01:01:28 +02:00
class C {
public function foo($bar) : void {
echo $bar;
}
}
$a = stdClass::class;
(new C)->foo($a);',
'output' => '<?php
2021-10-04 01:01:28 +02:00
class C {
/**
* @psalm-param stdClass::class $bar
*/
public function foo(string $bar) : void {
echo $bar;
}
}
2021-10-04 01:13:16 +02:00
$a = stdClass::class;
2021-10-04 01:01:28 +02:00
(new C)->foo($a);',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2021-10-04 01:01:28 +02:00
],
2021-10-13 11:22:33 +02:00
'StaticParamForbidden' => [
'input' => '<?php
2021-10-13 11:22:33 +02:00
class A {
private function foo($bar) : void {
}
public function test(): void {
$this->foo($this->ret());
}
public function ret(): static {
return $this;
}
}
class B extends A {
}
(new A)->test();
(new A)->test();
',
'output' => '<?php
2021-10-13 11:22:33 +02:00
class A {
/**
* @param static $bar
*/
private function foo($bar) : void {
}
public function test(): void {
$this->foo($this->ret());
}
public function ret(): static {
return $this;
}
}
class B extends A {
}
(new A)->test();
(new A)->test();
',
'php_version' => '8.0',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
2021-10-13 11:22:33 +02:00
],
2023-05-21 16:11:25 +02:00
'ChangingTypeOfExplicitMixedParam' => [
'input' => '<?php
class ConfigContainer
{
public function setValue(mixed $value): void
{
}
}
function foo(){
$config = new ConfigContainer();
$config->setValue([1,2,3,4]);
}
',
'output' => '<?php
class ConfigContainer
{
/**
* @param int[] $value
*
* @psalm-param list{1, 2, 3, 4} $value
*/
public function setValue(array $value): void
{
}
}
function foo(){
$config = new ConfigContainer();
$config->setValue([1,2,3,4]);
}
',
'php_version' => '8.0',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => false,
],
2019-05-02 18:15:38 +02:00
];
}
}