diff --git a/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php b/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php index 27bd5f11b687..8c0c40a26f13 100644 --- a/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php +++ b/packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php @@ -276,6 +276,26 @@ public function removeByName(string $nameToRemove): void } } + /** + * With "name" as key + * @return Type[] + */ + public function getParamTypesByName(): array + { + $paramTypesByName = []; + + foreach ($this->phpDocNode->getParamTagValues() as $paramTagValueNode) { + $parameterName = $paramTagValueNode->parameterName; + + $paramTypesByName[$parameterName] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType( + $paramTagValueNode, + $this->node + ); + } + + return $paramTypesByName; + } + private function getParamTagValueByName(string $name): ?AttributeAwareParamTagValueNode { $phpDocNode = $this->getPhpDocNode(); diff --git a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php index a95f9866e4d5..ebb8156b073f 100644 --- a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php +++ b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php @@ -7,10 +7,6 @@ use Nette\Utils\Strings; use PhpParser\Comment\Doc; use PhpParser\Node; -use PhpParser\Node\Expr\Closure; -use PhpParser\Node\FunctionLike; -use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Function_; use PHPStan\PhpDocParser\Ast\Node as PhpDocParserNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; @@ -63,11 +59,6 @@ final class DocBlockManipulator */ private $staticTypeMapper; - /** - * @var bool - */ - private $hasPhpDocChanged = false; - /** * @var DocBlockClassRenamer */ @@ -173,48 +164,6 @@ public function replaceAnnotationInNode(Node $node, string $oldAnnotation, strin $this->replaceTagByAnother($phpDocInfo->getPhpDocNode(), $oldAnnotation, $newAnnotation); } - /** - * With "name" as key - * - * @param Function_|ClassMethod|Closure $functionLike - * @return Type[] - */ - public function getParamTypesByName(FunctionLike $functionLike): array - { - /** @var PhpDocInfo|null $phpDocInfo */ - $phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO); - if ($phpDocInfo === null) { - return []; - } - - $paramTypesByName = []; - - foreach ($phpDocInfo->getParamTagValues() as $paramTagValueNode) { - $parameterName = $paramTagValueNode->parameterName; - - $paramTypesByName[$parameterName] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType( - $paramTagValueNode, - $functionLike - ); - } - - return $paramTypesByName; - } - - /** - * @return PhpDocTagNode[] - */ - public function getTagsByName(Node $node, string $name): array - { - /** @var PhpDocInfo|null $phpDocInfo */ - $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); - if ($phpDocInfo === null) { - return []; - } - - return $phpDocInfo->getTagsByName($name); - } - public function changeVarTag(Node $node, Type $newType): void { /** @var PhpDocInfo|null $phpDocInfo */ @@ -348,14 +297,8 @@ public function changeUnderscoreType(Node $node, string $namespacePrefix, array $nameParts = explode('_', $staticType->getClassName()); $node->name = '\\' . implode('\\', $nameParts); - $this->hasPhpDocChanged = true; - return $node; }); - - if (! $this->hasPhpDocChanged) { - return; - } } /** diff --git a/packages/PHPUnit/src/Rector/ExceptionAnnotationRector.php b/packages/PHPUnit/src/Rector/ExceptionAnnotationRector.php index 0252b7f9cc41..4459d262fd18 100644 --- a/packages/PHPUnit/src/Rector/ExceptionAnnotationRector.php +++ b/packages/PHPUnit/src/Rector/ExceptionAnnotationRector.php @@ -19,6 +19,7 @@ /** * @see https://thephp.cc/news/2016/02/questioning-phpunit-best-practices * @see https://github.com/sebastianbergmann/phpunit/commit/17c09b33ac5d9cad1459ace0ae7b1f942d1e9afd + * * @see \Rector\PHPUnit\Tests\Rector\ExceptionAnnotationRector\ExceptionAnnotationRectorTest */ final class ExceptionAnnotationRector extends AbstractPHPUnitRector @@ -82,13 +83,19 @@ public function refactor(Node $node): ?Node return null; } + /** @var PhpDocInfo|null $phpDocInfo */ + $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); + if ($phpDocInfo === null) { + return null; + } + foreach ($this->annotationToMethod as $annotation => $method) { - if (! $this->docBlockManipulator->hasTag($node, $annotation)) { + if (! $phpDocInfo->hasByName($annotation)) { continue; } /** @var GenericTagValueNode[] $tags */ - $tags = $this->docBlockManipulator->getTagsByName($node, $annotation); + $tags = $phpDocInfo->getTagsByName($annotation); $methodCallExpressions = array_map(function (PhpDocTagNode $phpDocTagNode) use ($method): Expression { $methodCall = $this->createMethodCallExpressionFromTag($phpDocTagNode, $method); @@ -97,11 +104,7 @@ public function refactor(Node $node): ?Node $node->stmts = array_merge($methodCallExpressions, (array) $node->stmts); - /** @var PhpDocInfo|null $phpDocInfo */ - $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); - if ($phpDocInfo !== null) { - $phpDocInfo->removeByName($annotation); - } + $phpDocInfo->removeByName($annotation); } return $node; diff --git a/packages/Renaming/src/Rector/Class_/RenameClassRector.php b/packages/Renaming/src/Rector/Class_/RenameClassRector.php index deb4fedcbbf3..27b776fd468d 100644 --- a/packages/Renaming/src/Rector/Class_/RenameClassRector.php +++ b/packages/Renaming/src/Rector/Class_/RenameClassRector.php @@ -18,6 +18,7 @@ use PhpParser\Node\Stmt\Use_; use PhpParser\Node\Stmt\UseUse; use PHPStan\Type\ObjectType; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\CodingStyle\Naming\ClassNaming; use Rector\NodeTypeResolver\ClassExistenceStaticHelper; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -149,8 +150,9 @@ public function refactor(Node $node): ?Node */ private function refactorPhpDoc(Node $node): void { - $nodePhpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); - if ($nodePhpDocInfo === null) { + /** @var PhpDocInfo|null $phpDocInfo */ + $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); + if ($phpDocInfo === null) { return; } diff --git a/packages/TypeDeclaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php b/packages/TypeDeclaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php index 0662971c959e..f1bdb72011fb 100644 --- a/packages/TypeDeclaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php +++ b/packages/TypeDeclaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Type\Type; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -104,8 +105,13 @@ public function refactor(Node $node): ?Node return null; } - $paramWithTypes = $this->docBlockManipulator->getParamTypesByName($node); + /** @var PhpDocInfo|null $phpDocInfo */ + $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); + if ($phpDocInfo === null) { + return null; + } + $paramWithTypes = $phpDocInfo->getParamTypesByName(); // no tags, nothing to complete here if ($paramWithTypes === []) { return null;