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

dg / dresscode / 41232180578

31 Oct 2026 06:57PM UTC coverage: 97.034% (-0.06%) from 97.095%
41232180578

push

github

dg
upgrade: the plan file refuses a name that is no package and a constraint Composer does not read, with the path of the key, rather than handing them to Composer

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

13 of 13 new or added lines in 3 files covered. (100.0%)

141 existing lines in 12 files now uncovered.

27840 of 28691 relevant lines covered (97.03%)

0.97 hits per line

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

95.65
/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 Nette\IOException;
13
use Nette\Neon\{Exception as NeonException, Neon};
14
use Nette\Utils\FileSystem;
15
use function is_array, is_bool, is_string;
16

17

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

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

33
        /** the step => its kind, the word of the file */
34
        public const Kinds = [FixStep::class => 'fix', ComposerStep::class => 'composer', ManualStep::class => 'manual'];
35

36
        /** the keys of a plan */
37
        private const Keys = ['version', 'toolVersion', 'dataVersions', 'targets', 'paths', 'tests', 'steps'];
38

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

42

43
        /** Whether the project in the root has a saved plan. */
44
        public static function exists(string $root): bool
45
        {
46
                return is_file("$root/" . self::Name);
1 ✔
47
        }
48

49

50
        /**
51
         * The saved plan of the project in the root.
52
         * @throws ConfigurationException  for a file that is no plan
53
         */
54
        public static function read(string $root): Plan
55
        {
56
                $file = "$root/" . self::Name;
1 ✔
57
                try {
58
                        $data = Neon::decode(FileSystem::read($file));
1 ✔
59
                } catch (NeonException|IOException $e) {
1 ✔
60
                        throw new ConfigurationException("The plan `$file` cannot be read: {$e->getMessage()}", previous: $e);
1 ✔
61
                }
62

63
                $fail = fn(string $what) => throw new ConfigurationException("The plan `$file`: $what.");
1 ✔
64
                if (!is_array($data)) {
1 ✔
65
                        $fail('a map of `version`, `targets` and `steps` is expected');
×
66
                } elseif (($data['version'] ?? null) !== self::Version) {
1 ✔
67
                        $fail('`version: ' . self::Version . '` is expected, the plan of another version of DressCode is made anew');
1 ✔
68
                } elseif (($unknown = array_diff(array_keys($data), self::Keys)) !== []) {
1 ✔
69
                        $fail('`' . reset($unknown) . '` is no key of a plan');
1 ✔
70
                }
71

72
                $targets = [];
1 ✔
73
                foreach (self::readMap($data['targets'] ?? [], 'targets', $fail) as $package => $version) {
1 ✔
74
                        if (!Target::isPackageName($package)) {
1 ✔
75
                                $fail("`targets.$package` must be `php` or a package as `vendor/name`");
1 ✔
76
                        } elseif (!Versions::isVersion($version)) {
1 ✔
UNCOV
77
                                $fail("`targets.$package` must be a version, as `'8.4'`");
×
78
                        }
79

80
                        $targets[] = new Target($package, $version);
1 ✔
81
                }
82

83
                $tests = self::readMap($data['tests'] ?? [], 'tests', $fail);
1 ✔
84
                if (($unknown = array_diff(array_keys($tests), ['command', 'quick'])) !== []) {
1 ✔
85
                        $fail('`tests.' . reset($unknown) . '` is no key of `tests`, which takes `command` and `quick`');
×
86
                }
87

88
                $steps = $data['steps'] ?? [];
1 ✔
89
                if (!is_array($steps) || !array_is_list($steps)) {
1 ✔
90
                        $fail('`steps` must be a list');
×
91
                }
92

93
                foreach ($steps as $index => $step) {
1 ✔
94
                        $steps[$index] = self::readStep($step, 'steps.' . ($index + 1), $targets, $fail);
1 ✔
95
                }
96

97
                if ($targets === [] || $steps === []) {
1 ✔
98
                        $fail('`targets` and `steps` are expected');
1 ✔
99
                }
100

101
                return new Plan(
1 ✔
102
                        $targets,
103
                        $steps,
104
                        self::readString($data['toolVersion'] ?? '', 'toolVersion', $fail),
1 ✔
105
                        self::readMap($data['dataVersions'] ?? [], 'dataVersions', $fail),
1 ✔
106
                        $tests['command'] ?? null,
1 ✔
107
                        $tests['quick'] ?? null,
1 ✔
108
                        self::readList($data['paths'] ?? [], 'paths', $fail),
1 ✔
109
                );
110
        }
111

112

113
        /** Saves the plan into the root of its project. */
114
        public static function write(Plan $plan, string $root): void
115
        {
116
                $steps = array_map(fn(Step $step) => [
1 ✔
117
                        'title' => $step->title,
1 ✔
118
                        'kind' => self::Kinds[$step::class],
1 ✔
119
                        ...match (true) {
120
                                $step instanceof FixStep => ['topics' => $step->topics],
1 ✔
121
                                $step instanceof ComposerStep => ['require' => $step->require, ...$step->update ? [] : ['update' => false]],
1 ✔
122
                                $step instanceof ManualStep => ['target' => $step->target->package],
1 ✔
123
                                default => [],
1 ✔
124
                        },
125
                        ...$step->skip ? ['skip' => true] : [],
1 ✔
126
                ], $plan->steps);
1 ✔
127
                $data = array_filter([
1 ✔
128
                        'version' => self::Version,
1 ✔
129
                        'toolVersion' => $plan->toolVersion,
1 ✔
130
                        'dataVersions' => $plan->dataVersions,
1 ✔
131
                        'targets' => Target::collectVersions($plan->targets),
1 ✔
132
                        'paths' => $plan->paths,
1 ✔
133
                        'tests' => array_filter(['command' => $plan->testCommand, 'quick' => $plan->quickTestCommand]),
1 ✔
134
                        'steps' => $steps,
1 ✔
135
                ], fn($value) => $value !== []);
1 ✔
136
                $header = '# dresscode upgrade: the plan for ' . implode(', ', array_map(fn(Target $target) => $target->format(), $plan->targets))
1 ✔
137
                        . "; set `skip: true` to leave a step out, and `dresscode upgrade run` runs what is left\n\n";
1 ✔
138
                FileSystem::write("$root/" . self::Name, $header . Neon::encode($data, blockMode: true));
1 ✔
139
        }
1 ✔
140

141

142
        /**
143
         * @param  list<Target>  $targets  those of the plan, one of which a manual step makes way for
144
         * @param  \Closure(string): never  $fail
145
         */
146
        private static function readStep(mixed $step, string $at, array $targets, \Closure $fail): Step
147
        {
148
                $kind = is_array($step) ? $step['kind'] ?? null : null;
1 ✔
149
                if (!is_string($kind) || !isset(self::StepKeys[$kind])) {
1 ✔
150
                        $fail("`$at` must give its `kind`, `fix`, `composer` or `manual`");
1 ✔
151
                } elseif (($unknown = array_diff(array_keys($step), ['title', 'kind', 'skip', ...self::StepKeys[$kind]])) !== []) {
1 ✔
152
                        $fail("`$at." . reset($unknown) . "` is no key of a step of the kind `$kind`");
1 ✔
153
                }
154

155
                $title = self::readString($step['title'] ?? $kind, "$at.title", $fail);
1 ✔
156
                $skip = self::readBool($step['skip'] ?? false, "$at.skip", $fail);
1 ✔
157
                return match ($kind) {
1 ✔
158
                        'fix' => new FixStep(
1 ✔
159
                                $title,
1 ✔
160
                                self::readList($step['topics'] ?? [], "$at.topics", $fail) ?: $fail("`$at.topics` must name the topics the step fixes"),
1 ✔
161
                                $skip,
162
                        ),
163
                        'composer' => new ComposerStep(
1 ✔
164
                                $title,
1 ✔
165
                                self::readConstraints($step['require'] ?? [], "$at.require", $fail),
1 ✔
166
                                self::readBool($step['update'] ?? true, "$at.update", $fail),
1 ✔
167
                                $skip,
168
                        ),
169
                        default => new ManualStep(
1 ✔
170
                                $title,
1 ✔
171
                                array_find($targets, fn(Target $target) => $target->package === ($step['target'] ?? null))
1 ✔
172
                                ?? $fail("`$at.target` must name a package of `targets`"),
1 ✔
173
                                $skip,
174
                        ),
175
                };
176
        }
177

178

179
        /**
180
         * @param  \Closure(string): never  $fail
181
         * @return non-empty-array<string, string>  package => constraint
182
         */
183
        private static function readConstraints(mixed $value, string $at, \Closure $fail): array
184
        {
185
                $require = self::readMap($value, $at, $fail) ?: $fail("`$at` must give the constraints the step writes");
1 ✔
186
                foreach ($require as $package => $constraint) {
1 ✔
187
                        if (!Target::isPackageName($package)) {
1 ✔
188
                                $fail("`$at.$package` must be `php` or a package as `vendor/name`");
1 ✔
189
                        } elseif (!Versions::isConstraint($constraint)) {
1 ✔
190
                                $fail("`$at.$package` must be a constraint Composer reads, as `^8.4`");
1 ✔
191
                        }
192
                }
193

194
                return $require;
1 ✔
195
        }
196

197

198
        /**
199
         * @param  \Closure(string): never  $fail
200
         * @return list<string>
201
         */
202
        private static function readList(mixed $value, string $at, \Closure $fail): array
203
        {
204
                return is_array($value) && array_is_list($value) && array_all($value, fn($item) => is_string($item))
1 ✔
205
                        ? $value
1 ✔
206
                        : $fail("`$at` must be a list of strings");
1 ✔
207
        }
208

209

210
        /**
211
         * @param  \Closure(string): never  $fail
212
         * @return array<string, string>
213
         */
214
        private static function readMap(mixed $value, string $at, \Closure $fail): array
215
        {
216
                return is_array($value) && array_all($value, fn($item, $key) => is_string($key) && is_string($item))
1 ✔
217
                        ? $value
1 ✔
218
                        : $fail("`$at` must be a map of strings, a version quoted as `'8.4'`");
1 ✔
219
        }
220

221

222
        /** @param  \Closure(string): never  $fail */
223
        private static function readString(mixed $value, string $at, \Closure $fail): string
224
        {
225
                return is_string($value) ? $value : $fail("`$at` must be a string");
1 ✔
226
        }
227

228

229
        /** @param  \Closure(string): never  $fail */
230
        private static function readBool(mixed $value, string $at, \Closure $fail): bool
231
        {
232
                return is_bool($value) ? $value : $fail("`$at` must be `true` or `false`");
1 ✔
233
        }
234
}
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