• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / dresscode / 41044778467

30 Oct 2026 10:08AM UTC coverage: 97.234% (-0.3%) from 97.495%
41044778467

push

github

dg
upgrade: a decision only the data of the target version turn on belongs to the jump, the project as it is today resolved without narrowing, so that the plan no longer fixes it in the bridge, where nothing runs it

837123ff-7353-45e9-8cd7-ba68b373e93e

5 of 5 new or added lines in 1 file covered. (100.0%)

160 existing lines in 33 files now uncovered.

27315 of 28092 relevant lines covered (97.23%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.44
/src/Rules/Upgrading/AnnotationMap.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the DressCode, a coding style and upgrade tool for PHP (https://dresscode.run)
5
 * Copyright (c) 2026 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace DressCode\Rules\Upgrading;
9

10
use DressCode\Analyses\PhpDoc;
11
use DressCode\Config\Layer;
12
use DressCode\{QualifiedNames, RuleContext, Values};
13
use DressCode\Upgrading\MemberMapGrammar;
14
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
15
use PhpSyntax\Analyses\NameResolver;
16
use PhpSyntax\{Node, SymbolKind};
17
use PhpSyntax\Nodes\{FileNode, NameNode};
18
use PhpSyntax\Nodes\Member\{ClassConstNode, EnumCaseNode, MethodNode, PropertyNode};
19
use PhpSyntax\Nodes\Statement\{ClassNode, EnumNode, FunctionNode, InterfaceNode, NamespaceNode, TraitNode};
20
use function strlen;
21

22

23
/**
24
 * The map `attributeForAnnotation` as the project resolved it: the attribute written for an annotation by its name,
25
 * for the class of a Doctrine annotation or of an attribute, and for the classes of a namespace of annotations. A key
26
 * without a backslash is the name of an annotation, one with it a class, `\Route` for a class of the global namespace,
27
 * and one ending in `\*` a namespace.
28
 * @internal
29
 */
30
final readonly class AnnotationMap
31
{
32
        /** The declarations whose doc comment holds the annotations. */
33
        public const Declarations = [
34
                ClassNode::class,
35
                InterfaceNode::class,
36
                TraitNode::class,
37
                EnumNode::class,
38
                FunctionNode::class,
39
                MethodNode::class,
40
                PropertyNode::class,
41
                ClassConstNode::class,
42
                EnumCaseNode::class,
43
        ];
44

45
        /** The names not in lower case Doctrine ignores where no import makes them a class. */
46
        private const IgnoredAnnotations = [
47
                'Annotation', 'Attribute', 'Attributes', 'Required', 'Target', 'NamedArgumentConstructor', 'TODO', 'SuppressWarnings',
48
        ];
49

50

51
        private function __construct(
52
                /** @var array<string, AttributeTarget>  lowercased annotation without @ => the attribute written instead */
53
                private array $attributes,
1 ✔
54
                /** @var array<string, AttributeTarget>  lowercased class of an attribute, fully qualified => the attribute written instead */
55
                private array $replacedAttributes,
1 ✔
56
                /**
57
                 * @var array<string, array{string, ?Layer}>  lowercased namespace of annotations => the namespace of the attributes
58
                 * written instead and the section of the upgrading data that laid it
59
                 */
60
                private array $namespaces,
1 ✔
61
                /** @var array<string, true>  lowercased classes withdrawn from the map, which no namespace reaches */
62
                private array $withdrawn = [],
1 ✔
63
        ) {
64
        }
1 ✔
65

66

67
        public static function fromValues(Values $values): self
68
        {
69
                $attributes = $replacedAttributes = $namespaces = [];
1 ✔
70
                foreach ($values->readMap(MemberMapGrammar::AttributeForAnnotation) as $key => $code) {
1 ✔
71
                        $origin = $values->getEntryOrigin(MemberMapGrammar::AttributeForAnnotation, (string) $key);
1 ✔
72
                        if (str_ends_with((string) $key, '*')) {
1 ✔
73
                                if (($namespace = AttributeTarget::findNamespace($code)) !== null) {
1 ✔
74
                                        $namespaces[strtolower(trim((string) $key, '\\*'))] = [$namespace, $origin];
1 ✔
75
                                }
76
                        } elseif (($target = AttributeTarget::fromCode($code, $origin)) === null) {
1 ✔
UNCOV
77
                                continue;
×
78
                        } elseif (str_contains((string) $key, '\\')) {
1 ✔
79
                                $replacedAttributes[strtolower(ltrim((string) $key, '\\'))] = $target;
1 ✔
80
                        } else {
81
                                $attributes[strtolower(ltrim((string) $key, '@'))] = $target;
1 ✔
82
                        }
83
                }
84

85
                $withdrawn = array_map(
1 ✔
86
                        fn($name) => strtolower(ltrim((string) $name, '\\')),
1 ✔
87
                        $values->findWithdrawnNames(MemberMapGrammar::AttributeForAnnotation),
1 ✔
88
                );
89
                return new self($attributes, $replacedAttributes, $namespaces, array_fill_keys($withdrawn, true));
1 ✔
90
        }
91

92

93
        public function isEmpty(): bool
94
        {
95
                return $this->attributes === [] && $this->replacedAttributes === [] && $this->namespaces === [];
1 ✔
96
        }
97

98

99
        /**
100
         * The attribute the map writes for the annotation: by its name, `@cached`, or for a Doctrine annotation by
101
         * its class, `@Validation\Size(...)` being the class the imports of the file make of the name, or by its namespace.
102
         */
103
        public function findAttribute(PhpDocTagNode $tag, Node $at, RuleContext $context): ?AttributeTarget
104
        {
105
                return $this->attributes[strtolower(ltrim($tag->name, '@'))]
1 ✔
106
                        ?? $this->findAttributeOfClass(self::resolveAnnotation($tag->name, $at, $context));
1 ✔
107
        }
108

109

110
        /** The attribute the map writes for the Doctrine annotation of the class, fully qualified, by the class or its namespace. */
111
        public function findAttributeOfClass(?string $class): ?AttributeTarget
112
        {
113
                if ($class === null || isset($this->withdrawn[strtolower($class)])) {
1 ✔
114
                        return null;
1 ✔
115
                }
116

117
                $namespace = QualifiedNames::extractNamespace($class);
1 ✔
118
                [$target, $origin] = $this->namespaces[strtolower($namespace)] ?? [null, null];
1 ✔
119
                return $this->replacedAttributes[strtolower($class)] ?? ($target === null ? null : new AttributeTarget(
1 ✔
120
                        $target . substr($class, strlen($namespace)),
1 ✔
121
                        fromNamespace: true,
1 ✔
122
                        origin: $origin,
123
                ));
124
        }
125

126

127
        /** Whether the map writes an attribute for the annotation by its name, which is text after it and no Doctrine annotation. */
128
        public function hasAnnotation(PhpDocTagNode $tag): bool
129
        {
130
                return isset($this->attributes[strtolower(ltrim($tag->name, '@'))]);
1 ✔
131
        }
132

133

134
        /** The attribute written instead of the attribute of the class, fully qualified. */
135
        public function findReplacedAttribute(string $class): ?AttributeTarget
136
        {
137
                return $this->replacedAttributes[strtolower($class)] ?? null;
1 ✔
138
        }
139

140

141
        /**
142
         * The classes, lowercased, the Doctrine annotations of the scope stand for that the map writes an attribute for,
143
         * whose imports a rule rewriting imports leaves to the annotations until they are attributes.
144
         * @return array<string, true>
145
         */
146
        public function findAnnotatedClasses(FileNode|NamespaceNode $scope, RuleContext $context): array
147
        {
148
                if ($this->replacedAttributes === [] && $this->namespaces === []) {
1 ✔
149
                        return [];
1 ✔
150
                }
151

152
                $classes = [];
1 ✔
153
                $phpDoc = $context->getAnalysis(PhpDoc::class);
1 ✔
154
                foreach ($scope->find(
1 ✔
155
                        Node::class,
1 ✔
156
                        fn(Node $node) => array_any(self::Declarations, fn(string $class) => $node instanceof $class),
1 ✔
157
                ) as $declaration) {
158
                        $docComment = $declaration->getDocComment();
1 ✔
159
                        if ($docComment === null || !str_contains($docComment->text, '@')) {
1 ✔
160
                                continue;
1 ✔
161
                        }
162

163
                        foreach ($phpDoc->parse($docComment)->children as $child) {
1 ✔
164
                                $class = $child instanceof PhpDocTagNode ? self::resolveAnnotation($child->name, $declaration, $context) : null;
1 ✔
165
                                if ($class !== null && $this->findAttributeOfClass($class) !== null) {
1 ✔
166
                                        $classes[strtolower($class)] = true;
1 ✔
167
                                }
168
                        }
169
                }
170

171
                return $classes;
1 ✔
172
        }
173

174

175
        /**
176
         * The class the name of a Doctrine annotation stands for, as the imports of the file and its namespace make it.
177
         * Null where no import makes the name a class and it is in lower case or one Doctrine ignores, a tag of phpDoc
178
         * such as `@param` or `@internal`, or one of Doctrine itself such as `@Target`.
179
         */
180
        public static function resolveAnnotation(string $name, Node $at, RuleContext $context): ?string
181
        {
182
                $name = ltrim($name, '@');
1 ✔
183
                if (str_starts_with($name, '\\')) {
1 ✔
UNCOV
184
                        return substr($name, 1);
×
185
                }
186

187
                $resolver = $context->getAnalysis(NameResolver::class);
1 ✔
188
                $parts = explode('\\', $name, 2);
1 ✔
189
                $import = $resolver->getImports(SymbolKind::ClassLike, $at)[strtolower($parts[0])] ?? null;
1 ✔
190
                if ($import === null && (!ctype_upper($name[0]) || in_array($name, self::IgnoredAnnotations, true))) {
1 ✔
191
                        return null;
1 ✔
192
                }
193

194
                $node = NameNode::tryFromText($name);
1 ✔
195
                if ($node !== null) {
1 ✔
196
                        return $resolver->resolveClass($node, $at);
1 ✔
197
                }
198

UNCOV
199
                $base = $import ?? ltrim($resolver->getNamespace($at) . '\\' . $parts[0], '\\');
×
UNCOV
200
                return isset($parts[1]) ? "$base\\$parts[1]" : $base;
×
201
        }
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc