From 55fde2621d48c6924c9fadbf6b486b16c9b67cf1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 24 Oct 2025 09:57:40 +0700 Subject: [PATCH 1/3] [CodingStyle] Skip Method target not found or defined only via @method on FunctionLikeToFirstClassCallableRector --- .../Fixture/fixture.php.inc | 19 ++++--------------- .../Fixture/skip_null_variable_call.php.inc | 12 ++++++++++++ ...skip_target_method_from_method_doc.php.inc | 14 ++++++++++++++ ...ip_target_native_method_not_exists.php.inc | 11 +++++++++++ .../supports_variadic_and_unpack.php.inc | 4 ++++ .../Source/FooBar.php | 10 ++++++++++ ...FunctionLikeToFirstClassCallableRector.php | 12 ++++++++++++ 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc create mode 100644 rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_method_from_method_doc.php.inc create mode 100644 rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_native_method_not_exists.php.inc create mode 100644 rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/fixture.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/fixture.php.inc index 826ee48c6b3..90c340f7603 100644 --- a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/fixture.php.inc +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/fixture.php.inc @@ -2,6 +2,8 @@ namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture; +use Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Source\FooBar; + function ($foo) { return FooBar::foo($foo); @@ -9,15 +11,6 @@ function ($foo) fn ($foo) => FooBar::foo($foo); -$bar = null; - -fn ($foo) => $bar->foo($foo); - -function ($foo) use ($bar) -{ - return $bar->foo($foo); -}; - function ($foo, $bar, $ray) { return FooBar::foo($foo, $bar); @@ -31,15 +24,11 @@ fn ($foo, $bar, $ray) => FooBar::foo($foo); namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture; -FooBar::foo(...); +use Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Source\FooBar; FooBar::foo(...); -$bar = null; - -$bar->foo(...); - -$bar->foo(...); +FooBar::foo(...); FooBar::foo(...); diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc new file mode 100644 index 00000000000..a9b850ca933 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc @@ -0,0 +1,12 @@ + $bar->foo($foo); + +function ($foo) use ($bar) +{ + return $bar->foo($foo); +}; \ No newline at end of file diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_method_from_method_doc.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_method_from_method_doc.php.inc new file mode 100644 index 00000000000..231dd7ab55f --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_method_from_method_doc.php.inc @@ -0,0 +1,14 @@ + SkipTargetNativeMethodNotExists::test($value), [1, 2, 3, 4]); + } +} diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_native_method_not_exists.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_native_method_not_exists.php.inc new file mode 100644 index 00000000000..c421d29bb8e --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_target_native_method_not_exists.php.inc @@ -0,0 +1,11 @@ + SkipTargetNativeMethodNotExists::test($value), [1, 2, 3, 4]); + } +} diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/supports_variadic_and_unpack.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/supports_variadic_and_unpack.php.inc index 18a91da686b..c947fb5acbc 100644 --- a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/supports_variadic_and_unpack.php.inc +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/supports_variadic_and_unpack.php.inc @@ -2,6 +2,8 @@ namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture; +use Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Source\FooBar; + function (...$foo) { return FooBar::foo(...$foo); @@ -15,6 +17,8 @@ fn (...$foo) => FooBar::foo(...$foo); namespace Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Fixture; +use Rector\Tests\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector\Source\FooBar; + FooBar::foo(...); FooBar::foo(...); diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php new file mode 100644 index 00000000000..8e233c18f73 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php @@ -0,0 +1,10 @@ +reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); + + // not exists, probably by magic method + if ($reflection === null) { + return true; + } + + // exists, but by @method annotation + if ($reflection instanceof \PHPStan\Reflection\Annotations\AnnotationMethodReflection) { + return true; + } + $functionLike = $this->astResolver->resolveClassMethodOrFunctionFromCall($callLike); if (! $functionLike instanceof FunctionLike) { return false; From edbb34f40c6db1354d43adeec526aafa50ed9a5d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 24 Oct 2025 09:58:30 +0700 Subject: [PATCH 2/3] [CodingStyle] Skip Method target not found or defined only via @method on FunctionLikeToFirstClassCallableRector --- .../FunctionLike/FunctionLikeToFirstClassCallableRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php index e3c77895394..68275de9b90 100644 --- a/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php +++ b/rules/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector.php @@ -22,6 +22,7 @@ use PhpParser\Node\VariadicPlaceholder; use PhpParser\NodeVisitor; use PHPStan\Analyser\Scope; +use PHPStan\Reflection\Annotations\AnnotationMethodReflection; use PHPStan\Reflection\ResolvedFunctionVariantWithOriginal; use PHPStan\Type\CallableType; use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; @@ -193,7 +194,7 @@ private function shouldSkip( } // exists, but by @method annotation - if ($reflection instanceof \PHPStan\Reflection\Annotations\AnnotationMethodReflection) { + if ($reflection instanceof AnnotationMethodReflection) { return true; } From e19f1d8c232bb45bad517efb14767b6b5396415c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 24 Oct 2025 10:04:11 +0700 Subject: [PATCH 3/3] eol --- .../Fixture/skip_null_variable_call.php.inc | 2 +- .../FunctionLikeToFirstClassCallableRector/Source/FooBar.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc index a9b850ca933..4a877680162 100644 --- a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Fixture/skip_null_variable_call.php.inc @@ -9,4 +9,4 @@ fn ($foo) => $bar->foo($foo); function ($foo) use ($bar) { return $bar->foo($foo); -}; \ No newline at end of file +}; diff --git a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php index 8e233c18f73..f181213a615 100644 --- a/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php +++ b/rules-tests/CodingStyle/Rector/FunctionLike/FunctionLikeToFirstClassCallableRector/Source/FooBar.php @@ -7,4 +7,4 @@ class FooBar public static function foo($args) { } -} \ No newline at end of file +}