mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
c0bc2824b0
b9ea5fd996
[PostRector] Skip remove use statement on used in annotation on removeUnusedImports() (#5657)
66 lines
2.6 KiB
PHP
66 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\BetterPhpDocParser\PhpDocParser;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
|
|
use PhpParser\Node as PhpNode;
|
|
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
|
|
use PHPStan\PhpDocParser\Ast\Node;
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
|
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
|
|
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
|
|
use Rector\StaticTypeMapper\Naming\NameScopeFactory;
|
|
/**
|
|
* Decorate node with fully qualified class name for annotation:
|
|
* e.g. @ORM\Column(type=Types::STRING, length=100, nullable=false)
|
|
*/
|
|
final class ArrayItemClassNameDecorator implements PhpDocNodeDecoratorInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\StaticTypeMapper\Naming\NameScopeFactory
|
|
*/
|
|
private $nameScopeFactory;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser
|
|
*/
|
|
private $phpDocNodeTraverser;
|
|
public function __construct(NameScopeFactory $nameScopeFactory, PhpDocNodeTraverser $phpDocNodeTraverser)
|
|
{
|
|
$this->nameScopeFactory = $nameScopeFactory;
|
|
$this->phpDocNodeTraverser = $phpDocNodeTraverser;
|
|
}
|
|
public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode) : void
|
|
{
|
|
// iterating all phpdocs has big overhead. peek into the phpdoc to exit early
|
|
if (\strpos($phpDocNode->__toString(), '::') === \false) {
|
|
return;
|
|
}
|
|
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use($phpNode) : ?\PHPStan\PhpDocParser\Ast\Node {
|
|
if (!$node instanceof ArrayItemNode) {
|
|
return null;
|
|
}
|
|
if (!\is_string($node->value)) {
|
|
return null;
|
|
}
|
|
$splitScopeResolution = \explode('::', $node->value);
|
|
if (\count($splitScopeResolution) !== 2) {
|
|
return null;
|
|
}
|
|
$firstName = $splitScopeResolution[0];
|
|
$constFetchNode = new ConstFetchNode($firstName, $firstName);
|
|
$className = $this->resolveFullyQualifiedClass($constFetchNode, $phpNode);
|
|
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className);
|
|
return $node;
|
|
});
|
|
}
|
|
private function resolveFullyQualifiedClass(ConstFetchNode $constFetchNode, PhpNode $phpNode) : string
|
|
{
|
|
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode);
|
|
return $nameScope->resolveStringName($constFetchNode->className);
|
|
}
|
|
}
|