Skip to content

Commit 2933ae4

Browse files
committed
[DI] move phpdoc node decorators to NodeDecorator namespace, inject explicitly in order
Move the four PhpDocNodeDecorator services out of the PhpDocParser namespace into a dedicated NodeDecorator namespace, and inject them into BetterPhpDocParser as explicit constructor params instead of a tagged collection. The decorators have a required run order: DoctrineAnnotationDecorator must run before ArrayItemClassNameDecorator, which resolves class names in the array items the former produces (e.g. inside doctrine annotations). Explicit ordered injection guarantees this; tagged or autodiscovered registration would not.
1 parent f331ab2 commit 2933ae4

9 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ parameters:
303303
paths:
304304
- src/NodeNameResolver/NodeNameResolver.php
305305
- src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
306-
- src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php
306+
- src/BetterPhpDocParser/NodeDecorator/DoctrineAnnotationDecorator.php
307307

308308
-
309309
identifier: symplify.forbiddenFuncCall

‎rules-tests/DeadCode/Rector/Assign/RemoveDoubleSelfAssignRector/Fixture/skip_different_variable.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;
44

5-
$first = $second = createValidator();
5+
$first = $second = 1234;

src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php renamed to src/BetterPhpDocParser/NodeDecorator/ArrayItemClassNameDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\BetterPhpDocParser\PhpDocParser;
5+
namespace Rector\BetterPhpDocParser\NodeDecorator;
66

77
use PhpParser\Node as PhpNode;
88
use PHPStan\PhpDocParser\Ast\Node;

src/BetterPhpDocParser/PhpDocParser/ConstExprClassNameDecorator.php renamed to src/BetterPhpDocParser/NodeDecorator/ConstExprClassNameDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\BetterPhpDocParser\PhpDocParser;
5+
namespace Rector\BetterPhpDocParser\NodeDecorator;
66

77
use PhpParser\Node as PhpNode;
88
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;

src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php renamed to src/BetterPhpDocParser/NodeDecorator/DoctrineAnnotationDecorator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\BetterPhpDocParser\PhpDocParser;
5+
namespace Rector\BetterPhpDocParser\NodeDecorator;
66

77
use Nette\Utils\Strings;
88
use PhpParser\Node;
@@ -21,6 +21,8 @@
2121
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
2222
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
2323
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
24+
use Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher;
25+
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
2426
use Rector\BetterPhpDocParser\ValueObject\DoctrineAnnotation\SilentKeyMap;
2527
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
2628
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;

src/BetterPhpDocParser/PhpDocParser/PhpDocTagGenericUsesDecorator.php renamed to src/BetterPhpDocParser/NodeDecorator/PhpDocTagGenericUsesDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\BetterPhpDocParser\PhpDocParser;
5+
namespace Rector\BetterPhpDocParser\NodeDecorator;
66

77
use PhpParser\Node as PhpNode;
88
use PHPStan\PhpDocParser\Ast\Node;

‎src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
use PHPStan\PhpDocParser\Parser\TypeParser;
2222
use PHPStan\PhpDocParser\ParserConfig;
2323
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
24+
use Rector\BetterPhpDocParser\NodeDecorator\ArrayItemClassNameDecorator;
25+
use Rector\BetterPhpDocParser\NodeDecorator\ConstExprClassNameDecorator;
26+
use Rector\BetterPhpDocParser\NodeDecorator\DoctrineAnnotationDecorator;
27+
use Rector\BetterPhpDocParser\NodeDecorator\PhpDocTagGenericUsesDecorator;
2428
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
2529
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
2630
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
@@ -44,16 +48,32 @@ final class BetterPhpDocParser extends PhpDocParser
4448
private const string MULTI_NEW_LINES_REGEX = '#(?<new_line>\r\n|\n){2,}#';
4549

4650
/**
47-
* @param PhpDocNodeDecoratorInterface[] $phpDocNodeDecorators
51+
* @var PhpDocNodeDecoratorInterface[]
4852
*/
53+
private readonly array $phpDocNodeDecorators;
54+
4955
public function __construct(
5056
ParserConfig $parserConfig,
5157
TypeParser $typeParser,
5258
ConstExprParser $constExprParser,
5359
private readonly TokenIteratorFactory $tokenIteratorFactory,
54-
private readonly array $phpDocNodeDecorators,
60+
ConstExprClassNameDecorator $constExprClassNameDecorator,
61+
DoctrineAnnotationDecorator $doctrineAnnotationDecorator,
62+
ArrayItemClassNameDecorator $arrayItemClassNameDecorator,
63+
PhpDocTagGenericUsesDecorator $phpDocTagGenericUsesDecorator,
5564
private readonly PrivatesAccessor $privatesAccessor,
5665
) {
66+
// The decorator order below is significant; keep it as is. DoctrineAnnotationDecorator must
67+
// run before ArrayItemClassNameDecorator, which resolves class names inside the array items the
68+
// former produces. A wrong order silently drops annotation class usages and strips their imports.
69+
// They are injected explicitly, not autodiscovered, so the order stays under our control.
70+
$this->phpDocNodeDecorators = [
71+
$constExprClassNameDecorator,
72+
$doctrineAnnotationDecorator,
73+
$arrayItemClassNameDecorator,
74+
$phpDocTagGenericUsesDecorator,
75+
];
76+
5777
parent::__construct(
5878
// ParserConfig
5979
$parserConfig,

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919
use Rector\Application\Provider\CurrentFileProvider;
2020
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
2121
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
22-
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
2322
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ArrayTypePhpDocNodeVisitor;
2423
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\CallableTypePhpDocNodeVisitor;
2524
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\IntersectionTypeNodePhpDocNodeVisitor;
2625
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\TemplatePhpDocNodeVisitor;
2726
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\UnionTypeNodePhpDocNodeVisitor;
28-
use Rector\BetterPhpDocParser\PhpDocParser\ArrayItemClassNameDecorator;
29-
use Rector\BetterPhpDocParser\PhpDocParser\ConstExprClassNameDecorator;
30-
use Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator;
31-
use Rector\BetterPhpDocParser\PhpDocParser\PhpDocTagGenericUsesDecorator;
3227
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
3328
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser;
3429
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\PlainValueParser;
@@ -119,16 +114,6 @@ final class LazyContainerFactory
119114
ArgNotAcceptingClosureNodeVisitor::class,
120115
];
121116

122-
/**
123-
* @var array<class-string<PhpDocNodeDecoratorInterface>>
124-
*/
125-
private const array PHP_DOC_NODE_DECORATOR_CLASSES = [
126-
ConstExprClassNameDecorator::class,
127-
DoctrineAnnotationDecorator::class,
128-
ArrayItemClassNameDecorator::class,
129-
PhpDocTagGenericUsesDecorator::class,
130-
];
131-
132117
/**
133118
* @var array<class-string>
134119
*/
@@ -288,12 +273,6 @@ static function (AbstractRector $rector) use ($rectorConfig): void {
288273

289274
$rectorConfig->autodiscover(__DIR__ . '/../StaticTypeMapper/PhpParser');
290275

291-
$this->registerTagged(
292-
$rectorConfig,
293-
self::PHP_DOC_NODE_DECORATOR_CLASSES,
294-
PhpDocNodeDecoratorInterface::class
295-
);
296-
297276
$this->registerTagged(
298277
$rectorConfig,
299278
self::BASE_PHP_DOC_NODE_VISITORS,

‎src/PhpAttribute/NodeFactory/PhpNestedAttributeGroupFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
use PhpParser\Node\Stmt\Nop;
1515
use PhpParser\Node\Stmt\Use_;
1616
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
17+
use Rector\BetterPhpDocParser\NodeDecorator\DoctrineAnnotationDecorator;
1718
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
1819
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
1920
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
20-
use Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator;
2121
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
2222
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
2323
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;

0 commit comments

Comments
 (0)