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

dg / dresscode / 42041664657

30 Oct 2026 03:50PM UTC coverage: 97.067% (-0.7%) from 97.783%
42041664657

push

github

dg
cli: the hidden option --profile writes where the time of a run went, the workers included

`--profile <file>` writes as JSON where the time of a run went, in nanoseconds: the phases (the start of the process,
the start of the container of PHPStan or of a derived one and its scopes, the parse, the passes, the gaps, the print,
the cycle collection, the run as a whole), every rule by the time of its callbacks, how often they ran and how often
they changed the tree, every gap rule by the time of its claims, every file with its size, rounds and passes, and the
peak memory of every process. A worker measures its own files and hands that over with every result and the parent
adds it up, so the totals do not depend on the number of workers. A run with the option leaves the result cache out,
a file served from the cache having nothing to measure.

It says where the time of a run goes, not only how much of it: a comparison of two revisions then names the rule or
the claim that moved. Without the option the engine only asks whether there is a profiler, and wraps a claim in
a measurement only when there is one.

137 of 148 new or added lines in 14 files covered. (92.57%)

162 existing lines in 35 files now uncovered.

29456 of 30346 relevant lines covered (97.07%)

0.97 hits per line

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

93.23
/src/Config/UpgradingFile.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\Config;
9

10
use DressCode\{ConfigurationException, Profile};
11
use DressCode\Domains\Map;
12
use DressCode\Upgrading\{FunctionPattern, MemberMapGrammar, MemberPattern};
13
use Nette\Neon\{Exception as NeonException, Neon};
14
use function array_key_exists, is_array, is_string;
15

16

17
/**
18
 * An upgrading file as it is written: the package whose versions its sections are of, what the namespaces of the
19
 * package declare, and the maps every section brings with the requirements of the rules of the plugin of the package it
20
 * turns on, which `selectFor()` lays out for the versions of the package the code must work with.
21
 * @internal
22
 */
23
final readonly class UpgradingFile
24
{
25
        /** the maps of what the code must not have, by which what a version above the lowest one retires is reported */
26
        private const ForbiddenMaps = ['forbiddenClasses', 'forbiddenFunctions', 'forbiddenConstants', 'forbiddenMembers'];
27

28
        private const SectionPattern = '~^since (\d+(?:\.\d+)*)$~D';
29
        private const PackagePattern = '~^(?:php|[a-z0-9_.-]+/[a-z0-9_.-]+)$~D';
30
        private const VersionPattern = '~^\d+(?:\.\d+)*$~D';
31

32

33
        private function __construct(
34
                /** the package the sections are versions of, `php` for PHP itself */
35
                public string $package,
1 ✔
36
                /** @var array{functions: list<string>, constants: list<string>}  what the package declares in its namespaces, fully qualified */
37
                public array $namespaces,
1 ✔
38
                /** @var array<string, array<string, array<string, mixed>>>  version => map => its entries, the oldest first */
39
                private array $sections,
1 ✔
40
                /** @var array<string, array<string, string>>  version => a requirement of the plugin of the package => its value */
41
                private array $requirements = [],
1 ✔
42
        ) {
43
        }
1 ✔
44

45

46
        /**
47
         * Reads the file, a value as NEON gives it, an entity too, for the grammar of the map to read.
48
         * @throws ConfigurationException
49
         */
50
        public static function fromFile(string $file, string $label): self
51
        {
52
                if (!is_file($file)) {
1 ✔
53
                        throw new ConfigurationException("Upgrading file $label does not exist.");
×
54
                }
55

56
                try {
57
                        $data = Neon::decodeFile($file);
1 ✔
58
                } catch (NeonException $e) {
1 ✔
59
                        throw new ConfigurationException("Upgrading file $label is not valid NEON: {$e->getMessage()}", previous: $e);
1 ✔
60
                }
61

62
                $package = is_array($data) ? $data['package'] ?? null : null;
1 ✔
63
                if (!is_string($package) || !preg_match(self::PackagePattern, $package)) {
1 ✔
64
                        throw new ConfigurationException("Upgrading file $label: The key `package` must name the package the sections are versions of, as `vendor/name`, or `php`.");
1 ✔
65
                }
66

67
                $sections = $requirements = [];
1 ✔
68
                foreach ($data as $key => $section) {
1 ✔
69
                        if ($key === 'package' || $key === 'namespaces') {
1 ✔
70
                                continue;
1 ✔
71
                        } elseif (!is_string($key) || !preg_match(self::SectionPattern, $key, $m)) {
1 ✔
72
                                throw new ConfigurationException("Upgrading file $label: Unexpected key `$key`; the file holds `package`, `namespaces` and sections `since <version>`.");
1 ✔
73
                        } elseif (!is_array($section) || (array_is_list($section) && $section !== [])) {
1 ✔
74
                                throw new ConfigurationException("Upgrading file $label: The section `$key` must be a map of maps to their entries.");
1 ✔
75
                        }
76

77
                        foreach ($section as $map => $entries) {
1 ✔
78
                                if (is_string($map) && str_contains($map, '.') && is_string($entries)) {
1 ✔
79
                                        $requirements[$m[1]][$map] = $entries;
1 ✔
80
                                        continue;
1 ✔
81
                                } elseif (!is_string($map) || !is_array($entries)) {
1 ✔
82
                                        throw new ConfigurationException("Upgrading file $label: The map `$map` in `$key` must be a map of entries; a package turns on only a rule of its own plugin, by the path of its decision and a word.");
1 ✔
83
                                } elseif (in_array($map, self::ForbiddenMaps, true)) {
1 ✔
84
                                        try {
85
                                                $entries = Map::expandBareNames($entries, $map);
1 ✔
86
                                        } catch (ConfigurationException $e) {
×
87
                                                throw new ConfigurationException("Upgrading file $label, `$key`: {$e->getMessage()}", previous: $e);
×
88
                                        }
89
                                }
90

91
                                foreach ($entries as $entry => $value) {
1 ✔
92
                                        $from = is_array($value) ? $value['from'] ?? null : null;
1 ✔
93
                                        if ($from !== null && (!is_string($from) || !preg_match(self::VersionPattern, $from))) {
1 ✔
94
                                                throw new ConfigurationException("Upgrading file $label: `from` of the entry `$entry` of `$map` in `$key` must be a version, as `'8.0'`.");
1 ✔
95
                                        }
96
                                }
97

98
                                $sections[$m[1]][$map] = $entries;
1 ✔
99
                        }
100
                }
101

102
                uksort($sections, fn(string $a, string $b) => version_compare($a, $b));
1 ✔
103
                uksort($requirements, fn(string $a, string $b) => version_compare($a, $b));
1 ✔
104
                try {
105
                        $namespaces = new Profile(namespaces: is_array($data['namespaces'] ?? null) ? $data['namespaces'] : [])->namespaces;
1 ✔
106
                } catch (\InvalidArgumentException $e) {
×
107
                        throw new ConfigurationException("Upgrading file $label: {$e->getMessage()}", previous: $e);
×
108
                }
109

110
                return new self($package, $namespaces, $sections, $requirements);
1 ✔
111
        }
112

113

114
        /**
115
         * Every section as it is written, the oldest first: its maps, the entries without the version `from` says their fix
116
         * is right from, and the requirements it turns on.
117
         * @return array<string, array{array<string, array<string, mixed>>, array<string, string>}>  version => maps, requirements
118
         */
119
        public function getSections(): array
120
        {
121
                $versions = array_unique(array_map(strval(...), [...array_keys($this->sections), ...array_keys($this->requirements)]));
1 ✔
122
                usort($versions, fn(string $a, string $b) => version_compare($a, $b));
1 ✔
123
                $sections = [];
1 ✔
124
                foreach ($versions as $version) {
1 ✔
125
                        $sections[$version] = [
1 ✔
126
                                array_map(fn(array $entries) => array_map(self::removeFrom(...), $entries), $this->sections[$version] ?? []),
1 ✔
127
                                $this->requirements[$version] ?? [],
1 ✔
128
                        ];
129
                }
130

131
                return $sections;
1 ✔
132
        }
133

134

135
        /**
136
         * The data for code that must work with the versions of the constraint, null where any version does. An entry is
137
         * rewritten where the lowest version reaches its section, or the version `from` says its fix is right from, a later
138
         * section having the last word on it: every entry where any version does, none where there is no lower bound.
139
         * What a section above the lowest version retires, where a version the constraint allows reaches it, is reported
140
         * without a fix as the entry of a forbidden map (`retire()`), unless a section reached rewrites or forbids the same
141
         * thing already. A requirement is turned on where the lowest version reaches its section, and says nothing above it.
142
         * Every entry keeps the section and the version `from` it came with.
143
         */
144
        public function selectFor(?string $constraint, Layer $layer): UpgradingData
145
        {
146
                $lowest = $constraint === null ? null : Versions::findLowestVersion($constraint);
1 ✔
147
                $reaches = fn(string $version) => $constraint === null || ($lowest !== null && version_compare($lowest, $version, '>='));
1 ✔
148
                $maps = $retirements = $origins = $retired = $unreached = $reported = [];
1 ✔
149
                foreach ($this->sections as $since => $section) {
1 ✔
150
                        $since = (string) $since;
1 ✔
151
                        $left = [];
1 ✔
152
                        foreach ($section as $map => $entries) {
1 ✔
153
                                foreach ($entries as $key => $value) {
1 ✔
154
                                        $from = is_array($value) ? $value['from'] ?? null : null;
1 ✔
155
                                        if (!$reaches($since) && ($from === null || !$reaches($from))) {
1 ✔
156
                                                $left[$map][$key] = $value;
1 ✔
157
                                                continue;
1 ✔
158
                                        }
159

160
                                        $maps[$map][$key] = self::removeFrom($value);
1 ✔
161
                                        $origins[$map][$key] = [$since, $from];
1 ✔
162
                                        unset($retirements[$map][$key]);
1 ✔
163
                                        if (in_array($map, self::ForbiddenMaps, true) && $value !== MemberMapGrammar::Keep) {
1 ✔
164
                                                $retirements[$map][$key] = $since;
1 ✔
165
                                        }
166
                                }
167
                        }
168

169
                        if ($left === [] || $constraint === null) {
1 ✔
170
                                continue;
1 ✔
171
                        }
172

173
                        $unreached[] = $since;
1 ✔
174
                        if (!Versions::isSubset($constraint, "<$since")) {
1 ✔
175
                                $reported[] = $since;
1 ✔
176
                                foreach (self::retire($left) as [$map, $key, $sentence, $version]) {
1 ✔
177
                                        $retired[$map][$key] = [$sentence, $since, $version];
1 ✔
178
                                }
179
                        }
180
                }
181

182
                $written = [];
1 ✔
183
                foreach ($maps as $map => $entries) {
1 ✔
184
                        foreach (array_keys($entries) as $key) {
1 ✔
185
                                $target = self::findForbidden($map, (string) $key);
1 ✔
186
                                if ($target !== null) {
1 ✔
187
                                        $written[$target[0]][self::identify(...$target)] = true;
1 ✔
188
                                }
189
                        }
190
                }
191

192
                foreach ($retired as $map => $entries) {
1 ✔
193
                        foreach ($entries as $key => [$sentence, $since, $version]) {
1 ✔
194
                                if (!isset($written[$map][self::identify($map, $key)])) {
1 ✔
195
                                        $maps[$map][$key] = $sentence;
1 ✔
196
                                        $origins[$map][$key] = [$since, $version];
1 ✔
197
                                        $retirements[$map][$key] = $version ?? $since;
1 ✔
198
                                }
199
                        }
200
                }
201

202
                $requirements = [];
1 ✔
203
                foreach ($this->requirements as $since => $values) {
1 ✔
204
                        if ($reaches((string) $since)) {
1 ✔
205
                                $requirements = array_replace($requirements, $values);
1 ✔
206
                        }
207
                }
208

209
                return new UpgradingData(
1 ✔
210
                        $layer,
1 ✔
211
                        $this->package,
1 ✔
212
                        $this->namespaces,
1 ✔
213
                        $maps,
214
                        $retirements,
215
                        $unreached,
216
                        $reported,
217
                        $requirements,
218
                        $origins,
219
                        $this->findNewestVersion(),
1 ✔
220
                );
221
        }
222

223

224
        /** The newest version a section is of, the sections being in order of their versions. */
225
        private function findNewestVersion(): ?string
226
        {
227
                $newest = null;
1 ✔
228
                foreach ([array_key_last($this->sections), array_key_last($this->requirements)] as $version) {
1 ✔
229
                        if ($version !== null && ($newest === null || version_compare((string) $version, $newest, '>'))) {
1 ✔
230
                                $newest = (string) $version;
1 ✔
231
                        }
232
                }
233

234
                return $newest;
1 ✔
235
        }
236

237

238
        /**
239
         * What entries left above the lowest version say the code still meets on a version it may run on, as entries of the
240
         * forbidden maps (`findForbidden()`), since what they write instead is not there in the lowest one: what they forbid,
241
         * and what they replace, each with its sentence and the version it says, where that is not the one of its section.
242
         * @param  array<string, array<string, mixed>>  $section  map => its entries
243
         * @return list<array{string, string, ?string, ?string}>  forbidden map, key, sentence, version
244
         */
245
        private static function retire(array $section): array
246
        {
247
                $retired = [];
1 ✔
248
                foreach ($section as $map => $entries) {
1 ✔
249
                        foreach ($entries as $key => $value) {
1 ✔
250
                                [$write, $from] = is_array($value) ? [$value['write'] ?? null, $value['from'] ?? null] : [$value, null];
1 ✔
251
                                $target = self::findForbidden($map, (string) $key);
1 ✔
252
                                if ($target === null || $write === MemberMapGrammar::Keep) {
1 ✔
253
                                        continue;
1 ✔
254
                                }
255

256
                                [$forbidden, $name] = $target;
1 ✔
257
                                $retired[] = match (true) {
1 ✔
258
                                        $map === $forbidden => [$forbidden, $name, $write, null],
1 ✔
259
                                        $write === MemberMapGrammar::Removed => [$forbidden, $name, 'does nothing', $from],
1 ✔
260
                                        is_string($write) => [$forbidden, $name, 'replaced by `' . ltrim($write, '\\') . '`', null],
1 ✔
261
                                        default => [$forbidden, $name, 'replaced', null],
1 ✔
262
                                };
263
                        }
264
                }
265

266
                return $retired;
1 ✔
267
        }
268

269

270
        /**
271
         * The forbidden map an entry of the map is reported by where it cannot be rewritten, and its key there: a call of
272
         * a function as the function, a call of a member by the shape of its arguments as `forbiddenMembers` reads it; null
273
         * for a map that forbids nothing by itself, an annotation and the maps of plugins, and for a key that is no name.
274
         * @return ?array{string, string}
275
         */
276
        private static function findForbidden(string $map, string $key): ?array
277
        {
278
                try {
279
                        return match (true) {
280
                                in_array($map, self::ForbiddenMaps, true) => [$map, $key],
1 ✔
281
                                $map === 'replacedClasses' => ['forbiddenClasses', $key],
1 ✔
282
                                $map === 'replacedFunctions' => ['forbiddenFunctions', $key],
1 ✔
283
                                $map === 'replacedMembers' => ['forbiddenMembers', $key],
1 ✔
284
                                $map === 'replacedCalls' && MemberMapGrammar::isFunctionKey($key)
1 ✔
285
                                        => ['forbiddenFunctions', FunctionPattern::fromKey($key)->name],
1 ✔
286
                                $map === 'replacedCalls' => ['forbiddenMembers', $key],
1 ✔
287
                                default => null,
1 ✔
288
                        };
UNCOV
289
                } catch (\InvalidArgumentException) {
×
UNCOV
290
                        return null; // the grammar of the map says what is wrong with the key
×
291
                }
292
        }
293

294

295
        /**
296
         * What the key of a forbidden map names whatever way it is written, so that one rewritten and one forbidden meet: a
297
         * member by its class and name, a class, a function and a constant by its name.
298
         */
299
        private static function identify(string $forbidden, string $key): string
300
        {
301
                try {
302
                        $member = $forbidden === 'forbiddenMembers' ? MemberPattern::fromKey($key) : null;
1 ✔
UNCOV
303
                } catch (\InvalidArgumentException) {
×
UNCOV
304
                        $member = null;
×
305
                }
306

307
                return strtolower($member === null ? ltrim($key, '\\') : "$member->class::$member->name");
1 ✔
308
        }
309

310

311
        /** The entry as the map reads it, without the version it is right from; `{write: X}` is X. */
312
        private static function removeFrom(mixed $value): mixed
313
        {
314
                if (!is_array($value) || !array_key_exists('from', $value)) {
1 ✔
315
                        return $value;
1 ✔
316
                }
317

318
                unset($value['from']);
1 ✔
319
                return array_keys($value) === ['write'] ? $value['write'] : $value;
1 ✔
320
        }
321
}
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