Skip to content

Commit

Permalink
TASK: Use custom param type inferer
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Dec 5, 2022
1 parent 37f4fed commit 60b5d83
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/NodeFactory/InitializeArgumentsClassMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -73,7 +73,7 @@ final class InitializeArgumentsClassMethodFactory
/**
* @readonly
*/
private ParamTypeInferer $paramTypeInferer;
private FunctionLikeDocParamTypeInferer $paramTypeInferer;

/**
* @readonly
Expand All @@ -99,7 +99,7 @@ public function __construct(
NodeFactory $nodeFactory,
NodeNameResolver $nodeNameResolver,
StaticTypeMapper $staticTypeMapper,
ParamTypeInferer $paramTypeInferer,
FunctionLikeDocParamTypeInferer $paramTypeInferer,
PhpDocInfoFactory $phpDocInfoFactory,
ReflectionProvider $reflectionProvider,
ValueResolver $valueResolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TypeInferer\ParamTypeInferer;

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;

final class FunctionLikeDocParamTypeInferer
{
/**
* @readonly
*/
private NodeNameResolver $nodeNameResolver;

/**
* @readonly
*/
private PhpDocInfoFactory $phpDocInfoFactory;

/**
* @readonly
*/
private BetterNodeFinder $betterNodeFinder;

public function __construct(
NodeNameResolver $nodeNameResolver,
PhpDocInfoFactory $phpDocInfoFactory,
BetterNodeFinder $betterNodeFinder
) {
$this->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();
}
}

0 comments on commit 60b5d83

Please sign in to comment.