-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
ArrayItemClassNameDecorator.php
63 lines (52 loc) · 2.09 KB
/
ArrayItemClassNameDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocParser;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use PhpParser\Node as PhpNode;
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 readonly class ArrayItemClassNameDecorator implements PhpDocNodeDecoratorInterface
{
public function __construct(
private NameScopeFactory $nameScopeFactory,
private PhpDocNodeTraverser $phpDocNodeTraverser
) {
}
public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void
{
// iterating all phpdocs has big overhead. peek into the phpdoc to exit early
if (! str_contains($phpDocNode->__toString(), '::')) {
return;
}
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use (
$phpNode
): Node|null {
if (! $node instanceof ArrayItemNode) {
return null;
}
if (! is_string($node->value)) {
return null;
}
$splitScopeResolution = explode('::', $node->value);
if (count($splitScopeResolution) !== 2) {
return null;
}
$className = $this->resolveFullyQualifiedClass($splitScopeResolution[0], $phpNode);
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className);
return $node;
});
}
private function resolveFullyQualifiedClass(string $className, PhpNode $phpNode): string
{
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode);
return $nameScope->resolveStringName($className);
}
}