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

dg / dresscode / 41320243908

30 Oct 2026 03:50PM UTC coverage: 96.73% (-0.3%) from 97.034%
41320243908

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%)

463 existing lines in 55 files now uncovered.

29283 of 30273 relevant lines covered (96.73%)

0.97 hits per line

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

96.61
/src/Upgrade/PlanFile.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\Upgrade;
9

10
use DressCode\Config\Versions;
11
use DressCode\ConfigurationException;
12
use DressCode\Engine\Helpers;
13
use Nette\IOException;
14
use Nette\Neon\{Exception as NeonException, Neon};
15
use Nette\Utils\FileSystem;
16
use function is_array, is_bool, is_string;
17

18

19
/**
20
 * The plan of an upgrade as the file `dresscode-upgrade.neon` writes it, which the team commits, reviews and edits: a
21
 * step left out with `skip: true`, the topics of a fix narrowed, the commands of the tests given. The one shape of the
22
 * namespace a user writes, so it is read strictly: a key it does not know or a value of another type is refused with
23
 * where it stands, rather than read as something else.
24
 * @internal
25
 */
26
final class PlanFile
27
{
28
        /** the name of the file in the root of the project */
29
        public const Name = 'dresscode-upgrade.neon';
30

31
        /** the version of the shape of the file */
32
        public const Version = 1;
33

34
        /** the step => its kind, the word of the file */
35
        private const Kinds = [
36
                FixStep::class => 'fix',
37
                ComposerStep::class => 'composer',
38
                UpdateStep::class => 'update',
39
                ManualStep::class => 'manual',
40
        ];
41

42
        /** the keys of a plan */
43
        private const Keys = ['version', 'toolVersion', 'dataVersions', 'targets', 'paths', 'tests', 'estimate', 'steps'];
44

45
        /** the keys of a step that its kind takes, beside `title`, `kind` and `skip` */
46
        private const StepKeys = ['fix' => ['topics'], 'composer' => ['require', 'update', 'platform'], 'update' => [], 'manual' => ['target']];
47

48

49
        /** Whether the project in the root has a saved plan. */
50
        public static function exists(string $root): bool
51
        {
52
                return is_file("$root/" . self::Name);
1 ✔
53
        }
54

55

56
        /** The identity of the saved plan, a hash of its text, by which a tool tells one plan from another; null without one. */
57
        public static function identify(string $root): ?string
58
        {
59
                $text = @file_get_contents("$root/" . self::Name); // @ the plan may not be saved
1 ✔
60
                return $text === false ? null : substr(hash('xxh128', $text), 0, 16);
1 ✔
61
        }
62

63

64
        /**
65
         * The saved plan of the project in the root.
66
         * @throws ConfigurationException  for a file that is no plan
67
         */
68
        public static function read(string $root): Plan
69
        {
70
                $file = "$root/" . self::Name;
1 ✔
71
                try {
72
                        $data = Neon::decode(FileSystem::read($file));
1 ✔
73
                } catch (NeonException|IOException $e) {
1 ✔
74
                        throw new ConfigurationException("The plan `$file` cannot be read: {$e->getMessage()}", previous: $e);
1 ✔
75
                }
76

77
                $fail = fn(string $what) => throw new ConfigurationException("The plan `$file`: $what.");
1 ✔
78
                if (!is_array($data)) {
1 ✔
UNCOV
79
                        $fail('a map of `version`, `targets` and `steps` is expected');
×
80
                } elseif (($data['version'] ?? null) !== self::Version) {
1 ✔
81
                        $fail('`version: ' . self::Version . '` is expected, the plan of another version of DressCode is made anew');
1 ✔
82
                } elseif (($unknown = array_diff(array_keys($data), self::Keys)) !== []) {
1 ✔
83
                        $fail('`' . reset($unknown) . '` is no key of a plan');
1 ✔
84
                }
85

86
                $targets = [];
1 ✔
87
                foreach (self::readMap($data['targets'] ?? [], 'targets', $fail) as $package => $version) {
1 ✔
88
                        if (!Target::isPackageName($package)) {
1 ✔
89
                                $fail("`targets.$package` must be `php` or a package as `vendor/name`");
1 ✔
90
                        } elseif (!Versions::isVersion($version)) {
1 ✔
UNCOV
91
                                $fail("`targets.$package` must be a version, as `'8.4'`");
×
92
                        }
93

94
                        $targets[] = new Target($package, $version);
1 ✔
95
                }
96

97
                $tests = self::readMap($data['tests'] ?? [], 'tests', $fail);
1 ✔
98
                if (($unknown = array_diff(array_keys($tests), ['command', 'quick'])) !== []) {
1 ✔
UNCOV
99
                        $fail('`tests.' . reset($unknown) . '` is no key of `tests`, which takes `command` and `quick`');
×
100
                }
101

102
                foreach ($tests as $key => $command) {
1 ✔
103
                        if (!TestResults::hasJunitPlaceholder($command)) {
1 ✔
104
                                $fail("`tests.$key` must be a command writing its JUnit report where `{junit}` stands");
1 ✔
105
                        }
106
                }
107

108
                $estimate = $data['estimate'] ?? [];
1 ✔
109
                if (!is_array($estimate) || ($unknown = array_diff(array_keys($estimate), ['unprocessed', 'missingData'])) !== []) {
1 ✔
110
                        $fail(is_array($estimate)
1 ✔
111
                                ? '`estimate.' . reset($unknown) . '` is no key of `estimate`, which takes `unprocessed` and `missingData`'
1 ✔
112
                                : '`estimate` must be a map');
1 ✔
113
                }
114

115
                $missingData = [];
1 ✔
116
                $missing = $estimate['missingData'] ?? [];
1 ✔
117
                foreach (is_array($missing) ? $missing : $fail('`estimate.missingData` must be a map of lists') as $package => $libraries) {
1 ✔
118
                        $missingData[(string) $package] = self::readList($libraries, "estimate.missingData.$package", $fail);
1 ✔
119
                }
120

121
                $steps = $data['steps'] ?? [];
1 ✔
122
                if (!is_array($steps) || !array_is_list($steps)) {
1 ✔
UNCOV
123
                        $fail('`steps` must be a list');
×
124
                }
125

126
                foreach ($steps as $index => $step) {
1 ✔
127
                        $steps[$index] = self::readStep($step, 'steps.' . ($index + 1), $targets, $fail);
1 ✔
128
                }
129

130
                if ($targets === [] || $steps === []) {
1 ✔
131
                        $fail('`targets` and `steps` are expected');
1 ✔
132
                }
133

134
                return new Plan(
1 ✔
135
                        $targets,
1 ✔
136
                        $steps,
137
                        self::readString($data['toolVersion'] ?? '', 'toolVersion', $fail),
1 ✔
138
                        self::readMap($data['dataVersions'] ?? [], 'dataVersions', $fail),
1 ✔
139
                        $tests['command'] ?? null,
1 ✔
140
                        $tests['quick'] ?? null,
1 ✔
141
                        self::readList($data['paths'] ?? [], 'paths', $fail),
1 ✔
142
                        self::readList($estimate['unprocessed'] ?? [], 'estimate.unprocessed', $fail),
1 ✔
143
                        $missingData,
144
                );
145
        }
146

147

148
        /** Saves the plan into the root of its project. */
149
        public static function write(Plan $plan, string $root): void
150
        {
151
                $steps = array_map(fn(Step $step) => [...self::encodeStep($step), ...$step->skip ? ['skip' => true] : []], $plan->steps);
1 ✔
152
                $data = array_filter([
1 ✔
153
                        'version' => self::Version,
1 ✔
154
                        'toolVersion' => $plan->toolVersion,
1 ✔
155
                        'dataVersions' => $plan->dataVersions,
1 ✔
156
                        'targets' => Target::collectVersions($plan->targets),
1 ✔
157
                        'paths' => $plan->paths,
1 ✔
158
                        'tests' => array_filter(['command' => $plan->testCommand, 'quick' => $plan->quickTestCommand]),
1 ✔
159
                        'estimate' => array_filter(['unprocessed' => $plan->unestimated, 'missingData' => $plan->missingData]),
1 ✔
160
                        'steps' => $steps,
1 ✔
161
                ], fn($value) => $value !== []);
1 ✔
162
                $header = '# dresscode upgrade: the plan for ' . implode(', ', array_map(fn(Target $target) => $target->format(), $plan->targets))
1 ✔
163
                        . "; set `skip: true` to leave a step out, and `dresscode upgrade run` runs what is left\n\n";
1 ✔
164
                Helpers::replaceFile("$root/" . self::Name, $header . Neon::encode($data, blockMode: true));
1 ✔
165
        }
1 ✔
166

167

168
        /**
169
         * The step as the plan file writes it, `skip` aside.
170
         * @return array<string, mixed>
171
         */
172
        public static function encodeStep(Step $step): array
173
        {
174
                return [
175
                        'title' => $step->title,
1 ✔
176
                        'kind' => self::Kinds[$step::class],
1 ✔
177
                        ...match (true) {
178
                                $step instanceof FixStep => ['topics' => $step->topics],
1 ✔
179
                                $step instanceof ComposerStep => [
1 ✔
180
                                        'require' => $step->require,
1 ✔
181
                                        ...$step->update ? [] : ['update' => false],
1 ✔
182
                                        ...$step->platform === null ? [] : ['platform' => $step->platform],
1 ✔
183
                                ],
184
                                $step instanceof ManualStep => ['target' => $step->target->package],
1 ✔
185
                                default => [],
1 ✔
186
                        },
187
                ];
188
        }
189

190

191
        /**
192
         * @param  list<Target>  $targets  those of the plan, one of which a manual step makes way for
193
         * @param  \Closure(string): never  $fail
194
         */
195
        private static function readStep(mixed $step, string $at, array $targets, \Closure $fail): Step
196
        {
197
                $kind = is_array($step) ? $step['kind'] ?? null : null;
1 ✔
198
                if (!is_string($kind) || !isset(self::StepKeys[$kind])) {
1 ✔
199
                        $fail("`$at` must give its `kind`, `fix`, `composer`, `update` or `manual`");
1 ✔
200
                } elseif (($unknown = array_diff(array_keys($step), ['title', 'kind', 'skip', ...self::StepKeys[$kind]])) !== []) {
1 ✔
201
                        $fail("`$at." . reset($unknown) . "` is no key of a step of the kind `$kind`");
1 ✔
202
                }
203

204
                $title = self::readString($step['title'] ?? $kind, "$at.title", $fail);
1 ✔
205
                $skip = self::readBool($step['skip'] ?? false, "$at.skip", $fail);
1 ✔
206
                return match ($kind) {
1 ✔
207
                        'fix' => new FixStep(
1 ✔
208
                                $title,
1 ✔
209
                                self::readList($step['topics'] ?? [], "$at.topics", $fail) ?: $fail("`$at.topics` must name the topics the step fixes"),
1 ✔
210
                                $skip,
211
                        ),
212
                        'composer' => new ComposerStep(
1 ✔
213
                                $title,
1 ✔
214
                                self::readConstraints(
1 ✔
215
                                        $step['require'] ?? [],
1 ✔
216
                                        "$at.require",
1 ✔
217
                                        $fail,
218
                                        required: !isset($step['platform']),
1 ✔
219
                                ),
220
                                self::readBool($step['update'] ?? true, "$at.update", $fail),
1 ✔
221
                                isset($step['platform']) ? self::readVersion($step['platform'], "$at.platform", $fail) : null,
1 ✔
222
                                $skip,
223
                        ),
224
                        'update' => new UpdateStep($title, $skip),
1 ✔
225
                        default => new ManualStep(
1 ✔
226
                                $title,
1 ✔
227
                                array_find($targets, fn(Target $target) => $target->package === ($step['target'] ?? null))
1 ✔
228
                                ?? $fail("`$at.target` must name a package of `targets`"),
1 ✔
229
                                $skip,
230
                        ),
231
                };
232
        }
233

234

235
        /**
236
         * @param  \Closure(string): never  $fail
237
         * @return array<string, string>  package => constraint
238
         */
239
        private static function readConstraints(mixed $value, string $at, \Closure $fail, bool $required): array
240
        {
241
                $require = self::readMap($value, $at, $fail);
1 ✔
242
                if ($require === [] && $required) {
1 ✔
243
                        $fail("`$at` must give the constraints the step writes, or the step its `platform`");
1 ✔
244
                }
245

246
                foreach ($require as $package => $constraint) {
1 ✔
247
                        if (!Target::isPackageName($package)) {
1 ✔
248
                                $fail("`$at.$package` must be `php` or a package as `vendor/name`");
1 ✔
249
                        } elseif (!Versions::isConstraint($constraint)) {
1 ✔
250
                                $fail("`$at.$package` must be a constraint Composer reads, as `^8.4`");
1 ✔
251
                        }
252
                }
253

254
                return $require;
1 ✔
255
        }
256

257

258
        /**
259
         * @param  \Closure(string): never  $fail
260
         * @return list<string>
261
         */
262
        private static function readList(mixed $value, string $at, \Closure $fail): array
263
        {
264
                return is_array($value) && array_is_list($value) && array_all($value, fn($item) => is_string($item))
1 ✔
265
                        ? $value
1 ✔
266
                        : $fail("`$at` must be a list of strings");
1 ✔
267
        }
268

269

270
        /**
271
         * @param  \Closure(string): never  $fail
272
         * @return array<string, string>
273
         */
274
        private static function readMap(mixed $value, string $at, \Closure $fail): array
275
        {
276
                return is_array($value) && array_all($value, fn($item, $key) => is_string($key) && is_string($item))
1 ✔
277
                        ? $value
1 ✔
278
                        : $fail("`$at` must be a map of strings, a version quoted as `'8.4'`");
1 ✔
279
        }
280

281

282
        /** @param  \Closure(string): never  $fail */
283
        private static function readVersion(mixed $value, string $at, \Closure $fail): string
284
        {
285
                return is_string($value) && Versions::isVersion($value) ? $value : $fail("`$at` must be a version, as `'8.4'`");
1 ✔
286
        }
287

288

289
        /** @param  \Closure(string): never  $fail */
290
        private static function readString(mixed $value, string $at, \Closure $fail): string
291
        {
292
                return is_string($value) ? $value : $fail("`$at` must be a string");
1 ✔
293
        }
294

295

296
        /** @param  \Closure(string): never  $fail */
297
        private static function readBool(mixed $value, string $at, \Closure $fail): bool
298
        {
299
                return is_bool($value) ? $value : $fail("`$at` must be `true` or `false`");
1 ✔
300
        }
301
}
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