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 + { + } +} diff --git a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php index 06634f6a12e..ee49d4dfe25 100644 --- a/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php +++ b/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php @@ -8,8 +8,8 @@ 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_; use PhpParser\NodeVisitor; use Rector\NodeNameResolver\NodeNameResolver; @@ -25,14 +25,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/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 63a5121d382..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 { @@ -146,10 +146,6 @@ private function shouldSkipParam( return true; } - if (! $functionLike instanceof ClassMethod) { - return false; - } - return ! $this->paramTypeAddGuard->isLegal($param, $functionLike); }