From 9a7f96a37678c3a1bd78f5619433ad768d94c7f5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 20 Oct 2025 02:06:17 +0700 Subject: [PATCH 1/3] [TypeDeclaration] Skip callback with instanceof check on ParamTypeByMethodCallTypeRector (part 2) --- ...n_form_builder_callback_instanceof.php.inc | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback_instanceof.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback_instanceof.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback_instanceof.php.inc new file mode 100644 index 00000000000..bc88fc7b309 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback_instanceof.php.inc @@ -0,0 +1,37 @@ +add('someType', DocumentType::class, [ + 'class' => SomeType::class, + 'required' => false, + 'label' => 'Some Type', + 'attr' => [ + 'data-help' => 'some data help', + ], + 'constraints' => [ + new Assert\Callback(function ($someType, ExecutionContextInterface $context): void { + if (! $someType instanceof SomeType) { + return; + } + + $this->use($someType); + + // some logic here + }) + ] + ]); + } + + private function use(SomeType $someType): void + { + } +} From 6c2313cd9cf1a9397cfc85ba593f906fc15d97a3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 20 Oct 2025 13:34:57 +0700 Subject: [PATCH 2/3] fix --- rules/TypeDeclaration/Guard/ParamTypeAddGuard.php | 5 +++-- .../Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php | 4 ---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php index 06634f6a12e..cafe96d467e 100644 --- a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php +++ b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\FunctionLike; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\If_; @@ -25,14 +26,14 @@ public function __construct( ) { } - public function isLegal(Param $param, ClassMethod $classMethod): bool + public function isLegal(Param $param, FunctionLike $functionLike): bool { $paramName = $this->nodeNameResolver->getName($param); $isLegal = true; $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - (array) $classMethod->stmts, + (array) $functionLike->getStmts(), function (Node $subNode) use (&$isLegal, $paramName): ?int { if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->nodeNameResolver->isName( $subNode->var, diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index 63a5121d382..f373dcb0c8f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -146,10 +146,6 @@ private function shouldSkipParam( return true; } - if (! $functionLike instanceof ClassMethod) { - return false; - } - return ! $this->paramTypeAddGuard->isLegal($param, $functionLike); } From 89bf03917be772d27b87703a9acb59e456edf1cd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 20 Oct 2025 13:37:31 +0700 Subject: [PATCH 3/3] cs --- rules/TypeDeclaration/Guard/ParamTypeAddGuard.php | 1 - rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php | 8 -------- .../ClassMethod/ParamTypeByMethodCallTypeRector.php | 2 +- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php index cafe96d467e..ee49d4dfe25 100644 --- a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php +++ b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php @@ -10,7 +10,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; use PhpParser\Node\Param; -use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\If_; use PhpParser\NodeVisitor; use Rector\NodeNameResolver\NodeNameResolver; diff --git a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php index bd868569191..d51f69eaa78 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php +++ b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php @@ -22,11 +22,9 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\MixedType; -use PHPStan\Type\Type; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\TypeComparator\TypeComparator; use Rector\PhpParser\AstResolver; -use Rector\PHPStan\ScopeFetcher; use Rector\StaticTypeMapper\StaticTypeMapper; final readonly class CallerParamMatcher @@ -122,7 +120,6 @@ private function matchCallArgPosition(StaticCall | MethodCall | FuncCall $call, { $paramName = $this->nodeNameResolver->getName($param); - $scope = ScopeFetcher::fetch($call); foreach ($call->args as $argPosition => $arg) { if (! $arg instanceof Arg) { continue; @@ -136,11 +133,6 @@ private function matchCallArgPosition(StaticCall | MethodCall | FuncCall $call, continue; } - $currentType = $scope->getType($arg->value); - if ($currentType instanceof MixedType && $currentType->getSubtractedType() instanceof Type) { - return null; - } - return $argPosition; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php index f373dcb0c8f..01371866375 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php @@ -102,7 +102,7 @@ public function getNodeTypes(): array } /** - * @param ClassMethod|Function_|Closure $node + * @param ClassMethod|Function_|Closure|ArrowFunction $node */ public function refactor(Node $node): ?Node {