• 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

92.37
/src/Console/InitPrinter.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\Console;
9

10
use DressCode\Config\{PhpVersionSource, ResolvedConfig, Versions};
11
use DressCode\Interop\{Formatter, FormatterConfig};
12
use DressCode\Measuring\{LeftOutReason, Project, Proposal, ProposedValue, Question, Scope, StandardReason, ValueOrigin};
13
use Nette\CommandLine\Console;
14
use function count, is_array, sprintf;
15

16

17
/**
18
 * Writes what `dresscode init` finds, phase by phase, in the words of its consequences: the project and what is left
19
 * out of it, the version of PHP, the standard and what the code departs from it in, what a fix would change, and what
20
 * to run next.
21
 * @internal
22
 */
23
final readonly class InitPrinter
24
{
25
        /** the entries left out the output names one by one, the rest only counted */
26
        private const ListedLeftOut = 5;
27

28
        /** the values taken from the code the output names one by one */
29
        private const ListedValues = 8;
30

31

32
        public function __construct(
33
                private Console $console,
1 ✔
34
        ) {
35
        }
1 ✔
36

37

38
        /** The project, what of it is left out and why, the formatter it has, and the version of PHP its code is fixed for. */
39
        public function printScope(Project $project, Scope $scope): string
40
        {
41
                $out = $this->label('Project') . sprintf(
1 ✔
42
                        "%d PHP files %s%s\n",
43
                        count($scope->files),
1 ✔
44
                        $scope->isTracked() ? 'in git' : 'in ' . implode(', ', $scope->listPaths()),
1 ✔
45
                        $project->name === null ? '' : ", $project->name",
1 ✔
46
                );
47
                $out .= $this->printLeftOut(array_filter($scope->listExcludePaths(), fn(string $why) => $why !== LeftOutReason::Unparsed->value));
1 ✔
48
                if ($config = $project->formatterConfig) {
1 ✔
49
                        $out .= $this->label('Formatter')
1 ✔
50
                                . "{$config->formatter->value}, `$config->file`"
1 ✔
51
                                . ($config->runBy === null ? '' : ", run by `$config->runBy`")
1 ✔
52
                                . "\n";
1 ✔
53
                }
54

55
                $lowest = Versions::findLowestVersion($project->phpTarget) ?? $project->phpTarget;
1 ✔
56
                return $out . $this->label('PHP') . match ($project->phpTargetSource) {
1 ✔
57
                        PhpVersionSource::Default => "$lowest, `composer.json` names none",
1 ✔
58
                        default => "$lowest and newer, from `composer.json`",
1 ✔
59
                } . (version_compare($lowest, '8.0', '<') ? '; no fix writes what PHP 8 brought' : '') . "\n";
1 ✔
60
        }
61

62

63
        /** The standard, and what the code departs from it in and what of that the file writes as the code has it. */
64
        public function printMeasurements(Proposal $proposal): string
65
        {
66
                $standard = $proposal->standard;
1 ✔
67
                $residues = $standard->residues;
1 ✔
68
                $chosen = implode(', ', $standard->presets);
1 ✔
69
                $next = array_key_first(array_diff_key($residues, [$chosen => true]));
1 ✔
70
                $out = $this->label('Standard') . "$chosen (" . $standard->describe() . ')' . match ($standard->reason) {
1 ✔
71
                        StandardReason::Given => ", as given\n",
1 ✔
72
                        StandardReason::Formatter => ', ' . Formatter::Pint->value . " formats the project in this style\n",
1 ✔
73
                        StandardReason::Successor => sprintf(
1 ✔
74
                                ", the successor of psr12; your code departs from it in %s and from psr12 in %d; `--use` names another\n",
75
                                $this->formatDecisions($residues[$chosen]),
×
76
                                $residues['psr12'],
×
77
                        ),
78
                        StandardReason::Fewest => sprintf(
1 ✔
79
                                ", the nearest to your code, which departs from it in %s and from %s in %d; `--use` names another\n",
80
                                $this->formatDecisions($residues[$chosen]),
1 ✔
81
                                $next,
82
                                $residues[(string) $next] ?? 0,
1 ✔
83
                        ),
84
                };
85
                $out .= $this->label('Measured') . 'your code departs from it in ' . $this->formatDecisions(count($standard->residue))
1 ✔
86
                        . ' over ' . $this->formatFiles(count($proposal->sample->files)) . "\n";
1 ✔
87
                $taken = array_values(array_filter($proposal->values, fn(ProposedValue $proposed) => !$proposed->isKept()));
1 ✔
88
                foreach (array_slice($taken, 0, self::ListedValues) as $i => $proposed) {
1 ✔
89
                        $out .= $this->label($i === 0 ? 'As written' : '')
1 ✔
90
                                . "$proposed->path: " . $this->formatValue($proposed->value) . "   $proposed->comment\n";
1 ✔
91
                }
92

93
                if (count($taken) > self::ListedValues) {
1 ✔
UNCOV
94
                        $out .= $this->label('') . sprintf("and %d more, each in the file\n", count($taken) - self::ListedValues);
×
95
                }
96

97
                $unchecked = count(array_filter(
1 ✔
98
                        $proposal->values,
1 ✔
99
                        fn(ProposedValue $proposed) => $proposed->isKept() && $proposed->origin !== ValueOrigin::OverBudget,
1 ✔
100
                ));
101
                return $out . ($unchecked
1 ✔
102
                        ? $this->label('Unchecked')
1 ✔
103
                                . $this->formatDecisions($unchecked)
1 ✔
104
                                . " left as your code has them (`keep`), each with the reason in the file\n"
1 ✔
105
                        : '');
1 ✔
106
        }
107

108

109
        /**
110
         * What a fix of the configuration would change over the whole scope, what it leaves, and what the budget left
111
         * unchecked; the files a check found unparsable, which the configuration leaves out.
112
         */
113
        public function printVerification(Proposal $proposal): string
114
        {
115
                $verification = $proposal->verification ?? throw new \LogicException('The proposal is not verified.');
1 ✔
116
                $out = $this->label('Checked') . sprintf(
1 ✔
117
                        "all %d files: a fix changes %s%s%s\n",
118
                        count($proposal->scope->files),
1 ✔
119
                        $this->formatFiles($verification->changedFiles),
1 ✔
120
                        $verification->alignedFiles ? ", $verification->alignedFiles of them to indent them as the rest of the code is" : '',
1 ✔
121
                        $verification->leftPlaces
1 ✔
122
                                ? sprintf(
123
                                        '; %d places in %s that no rule fixes go to the baseline',
UNCOV
124
                                        $verification->leftPlaces,
×
UNCOV
125
                                        $this->formatFiles($verification->leftFiles),
×
126
                                )
127
                                : '',
1 ✔
128
                );
129
                $dropped = count(array_filter($proposal->values, fn(ProposedValue $proposed) => $proposed->origin === ValueOrigin::OverBudget));
1 ✔
130
                if ($dropped) {
1 ✔
131
                        $out .= $this->label('') . ($dropped === 1
1 ✔
132
                                ? '1 more decision is left as your code has it, as its fix would change'
1 ✔
133
                                : "$dropped more decisions are left as your code has them, as a fix of each would change")
1 ✔
134
                                . " more than {$this->formatFiles($verification->budget)} (`--budget` raises the limit)\n";
1 ✔
135
                }
136

137
                return $out . $this->printLeftOut(array_filter(
1 ✔
138
                        $proposal->scope->listExcludePaths(),
1 ✔
139
                        fn(string $why) => $why === LeftOutReason::Unparsed->value,
1 ✔
140
                ));
141
        }
142

143

144
        /** The commands to run next: a fix where it changes something, then the baseline where something remains. */
145
        public function printNext(Proposal $proposal): string
146
        {
147
                $verification = $proposal->verification ?? throw new \LogicException('The proposal is not verified.');
1 ✔
148
                [$changed, $left] = [$verification->changedFiles, $verification->leftPlaces];
1 ✔
149
                $out = $this->label('Written') . "`dresscode.neon`\n" . $this->label('Next') . match (true) {
1 ✔
150
                        $changed > 0 => "`dresscode fix`        rewrites {$this->formatFiles($changed)} in place (`--diff` shows the changes first); until then `check` reports them\n",
1 ✔
151
                        $left > 0 => "`dresscode baseline`   records the $left places that no rule fixes, so that `check` passes\n",
1 ✔
152
                        default => "`dresscode check`      passes already\n",
1 ✔
153
                };
154
                return $out . ($changed > 0 && $left > 0
1 ✔
UNCOV
155
                        ? $this->label('Then')
×
UNCOV
156
                                . "`dresscode baseline`   records the $left places in {$this->formatFiles($verification->leftFiles)} that no rule fixes, so that `check` passes\n"
×
157
                        : '');
1 ✔
158
        }
159

160

161
        /** A question asked in a terminal: what it asks and a place of the code it is about; the answers are the prompt's. */
162
        public function printQuestion(Question $question): string
163
        {
164
                return $question->text . "\n" . ($question->example === null ? '' : $this->label('') . "e.g. $question->example\n");
1 ✔
165
        }
166

167

168
        /**
169
         * What is the user's to decide where nobody was asked: each question as the line or the command that answers it
170
         * otherwise than the recommended answer init took.
171
         * @param  list<Question>  $questions
172
         */
173
        public function printQuestions(array $questions): string
174
        {
175
                $out = '';
1 ✔
176
                foreach ($questions as $question) {
1 ✔
177
                        $out .= $this->label($out === '' ? 'Your call' : '') . $question->instruction . "\n";
1 ✔
178
                }
179

180
                return $out;
1 ✔
181
        }
182

183

184
        /** The configuration of the formatter that could not be read, so its rules are not compared with the code. */
185
        public function printUnreadable(FormatterConfig $config, string $error): string
186
        {
UNCOV
187
                return $this->label('Formatter') . "`$config->file` could not be read, so its rules are not compared with your code: $error\n";
×
188
        }
189

190

191
        /**
192
         * What has moved since the configuration that exists was written: every decision init would write otherwise, and how
193
         * to see what its value changes.
194
         */
195
        public function printShifts(Proposal $proposal, ResolvedConfig $resolved, string $file): string
196
        {
197
                $out = '';
1 ✔
198
                foreach ($proposal->values as $path => $proposed) {
1 ✔
199
                        $value = ($resolved->decisions[$path] ?? null)?->value;
1 ✔
200
                        $measured = $proposal->standard->formatValue($path, $proposed->value);
1 ✔
201
                        if ($value?->toWrittenText() !== $measured) {
1 ✔
202
                                $cost = $value === null ? null : $proposal->estimateFiles($path, $value->toWrittenData());
1 ✔
203
                                $out .= $this->label($out === '' ? 'Moved' : '')
1 ✔
204
                                        . "$path: `$file` has " . ($value?->toWrittenText() ?? 'nothing')
1 ✔
205
                                        . ($cost ? ', a fix of it rewrites ' . ($proposal->isSampled() ? 'about ' : '') . $this->formatFiles($cost) : '')
1 ✔
206
                                        . ", init now writes $measured   ($proposed->comment)\n";
1 ✔
207
                        }
208
                }
209

210
                return ($out ?: $this->label('Moved') . "nothing: `$file` says what init measures\n")
1 ✔
211
                        . "\n`$file` exists, so nothing is written; `dresscode check --set path=value` shows what a value would change.\n";
1 ✔
212
        }
213

214

215
        /** What else the project may do: run DressCode where it runs its formatter, and rewrite the comments that silence it. */
216
        public function printAlso(Project $project, int $suppressions): string
217
        {
218
                $config = $project->formatterConfig;
1 ✔
219
                if ($config === null) {
1 ✔
220
                        return '';
1 ✔
221
                }
222

223
                $out = $this->label('Also') . "`dresscode check` can take the place of {$config->formatter->value}"
1 ✔
224
                        . ($config->runBy === null ? ' where you run it' : " in `$config->runBy`")
1 ✔
225
                        . "\n";
1 ✔
226
                return $out . ($suppressions
1 ✔
UNCOV
227
                        ? $this->label('') . "$suppressions `phpcs:ignore` comments: `dresscode migrate-suppressions` rewrites them\n"
×
228
                        : '');
1 ✔
229
        }
230

231

232
        /** @param  array<string, string>  $entries  path => why */
233
        private function printLeftOut(array $entries): string
234
        {
235
                $out = '';
1 ✔
236
                $width = $entries ? max(array_map(strlen(...), array_slice(array_keys($entries), 0, self::ListedLeftOut))) : 0;
1 ✔
237
                foreach (array_slice($entries, 0, self::ListedLeftOut, preserve_keys: true) as $path => $why) {
1 ✔
238
                        $out .= $this->label($out === '' ? 'Left out' : '') . str_pad($path, $width) . "   $why\n";
1 ✔
239
                }
240

241
                $more = count($entries) - self::ListedLeftOut;
1 ✔
242
                return $out . ($more > 0 ? $this->label('') . "and $more more, each in `excludePaths`\n" : '');
1 ✔
243
        }
244

245

246
        private function formatValue(mixed $value): string
247
        {
248
                return is_array($value) ? '[' . implode(', ', $value) . ']' : (string) $value;
1 ✔
249
        }
250

251

252
        private function formatDecisions(int $count): string
253
        {
254
                return match ($count) {
1 ✔
255
                        0 => 'no decision',
1 ✔
256
                        1 => '1 decision',
1 ✔
257
                        default => "$count decisions",
1 ✔
258
                };
259
        }
260

261

262
        private function formatFiles(int $count): string
263
        {
264
                return match ($count) {
1 ✔
265
                        0 => 'no file',
1 ✔
266
                        1 => '1 file',
1 ✔
267
                        default => "$count files",
1 ✔
268
                };
269
        }
270

271

272
        private function label(string $label): string
273
        {
274
                return $this->console->color('gray', str_pad($label, 11));
1 ✔
275
        }
276
}
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