2019-05-02 18:15:38 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-05-02 18:15:38 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
2021-01-11 23:30:33 +01:00
|
|
|
class ParamTypeManipulationTest extends FileManipulationTestCase
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return array<string,array{input:string,output:string,php_version:string,issues_to_fix:string[],safe_types:bool}>
|
2019-05-02 18:15:38 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'fixMismatchingDocblockParamType70' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
/**
|
|
|
|
* @param int $s
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
/**
|
|
|
|
* @param string $s
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.0',
|
|
|
|
'issues_to_fix' => ['MismatchingDocblockParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'fixNamespacedMismatchingDocblockParamsType70' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'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 {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'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 {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.0',
|
|
|
|
'issues_to_fix' => ['MismatchingDocblockParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'noStringParamType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-12-03 07:49:43 +01:00
|
|
|
class C {
|
|
|
|
public function fooFoo($a): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-12-03 07:49:43 +01:00
|
|
|
class C {
|
2021-05-28 15:46:11 +02:00
|
|
|
/**
|
2021-10-04 01:01:28 +02:00
|
|
|
* @psalm-param \'hello\' $a
|
2021-05-28 15:46:11 +02:00
|
|
|
*/
|
2019-12-03 07:49:43 +01:00
|
|
|
public function fooFoo(string $a): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-12-03 07:49:43 +01:00
|
|
|
],
|
2019-12-03 07:59:36 +01:00
|
|
|
'noStringParamTypeWithVariableCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'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");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'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");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-12-03 07:59:36 +01:00
|
|
|
],
|
2019-12-03 07:52:18 +01:00
|
|
|
'noStringParamTypeWithDocblockCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-12-03 07:52:18 +01:00
|
|
|
class C {
|
|
|
|
public function fooFoo($a): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
*/
|
|
|
|
function callsWithString($a): void {
|
|
|
|
(new C)->fooFoo($a);
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-12-03 07:52:18 +01:00
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
*/
|
|
|
|
public function fooFoo($a): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
*/
|
|
|
|
function callsWithString($a): void {
|
|
|
|
(new C)->fooFoo($a);
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-12-03 07:52:18 +01:00
|
|
|
],
|
2019-12-03 07:49:43 +01:00
|
|
|
'noStringParamType56' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-31 07:47:35 +02:00
|
|
|
class C {
|
|
|
|
public function fooFoo($a): void {}
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-31 07:47:35 +02:00
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @param string $a
|
2021-10-04 01:01:28 +02:00
|
|
|
*
|
|
|
|
* @psalm-param \'hello\' $a
|
2019-05-31 07:47:35 +02:00
|
|
|
*/
|
|
|
|
public function fooFoo($a): void {}
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
2019-05-31 17:55:24 +02:00
|
|
|
'noBoolParamTypeWithDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-31 17:55:24 +02:00
|
|
|
class C {
|
|
|
|
public function fooFoo($a = true): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->fooFoo(false);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-31 17:55:24 +02:00
|
|
|
class C {
|
2019-12-03 07:49:43 +01:00
|
|
|
public function fooFoo(bool $a = true): void {}
|
2019-05-31 17:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->fooFoo(false);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-31 17:55:24 +02:00
|
|
|
],
|
2019-05-31 16:37:26 +02:00
|
|
|
'noStringParamTypeParent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'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");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-31 16:37:26 +02:00
|
|
|
class C {
|
2021-05-28 15:46:11 +02:00
|
|
|
/**
|
2021-10-04 01:01:28 +02:00
|
|
|
* @psalm-param \'hello\' $a
|
2021-05-28 15:46:11 +02:00
|
|
|
*/
|
2019-12-03 07:49:43 +01:00
|
|
|
public function fooFoo(string $a): void {}
|
2019-05-31 16:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
(new D)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-31 16:37:26 +02:00
|
|
|
],
|
2019-05-31 07:47:35 +02:00
|
|
|
'stringParamTypeNoOp' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-31 07:47:35 +02:00
|
|
|
class C {
|
|
|
|
public function fooFoo(string $a): void {}
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-31 07:47:35 +02:00
|
|
|
class C {
|
|
|
|
public function fooFoo(string $a): void {}
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 07:47:35 +02:00
|
|
|
(new C)->fooFoo("hello");',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
2020-02-21 06:42:52 +01:00
|
|
|
'addMissingByRefParamType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-02-21 06:42:52 +01:00
|
|
|
class C {
|
|
|
|
public function foo(&$bar) : void {
|
|
|
|
$bar .= " me";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = "hello";
|
|
|
|
(new C)->foo($a);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-02-21 06:42:52 +01:00
|
|
|
class C {
|
2021-05-28 15:46:11 +02:00
|
|
|
/**
|
2021-10-04 01:01:28 +02:00
|
|
|
* @psalm-param \'hello\' $bar
|
2021-05-28 15:46:11 +02:00
|
|
|
*/
|
2020-02-21 06:42:52 +01:00
|
|
|
public function foo(string &$bar) : void {
|
|
|
|
$bar .= " me";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = "hello";
|
|
|
|
(new C)->foo($a);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2020-02-21 06:42:52 +01:00
|
|
|
],
|
2021-10-04 01:01:28 +02:00
|
|
|
'NamespacedParamNeeded' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'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);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'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);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'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();
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'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();
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '8.0',
|
|
|
|
'issues_to_fix' => ['MissingParamType'],
|
|
|
|
'safe_types' => true,
|
2021-10-13 11:22:33 +02:00
|
|
|
],
|
2019-05-02 18:15:38 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|