diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback.php.inc new file mode 100644 index 00000000000..fbc4f87df2c --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Fixture/skip_in_form_builder_callback.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 === null) { + return; + } + + $this->use($someType); + + // some logic here + }) + ] + ]); + } + + private function use(SomeType $someType): void + { + } +} diff --git a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php index d51f69eaa78..bd868569191 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php +++ b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php @@ -22,9 +22,11 @@ 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 @@ -120,6 +122,7 @@ 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; @@ -133,6 +136,11 @@ 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/stubs/Symfony/Component/Form/AbstractType.php b/stubs/Symfony/Component/Form/AbstractType.php new file mode 100644 index 00000000000..436e31985a8 --- /dev/null +++ b/stubs/Symfony/Component/Form/AbstractType.php @@ -0,0 +1,11 @@ +