diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_arrow_function.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_arrow_function.php.inc index ed7e5fa6ec4..efa49be2038 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_arrow_function.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_arrow_function.php.inc @@ -13,7 +13,7 @@ final class AlsoArrowFunction public function go() { - $closure = fn ($value) => $this->someTypedService->run($value); + $closure = fn ($value) => $this->someTypedService->runInt($value); } } @@ -34,7 +34,7 @@ final class AlsoArrowFunction public function go() { - $closure = fn (string $value) => $this->someTypedService->run($value); + $closure = fn (int $value) => $this->someTypedService->runInt($value); } } diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_closure.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_closure.php.inc index 1805df9329a..a643bf40d48 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_closure.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/also_closure.php.inc @@ -14,7 +14,7 @@ final class AlsoClosure public function go() { $closure = function ($value) { - $this->someTypedService->run($value); + $this->someTypedService->runInt($value); }; } } @@ -36,8 +36,8 @@ final class AlsoClosure public function go() { - $closure = function (string $value) { - $this->someTypedService->run($value); + $closure = function (int $value) { + $this->someTypedService->runInt($value); }; } } diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/skip_string_caller_param.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/skip_string_caller_param.php.inc new file mode 100644 index 00000000000..9ade3c92293 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/skip_string_caller_param.php.inc @@ -0,0 +1,19 @@ +someTypedService->run($value); + } +} diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/some_class.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/some_class.php.inc index 547f2b8d8d2..a9d4d3603d7 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/some_class.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/some_class.php.inc @@ -13,7 +13,7 @@ final class UseDependency public function go($value) { - $this->someTypedService->run($value); + $this->someTypedService->runInt($value); } } @@ -32,9 +32,9 @@ final class UseDependency ) { } - public function go(string $value) + public function go(int $value) { - $this->someTypedService->run($value); + $this->someTypedService->runInt($value); } } diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/static_call_elsewhere.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/static_call_elsewhere.php.inc index 1b2a840179b..0ea6134d7a0 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/static_call_elsewhere.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Fixture/static_call_elsewhere.php.inc @@ -8,7 +8,7 @@ final class StaticCallElsewhere { public function go($value, $first) { - SomeTypedService::fun($first, $value); + SomeTypedService::funInt($first, $value); } } @@ -22,9 +22,9 @@ use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ScalarParamTypeByMethodCallT final class StaticCallElsewhere { - public function go(string $value, $first) + public function go(int $value, $first) { - SomeTypedService::fun($first, $value); + SomeTypedService::funInt($first, $value); } } diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Source/SomeTypedService.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Source/SomeTypedService.php index 47f3309543a..bbc02f841c4 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Source/SomeTypedService.php +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector/Source/SomeTypedService.php @@ -10,10 +10,18 @@ public function run(string $name) { } + public function runInt(int $count) + { + } + public static function fun($surname, string $name) { } + public static function funInt($surname, int $count) + { + } + public function withDefaultNullUnion(bool|string $name = null) { } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector.php index 39a088a9346..30832bec1d9 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ScalarParamTypeByMethodCallTypeRector.php @@ -23,7 +23,7 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' class SomeTypedService { - public function run(string $name) + public function run(int $value) { } } @@ -45,7 +45,7 @@ public function go($value) <<<'CODE_SAMPLE' class SomeTypedService { - public function run(string $name) + public function run(int $value) { } } @@ -57,7 +57,7 @@ public function __construct( ) { } - public function go(string $value) + public function go(int $value) { $this->someTypedService->run($value); } @@ -69,7 +69,14 @@ public function go(string $value) protected function isMatchingParamType(Type $type): bool { - return TypeCombinator::removeNull($type)->isScalar() + $type = TypeCombinator::removeNull($type); + if (! $type->isScalar()->yes()) { + return false; + } + + // a string param accepts int/float/bool via scalar coercion, so a caller + // may pass another scalar - inferring string from it would be unsafe + return ! $type->isString() ->yes(); } }