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

dg / dresscode / 41225904924

31 Oct 2026 05:33PM UTC coverage: 97.095% (-0.01%) from 97.107%
41225904924

push

github

dg
init: names no rule renames go to the baseline by the recommended answer, naming.except letting them pass now and later being the second

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

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

152 existing lines in 12 files now uncovered.

27771 of 28602 relevant lines covered (97.09%)

0.97 hits per line

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

95.24
/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 ✔
UNCOV
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 (!Versions::isVersion($version)) {
1 ✔
UNCOV
75
                                $fail("`targets.$package` must be a version, as `'8.4'`");
×
76
                        }
77

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

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

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

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

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

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

110

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

139

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

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

177

178
        /**
179
         * @param  \Closure(string): never  $fail
180
         * @return list<string>
181
         */
182
        private static function readList(mixed $value, string $at, \Closure $fail): array
183
        {
184
                return is_array($value) && array_is_list($value) && array_all($value, fn($item) => is_string($item))
1 ✔
185
                        ? $value
1 ✔
186
                        : $fail("`$at` must be a list of strings");
1 ✔
187
        }
188

189

190
        /**
191
         * @param  \Closure(string): never  $fail
192
         * @return array<string, string>
193
         */
194
        private static function readMap(mixed $value, string $at, \Closure $fail): array
195
        {
196
                return is_array($value) && array_all($value, fn($item, $key) => is_string($key) && is_string($item))
1 ✔
197
                        ? $value
1 ✔
198
                        : $fail("`$at` must be a map of strings, a version quoted as `'8.4'`");
1 ✔
199
        }
200

201

202
        /** @param  \Closure(string): never  $fail */
203
        private static function readString(mixed $value, string $at, \Closure $fail): string
204
        {
205
                return is_string($value) ? $value : $fail("`$at` must be a string");
1 ✔
206
        }
207

208

209
        /** @param  \Closure(string): never  $fail */
210
        private static function readBool(mixed $value, string $at, \Closure $fail): bool
211
        {
212
                return is_bool($value) ? $value : $fail("`$at` must be `true` or `false`");
1 ✔
213
        }
214
}
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