Skip to content

Commit d9c5826

Browse files
committed
[BetterPhpDocParser] inject phpdoc node decorators explicitly to keep required order
Autodiscovering the decorators used filesystem order, dropping DoctrineAnnotationDecorator to last. It must run before ArrayItemClassNameDecorator, which resolves class names in the ArrayItemNodes it produces (e.g. @Orm\Column(type=Types::STRING)). Wrong order silently lost annotation class usages and removed their imports via removeUnusedImports(). Inject the four decorators as explicit constructor params, in order, with a comment explaining the constraint.
1 parent f4cd9bb commit d9c5826

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

‎src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php‎

Lines changed: 25 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,35 @@ 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 is significant and must stay as listed below.
67+
// DoctrineAnnotationDecorator has to run before ArrayItemClassNameDecorator: it turns raw
68+
// GenericTagValueNode annotations into DoctrineAnnotationTagValueNode trees, creating the
69+
// ArrayItemNodes that ArrayItemClassNameDecorator then resolves class names in
70+
// (e.g. @ORM\Column(type=Types::STRING)). Reorder and annotation class usages are silently
71+
// lost, breaking e.g. removeUnusedImports(). This is why they are injected explicitly
72+
// instead of autodiscovered, where filesystem order would not be guaranteed.
73+
$this->phpDocNodeDecorators = [
74+
$constExprClassNameDecorator,
75+
$doctrineAnnotationDecorator,
76+
$arrayItemClassNameDecorator,
77+
$phpDocTagGenericUsesDecorator,
78+
];
79+
5780
parent::__construct(
5881
// ParserConfig
5982
$parserConfig,

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
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;
23-
use Rector\BetterPhpDocParser\NodeDecorator\ArrayItemClassNameDecorator;
24-
use Rector\BetterPhpDocParser\NodeDecorator\ConstExprClassNameDecorator;
25-
use Rector\BetterPhpDocParser\NodeDecorator\DoctrineAnnotationDecorator;
26-
use Rector\BetterPhpDocParser\NodeDecorator\PhpDocTagGenericUsesDecorator;
2722
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ArrayTypePhpDocNodeVisitor;
2823
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\CallableTypePhpDocNodeVisitor;
2924
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\IntersectionTypeNodePhpDocNodeVisitor;
@@ -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
*/
@@ -287,13 +272,6 @@ static function (AbstractRector $rector) use ($rectorConfig): void {
287272
);
288273

289274
$rectorConfig->autodiscover(__DIR__ . '/../StaticTypeMapper/PhpParser');
290-
$rectorConfig->autodiscover(__DIR__ . '/../BetterPhpDocParser/NodeDecorator');
291-
292-
// $this->registerTagged(
293-
// $rectorConfig,
294-
// self::PHP_DOC_NODE_DECORATOR_CLASSES,
295-
// PhpDocNodeDecoratorInterface::class
296-
// );
297275

298276
$this->registerTagged(
299277
$rectorConfig,

0 commit comments

Comments
 (0)