• 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

98.41
/src/Upgrading/MemberMap.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\Upgrading;
9

10
use DressCode\Analyses\{MemberAccess, Parameter, Types};
11
use DressCode\Config\Layer;
12
use DressCode\{RuleContext, Tristate, Values};
13
use PhpSyntax\Builder;
14
use PhpSyntax\Nodes\{ArgumentListNode, IdentifierNode};
15
use PhpSyntax\Nodes\Expression\{
16
        ClassConstantFetchNode, MethodCallNode, NewNode, PropertyFetchNode, StaticMethodCallNode, StaticPropertyFetchNode,
17
};
18
use PhpSyntax\Nodes\Member\MethodNode;
19

20

21
/**
22
 * The entries of a map of members by the lowercased name of the member, which is what a rule looks a node up by, and
23
 * the entry a use or a declaration of a member is of.
24
 * @template T
25
 */
26
final readonly class MemberMap
27
{
28
        private function __construct(
29
                /** @var array<string, list<MemberMapEntry<T>>> */
30
                private array $byName,
1 ✔
31
        ) {
32
        }
1 ✔
33

34

35
        /**
36
         * The map of the decision as the project resolved it, with what the rule keeps of each value; without `$convert` the
37
         * value as the grammar reads it.
38
         * @template V
39
         * @param  ?\Closure(mixed, MemberPattern, string): V  $convert  given the value, the pattern and the key as written
40
         * @return ($convert is null ? self<mixed> : self<V>)
41
         */
42
        public static function fromValues(Values $values, string $path, ?\Closure $convert = null): self
43
        {
44
                return self::build(
1 ✔
45
                        $values->readMap($path),
1 ✔
46
                        $convert,
47
                        fn(string $key) => $values->getEntryDecision($path, $key),
1 ✔
48
                        fn(string $key) => $values->getEntryOrigin($path, $key),
1 ✔
49
                );
50
        }
51

52

53
        /**
54
         * The map of entries a use of each of which is reported under the decision.
55
         * @template V
56
         * @param  array<string, mixed>  $entries  as `Values::readMap()` gives them
57
         * @param  \Closure(mixed, MemberPattern, string): V  $convert  what the rule keeps of a value, given also the key as written
58
         * @return self<V>
59
         */
60
        public static function fromEntries(array $entries, \Closure $convert, string $decision): self
61
        {
62
                return self::build($entries, $convert, fn() => $decision);
1 ✔
63
        }
64

65

66
        /**
67
         * @template V
68
         * @param  array<mixed>  $entries
69
         * @param  ?\Closure(mixed, MemberPattern, string): V  $convert
70
         * @param  \Closure(string): string  $decide  the key as written => the decision its entry is reported under
71
         * @param  ?\Closure(string): ?Layer  $findOrigin  the key as written => the section of the upgrading data that laid it
72
         * @return self<V>
73
         */
74
        private static function build(array $entries, ?\Closure $convert, \Closure $decide, ?\Closure $findOrigin = null): self
75
        {
76
                $byName = [];
1 ✔
77
                foreach ($entries as $key => $value) {
1 ✔
78
                        if (MemberMapGrammar::isFunctionKey((string) $key)) {
1 ✔
79
                                continue; // a function, which replacedCalls has keys of too, is no member
1 ✔
80
                        }
81

82
                        $pattern = MemberPattern::fromKey((string) $key);
1 ✔
83
                        $byName[$pattern->getLookupName()][] = new MemberMapEntry(
1 ✔
84
                                $pattern,
85
                                $convert === null ? $value : $convert($value, $pattern, (string) $key),
1 ✔
86
                                $decide((string) $key),
1 ✔
87
                                $findOrigin === null ? null : $findOrigin((string) $key),
1 ✔
88
                        );
89
                }
90

91
                return new self($byName);
1 ✔
92
        }
93

94

95
        /**
96
         * The entries of the name in any letter case, in the order the map writes them; a rule asks them before it asks
97
         * the types, which it then asks only about a name the map knows.
98
         * @return list<MemberMapEntry<T>>
99
         */
100
        public function getEntries(string $name): array
101
        {
102
                return $this->byName[strtolower($name)] ?? [];
1 ✔
103
        }
104

105

106
        /** Whether a key of the map is of the access. */
107
        public function has(MemberAccess $access, Types $types): bool
108
        {
109
                return array_any($this->getEntries($access->name), fn(MemberMapEntry $entry) => $entry->pattern->matches($access, $types));
1 ✔
110
        }
111

112

113
        /**
114
         * The entry the access is of, the first `findDecidingEntry()` gives of those whose key the access is of and, where
115
         * the arguments of the call are given, whose arguments they bind to, the key whose arguments have the more specific
116
         * shape asked first; null where none is.
117
         * @param  ?list<Parameter>  $parameters  of the method called, null where nothing declares it
118
         * @return ?MemberMapMatch<T>
119
         */
120
        public function findAccess(
121
                MemberAccess $access,
122
                Types $types,
123
                ?ArgumentListNode $arguments = null,
124
                ?array $parameters = null,
125
        ): ?MemberMapMatch
126
        {
127
                $bindings = [];
1 ✔
128
                $entry = $this->findDecidingEntry(
1 ✔
129
                        strtolower($access->name),
1 ✔
130
                        $types,
131
                        function (MemberMapEntry $entry) use ($access, $types, $arguments, $parameters, &$bindings): bool {
1 ✔
132
                                if ($arguments === null) {
1 ✔
133
                                        return $entry->pattern->matches($access, $types);
1 ✔
134
                                }
135

136
                                $bound = $entry->pattern->bind($access, $arguments, $parameters, $types);
1 ✔
137
                                if ($bound !== null) {
1 ✔
138
                                        $bindings[spl_object_id($entry)] = $bound;
1 ✔
139
                                }
140

141
                                return $bound !== null;
1 ✔
142
                        },
1 ✔
143
                        specificFirst: $arguments !== null,
1 ✔
144
                );
145
                return $entry === null ? null : new MemberMapMatch($entry, $access, $bindings[spl_object_id($entry)] ?? null);
1 ✔
146
        }
147

148

149
        /**
150
         * The entry the call is of by the shape of its arguments, the most specific one, with what it binds them to; null
151
         * for none. An instantiation is an access of the constructor of the class it creates.
152
         * @return ?MemberMapMatch<T>
153
         */
154
        public function findCall(MethodCallNode|StaticMethodCallNode|NewNode $node, RuleContext $context): ?MemberMapMatch
155
        {
156
                $name = self::findLookupName($node);
1 ✔
157
                if ($name === null || $this->getEntries($name) === []) {
1 ✔
158
                        return null; // the types are asked only about a name the map knows
1 ✔
159
                }
160

161
                $types = $context->getAnalysis(Types::class);
1 ✔
162
                $access = $types->findConstructorAccess($node) ?? $types->findMemberAccess($node);
1 ✔
163
                return $access === null
1 ✔
164
                        ? null
1 ✔
165
                        : $this->findAccess($access, $types, $node->arguments ?? (new Builder)->arguments([]), $types->findParameters($access));
1 ✔
166
        }
167

168

169
        /**
170
         * The entry whose key is the method the declaration overrides, the key whose arguments have the more specific shape
171
         * first where `$specificFirst` asks for it; with `$anyArguments` only a key taking any arguments, the declaration
172
         * being of no call; null where none is.
173
         * @return ?MemberMapEntry<T>
174
         */
175
        public function findDeclaration(
176
                MethodNode $declaration,
177
                Types $types,
178
                bool $anyArguments = false,
179
                bool $specificFirst = false,
180
        ): ?MemberMapEntry
181
        {
182
                $name = strtolower($declaration->name->text);
1 ✔
183
                $class = $this->getEntries($name) === [] ? null : $types->findDeclaringClass($declaration);
1 ✔
184
                return $class === null
1 ✔
UNCOV
185
                        ? null
×
186
                        : $this->findDecidingEntry(
1 ✔
187
                                $name,
1 ✔
188
                                $types,
189
                                fn(MemberMapEntry $entry) => (!$anyArguments || $entry->pattern->takesAnyArguments())
1 ✔
190
                                        && $entry->pattern->matchesMethodDeclaration($class, $declaration->name->text, $types),
1 ✔
191
                                specificFirst: $specificFirst,
192
                        );
193
        }
194

195

196
        /**
197
         * The entry of the lowercased name that decides among those `$accepts` takes, the first as the map writes them that
198
         * no other one taken precedes: with `$specificFirst` the key whose arguments have the more specific shape precedes,
199
         * then the key of a class precedes that of its ancestor, so that of two keys an access fits both, the one of the
200
         * class nearer to it decides; null where none is taken.
201
         * @param  \Closure(MemberMapEntry<T>): bool  $accepts
202
         * @return ?MemberMapEntry<T>
203
         */
204
        public function findDecidingEntry(string $name, Types $types, \Closure $accepts, bool $specificFirst = false): ?MemberMapEntry
205
        {
206
                // the order is partial, which a sort cannot be given
207
                $accepted = array_filter($this->getEntries($name), $accepts);
1 ✔
208
                return array_find(
1 ✔
209
                        $accepted,
210
                        fn(MemberMapEntry $entry) => !array_any(
1 ✔
211
                                $accepted,
212
                                fn(MemberMapEntry $other) => self::precedes($other->pattern, $entry->pattern, $types, $specificFirst),
1 ✔
213
                        ),
1 ✔
214
                );
215
        }
216

217

218
        private static function precedes(MemberPattern $a, MemberPattern $b, Types $types, bool $specificFirst): bool
219
        {
220
                $result = $specificFirst ? $a->compareSpecificity($b) : 0;
1 ✔
221
                return $result < 0
1 ✔
222
                        || ($result === 0 && strcasecmp($a->class, $b->class) !== 0 && $types->isSubtype($a->class, $b->class) === Tristate::Yes);
1 ✔
223
        }
224

225

226
        /**
227
         * The lowercased name `getEntries()` is asked by for a node reaching a member, `__construct` for an instantiation;
228
         * null where the name is an expression.
229
         */
230
        public static function findLookupName(
231
                ClassConstantFetchNode|MethodCallNode|StaticMethodCallNode|PropertyFetchNode|StaticPropertyFetchNode|NewNode $node,
232
        ): ?string
233
        {
234
                $name = match (true) {
1 ✔
235
                        $node instanceof NewNode => '__construct',
1 ✔
236
                        $node instanceof StaticPropertyFetchNode => $node->plainName,
1 ✔
237
                        $node->name instanceof IdentifierNode => $node->name->text,
1 ✔
238
                        default => null,
1 ✔
239
                };
240
                return $name === null ? null : strtolower($name);
1 ✔
241
        }
242
}
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