diff --git a/src/NodeFactory/InitializeArgumentsClassMethodFactory.php b/src/NodeFactory/InitializeArgumentsClassMethodFactory.php index bd4ce840d..52dfe9561 100644 --- a/src/NodeFactory/InitializeArgumentsClassMethodFactory.php +++ b/src/NodeFactory/InitializeArgumentsClassMethodFactory.php @@ -41,7 +41,7 @@ use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; -use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer; +use Ssch\TYPO3Rector\TypeInferer\ParamTypeInferer\FunctionLikeDocParamTypeInferer; final class InitializeArgumentsClassMethodFactory { @@ -73,7 +73,7 @@ final class InitializeArgumentsClassMethodFactory /** * @readonly */ - private ParamTypeInferer $paramTypeInferer; + private FunctionLikeDocParamTypeInferer $paramTypeInferer; /** * @readonly @@ -99,7 +99,7 @@ public function __construct( NodeFactory $nodeFactory, NodeNameResolver $nodeNameResolver, StaticTypeMapper $staticTypeMapper, - ParamTypeInferer $paramTypeInferer, + FunctionLikeDocParamTypeInferer $paramTypeInferer, PhpDocInfoFactory $phpDocInfoFactory, ReflectionProvider $reflectionProvider, ValueResolver $valueResolver, diff --git a/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php b/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php new file mode 100644 index 000000000..ba897d130 --- /dev/null +++ b/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php @@ -0,0 +1,75 @@ +nodeNameResolver = $nodeNameResolver; + $this->phpDocInfoFactory = $phpDocInfoFactory; + $this->betterNodeFinder = $betterNodeFinder; + } + + public function inferParam(Param $param): Type + { + $functionLike = $this->resolveScopeNode($param); + if (! $functionLike instanceof FunctionLike) { + return new MixedType(); + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $paramTypesByName = $phpDocInfo->getParamTypesByName(); + if ([] === $paramTypesByName) { + return new MixedType(); + } + return $this->matchParamNodeFromDoc($paramTypesByName, $param); + } + + /** + * @return ClassMethod|Function_|null + */ + private function resolveScopeNode(Param $param): ?Node + { + return $this->betterNodeFinder->findParentByTypes($param, [ClassMethod::class, Function_::class]); + } + + /** + * @param Type[] $paramWithTypes + */ + private function matchParamNodeFromDoc(array $paramWithTypes, Param $param): Type + { + $paramNodeName = '$' . $this->nodeNameResolver->getName($param->var); + return $paramWithTypes[$paramNodeName] ?? new MixedType(); + } +}