• 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

98.8
/src/Config/ConfigResolver.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\{Config, ConfigurationException, Decision, DecisionKind, Plugin, Profile, Rule, RuleInfo, Value, Values};
11
use DressCode\Domains\Map;
12
use PhpSyntax\SymbolKind;
13
use function count, is_array, is_string;
14

15

16
/**
17
 * Lays the profiles that decide for a file one over another, each above the presets it uses, what the installed
18
 * packages say under them all, and resolves every decision and the rule owning it once into a ResolvedConfig.
19
 * @internal
20
 */
21
final class ConfigResolver
22
{
23
        /** the decision that lays the maps of the upgrading data of the packages under those of the project */
24
        private const PackagesDecision = 'upgrading.packages';
25

26
        /** @var array<string, string> */
27
        private array $warnings = [];
28

29
        /** @var array<string, Profile>  the presets by the path of their file */
30
        private array $presetFiles = [];
31

32
        /** @var list<string>  the preset files being laid, each used by the one before it */
33
        private array $using = [];
34

35
        /** @var \WeakMap<Config, array<class-string<Rule>, true>>  the rules some override of a resolved configuration turns on */
36
        private \WeakMap $rulesOfOverrides;
37

38

39
        public function __construct(
40
                private readonly PluginRegistry $registry,
1 ✔
41
                /**
42
                 * @var list<UpgradingData>  what the installed packages say, the maps of each laid under the project where it says
43
                 * `upgrading.packages: yes`
44
                 */
45
                private readonly array $upgradingData = [],
1 ✔
46
                /** the packages the project stands on, which decide whether a rule requiring one runs */
47
                private readonly ProjectPackages $project = new ProjectPackages,
1 ✔
48
                /** the run can get the types the configuration or the command line asks for; without them it resolves as if it asked for none */
49
                private readonly bool $typesAvailable = true,
1 ✔
50
                /** the directory a file in `use` of the configuration and its overrides is relative to */
51
                private readonly string $root = '.',
1 ✔
52
                /** @var list<string>  the packages whose plugins the project does not let in, so the maps of their rules are unknown */
53
                private readonly array $pluginsSkipped = [],
1 ✔
54
                /**
55
                 * @var list<string>  presets laid over those the configuration uses and beneath its own decisions and its overrides,
56
                 * so that the project keeps the last word, as `upgrade today` lays its sets
57
                 */
58
                private readonly array $laid = [],
1 ✔
59
        ) {
60
                $this->rulesOfOverrides = new \WeakMap;
1 ✔
61
        }
1 ✔
62

63

64
        /**
65
         * What the caller should tell the user about the configuration, each thing once.
66
         * @return list<string>
67
         */
68
        public function getWarnings(): array
69
        {
70
                return array_values($this->warnings);
1 ✔
71
        }
72

73

74
        /**
75
         * The configuration as data for a file the given overrides match: what every decision ends up with and where it came
76
         * from, the rules owning those that take effect, and why a rule that does not run does not.
77
         * @param  string  $phpTarget  the versions the code is written for as a Composer constraint, unless a profile says another
78
         * @param  list<int>  $overrides  indexes of the overrides that match the file
79
         * @param  ?Profile  $commandLine  laid over everything else
80
         * @param  ?list<string>  $only  decisions, sections, presets and classes of rules the run is narrowed to
81
         * @throws ConfigurationException
82
         */
83
        public function resolve(
84
                Config $config,
85
                string $phpTarget,
86
                array $overrides = [],
87
                ?Profile $commandLine = null,
88
                ?array $only = null,
89
        ): ResolvedConfig
90
        {
91
                $fixRisky = $fixRiskyPaths = $reportOnly = $severities = $use = $suppressionComments = $suppressionExamples = [];
1 ✔
92
                $symbols = [SymbolKind::Function->name => [], SymbolKind::Constant->name => []];
1 ✔
93
                $php = $resolution = null;
1 ✔
94
                $typeAnalysis = ($commandLine instanceof Config ? $commandLine->typeAnalysis : null) ?? $config->typeAnalysis;
1 ✔
95
                $decisionLayers = [];
1 ✔
96
                // what the packages declare lies under every layer, which may add to it
97
                foreach ($this->upgradingData as $package) {
1 ✔
98
                        self::addSymbols($symbols, $package->namespaces, $package->layer->describe());
1 ✔
99
                }
100

101
                ['layers' => $collected, 'repeated' => $repeated] = $this->collectLayers(self::listProfiles($config, $overrides, $commandLine));
1 ✔
102
                foreach ($collected as [$layer, $profile]) {
1 ✔
103
                        try {
104
                                if ($layer->kind === LayerKind::Preset) {
1 ✔
105
                                        $use[] = (string) $layer->name;
1 ✔
106
                                }
107

108
                                $php = $profile->targets['php'] ?? $php;
1 ✔
109
                                $resolution = $profile->nameResolution ?? $resolution;
1 ✔
110

111
                                self::addSymbols($symbols, $profile->namespaces, $layer->describe());
1 ✔
112

113
                                if ($profile->decisions !== []) {
1 ✔
114
                                        new DecisionResolver($this->getCatalogue(), translator: $this->registry->translator)->checkLayer($profile->decisions);
1 ✔
115
                                        $decisionLayers[] = [$layer, $profile->decisions];
1 ✔
116
                                }
117

118
                                foreach ($profile->fixRisky as $name) {
1 ✔
119
                                        $expanded = $this->expandName($name);
1 ✔
120
                                        $fixRisky += array_fill_keys($expanded->rules, true);
1 ✔
121
                                        $fixRiskyPaths += array_fill_keys($expanded->paths, true);
1 ✔
122
                                }
123

124
                                foreach ($profile->reportOnly as $name) {
1 ✔
125
                                        $reportOnly += array_fill_keys($this->expandName($name)->paths, true);
1 ✔
126
                                }
127

128
                                $severities[] = $this->collectSeverity($profile->severity);
1 ✔
129

130
                                foreach ($profile->suppressionComments as $pattern => $entry) {
1 ✔
131
                                        // a pattern a later layer says again is that layer's, the example with it
132
                                        $suppressionComments[$pattern] = array_values(array_unique(array_merge(
1 ✔
133
                                                ...array_map($this->resolveSuppressedName(...), $entry['decisions']),
1 ✔
134
                                        )));
135
                                        $suppressionExamples[$pattern] = $entry['example'];
1 ✔
136
                                }
137

138
                        } catch (ConfigurationException $e) {
1 ✔
139
                                throw self::locate($e, $layer);
1 ✔
140
                        }
141
                }
142

143
                $narrowed = $only ? $this->resolveOnly($only) : null;
1 ✔
144
                // an override is resolved for a file it matches, so a name or an option it gets wrong would pass unnoticed until
145
                // such a file comes; each of them is resolved together with the configuration
146
                $resolvedOverrides = $overrides === []
1 ✔
147
                        ? array_map(
1 ✔
148
                                fn(int $index) => $this->resolve($config, $phpTarget, [$index], $commandLine, $only),
1 ✔
149
                                array_keys($config->overrides),
1 ✔
150
                        )
151
                        : [];
1 ✔
152
                // the rules some override turns on, whichever file it applies to, kept for the overrides resolved later; those
153
                // resolved above go without, only what runs being read of them
154
                if ($overrides === []) {
1 ✔
155
                        $ofOverrides = [];
1 ✔
156
                        foreach ($resolvedOverrides as $resolvedOverride) {
1 ✔
157
                                foreach ($resolvedOverride->getActiveRules() as $rule) {
1 ✔
158
                                        $ofOverrides[$rule->class] = true;
1 ✔
159
                                }
160
                        }
161

162
                        $this->rulesOfOverrides[$config] = $ofOverrides;
1 ✔
163
                }
164

165
                $ofOverrides = $this->rulesOfOverrides[$config] ?? [];
1 ✔
166

167
                [$phpTarget, $phpVersion] = $this->resolvePhpTarget($php ?? $phpTarget);
1 ✔
168
                $resolver = new DecisionResolver(
1 ✔
169
                        $this->getCatalogue(),
1 ✔
170
                        $phpTarget,
171
                        $this->project,
1 ✔
172
                        typesAnalyzed: $typeAnalysis !== null && $this->typesAvailable,
1 ✔
173
                        certainNames: $resolution === 'certain',
1 ✔
174
                        translator: $this->registry->translator,
1 ✔
175
                );
176
                $decisions = $resolver->resolve($decisionLayers);
1 ✔
177
                if ($packageLayers = $this->collectPackageMaps($decisions, $narrowed)) {
1 ✔
178
                        $decisions = $resolver->resolve([...$packageLayers, ...$decisionLayers]);
1 ✔
179
                }
180

181
                $values = $resolver->createValues($decisions, $narrowed === null ? null : $this->collectSelection($narrowed));
1 ✔
182
                foreach ($this->getCatalogue()->getDecisions() as $path => $decision) {
1 ✔
183
                        // a map the run is narrowed away from is refused where its entries are wrong all the same, no rule reading it
184
                        if (
185
                                $decision->domain instanceof Map
1 ✔
186
                                && $decision->domain->grammar !== null
1 ✔
187
                                && !$values->isKept($path)
1 ✔
188
                                && !$values->isSelected($path)
1 ✔
189
                        ) {
190
                                $values->readMap($path);
1 ✔
191
                        }
192
                }
193

194
                $factories = $config->getRuleFactories() + $this->registry->ruleFactories;
1 ✔
195
                $byRule = $active = $inactive = [];
1 ✔
196
                foreach ($decisions as $path => $decision) {
1 ✔
197
                        foreach ($decision->rules as $class) {
1 ✔
198
                                $byRule[$class][$path] = $decision;
1 ✔
199
                        }
200
                }
201

202
                foreach ($this->getCatalogue()->getRuleOrder() as $class) {
1 ✔
203
                        $resolved = $this->resolveRule(
1 ✔
204
                                $class,
205
                                $byRule[$class] ?? [],
1 ✔
206
                                $resolver->findRuleReason($class),
1 ✔
207
                                $values,
208
                                $phpTarget,
209
                                $factories[$class] ?? null,
1 ✔
210
                                isset($fixRisky[$class]),
1 ✔
211
                                isset($ofOverrides[$class]),
1 ✔
212
                        );
213
                        $resolved->isActive() ? $active[$class] = $resolved : $inactive[$class] = $resolved;
1 ✔
214
                }
215

216
                $this->warnRepeated($repeated);
1 ✔
217
                if ($overrides === []) {
1 ✔
218
                        $narrowed === null
1 ✔
219
                                ? $this->checkFixPolicies($config, $active, $ofOverrides)
1 ✔
220
                                : $this->checkOnly($narrowed, $active, $inactive, $ofOverrides, $use);
1 ✔
221
                }
222

223
                // one entry per symbol, spelled the way the first layer naming it spells it
224
                $bySource = fn(array $entries): array => array_column($entries, 1, 0);
1 ✔
225
                return new ResolvedConfig(
1 ✔
226
                        [...array_values($active), ...array_values($inactive)],
1 ✔
227
                        self::resolveIndent($values->get('indentation.unit')),
1 ✔
228
                        self::resolveLineEnding($values->get('file.lineEnding')),
1 ✔
229
                        $phpVersion,
230
                        $use,
231
                        namespacedFunctions: $bySource($symbols[SymbolKind::Function->name]),
1 ✔
232
                        namespacedConstants: $bySource($symbols[SymbolKind::Constant->name]),
1 ✔
233
                        nameResolution: $resolution ?? 'uncertain',
1 ✔
234
                        lineLength: self::resolveLineLength($values->get('file.lineLength')),
1 ✔
235
                        tabWidth: $values->get('indentation.tabWidth')->getCountRange()[0],
1 ✔
236
                        typeAnalysis: $this->typesAvailable ? $typeAnalysis : null,
1 ✔
237
                        packageTargets: array_diff_key(RunnerFactory::mergeTargets($config, $commandLine), ['php' => true]),
1 ✔
238
                        plugins: array_map(
1 ✔
239
                                fn(string|Plugin $plugin) => is_string($plugin) ? $plugin : $plugin::class,
1 ✔
240
                                [...$config->plugins, ...$commandLine instanceof Config ? $commandLine->plugins : []],
1 ✔
241
                        ),
242
                        suppressionComments: $suppressionComments,
243
                        suppressionExamples: array_filter($suppressionExamples),
1 ✔
244
                        decisions: $decisions,
245
                        values: $values,
246
                        fixRisky: $fixRiskyPaths,
247
                        reportOnly: $reportOnly,
248
                        warningDecisions: self::findWarningDecisions($decisions, array_column($collected, 0), $severities),
1 ✔
249
                        overrides: $resolvedOverrides,
250
                );
251
        }
252

253

254
        /**
255
         * The decisions each of the standards makes, resolved alone.
256
         * @return array<string, array<string, ResolvedDecision>>  standard => path => its decision
257
         * @throws ConfigurationException
258
         */
259
        public function resolveStandards(string $phpTarget): array
260
        {
261
                $standards = [];
1 ✔
262
                foreach (CorePlugin::Standards as $standard) {
1 ✔
263
                        $standards[$standard] = $this->resolve(new Config(use: ["dresscode/$standard"]), $phpTarget)->decisions;
1 ✔
264
                }
265

266
                return $standards;
1 ✔
267
        }
268

269

270
        /**
271
         * The catalogue of the registered rules, as the registry keeps it.
272
         * @throws ConfigurationException
273
         */
274
        public function getCatalogue(): Catalogue
275
        {
276
                return $this->registry->getCatalogue();
1 ✔
277
        }
278

279

280
        /**
281
         * The maps of the upgrading files of the installed packages, and the requirements of the rules of their plugins they
282
         * turn on, as layers under every other one, where `upgrading.packages` is `yes`, or where the requirement laying a
283
         * file is not `keep` (`upgrading.php.deprecatedApi` for the data of PHP). A run narrowed by `only` gets the maps of
284
         * a package or a library it names, and those of the others only where another of its names covers the map, or the
285
         * requirement laying them.
286
         * @param  array<string, ResolvedDecision>  $decisions
287
         * @param  ?list<ExpandedName>  $narrowed
288
         * @return list<array{Layer, array<string, mixed>}>
289
         * @throws ConfigurationException  where a file turns on a rule another plugin or the core brings
290
         */
291
        private function collectPackageMaps(array $decisions, ?array $narrowed): array
292
        {
293
                $named = $covered = [];
1 ✔
294
                foreach ($narrowed ?? [] as $expanded) {
1 ✔
295
                        $expanded->package ? $named[$expanded->name] = true : $covered += array_fill_keys($expanded->paths, true);
1 ✔
296
                }
297

298
                $layers = [];
1 ✔
299
                foreach ($this->upgradingData as $package) {
1 ✔
300
                        $requirement = $package->layer->decision;
1 ✔
301
                        $built = $package->buildDecisions(function (string $map) use ($package, $requirement, $narrowed, $named, $covered): ?string {
1 ✔
302
                                $path = $this->findDataPath($map);
1 ✔
303
                                if ($path === null && !in_array($package->layer->package, $this->pluginsSkipped, true)) { // the plugin skipped says so
1 ✔
304
                                        $this->warnings[$package->layer->describe() . " $map"] = "Map `$map`, which "
1 ✔
305
                                                . $package->layer->formatReference()
1 ✔
306
                                                . ' sets, is not known to this DressCode; skipped.';
1 ✔
307
                                } elseif (
308
                                        $path !== null
1 ✔
309
                                        && !$this->getCatalogue()->find($path)?->domain instanceof Map
1 ✔
310
                                        && !$this->isOfPackagePlugin($path, $package->layer->package)
1 ✔
311
                                ) {
312
                                        throw new ConfigurationException(
1 ✔
313
                                                $package->layer->formatReference() . " turns on `$path`, which is no rule of the plugin of its package.",
1 ✔
314
                                        );
315
                                }
316

317
                                return $path !== null
1 ✔
318
                                        && ($narrowed === null
1 ✔
319
                                                || isset($named[$package->layer->package ?? ''])
1 ✔
320
                                                || isset($named[$package->package])
1 ✔
321
                                                || isset($covered[$path])
1 ✔
322
                                                || isset($covered[$requirement ?? '']))
1 ✔
323
                                        ? $path
1 ✔
324
                                        : null;
1 ✔
325
                        });
1 ✔
326
                        $laid = $requirement === null
1 ✔
327
                                ? ($decisions[self::PackagesDecision] ?? null)?->value->getFlag()
1 ✔
328
                                : ($decisions[$requirement] ?? null)?->value->isKept() === false;
1 ✔
329
                        if ($laid) {
1 ✔
330
                                array_push($layers, ...$built);
1 ✔
331
                        }
332
                }
333

334
                return $layers;
1 ✔
335
        }
336

337

338
        /** Whether the decision is one of the rules of the plugin the package ships. */
339
        private function isOfPackagePlugin(string $path, ?string $package): bool
340
        {
341
                $section = $package === null ? null : $this->registry->findPackageSection($package);
1 ✔
342
                return $section !== null && str_starts_with($path, "$section.");
1 ✔
343
        }
344

345

346
        /**
347
         * The decision a key of a section of an upgrading file sets: a map of `upgrading.api` by its name, a map of a plugin
348
         * or a requirement it turns on by its path.
349
         */
350
        private function findDataPath(string $key): ?string
351
        {
352
                $decision = $this->getCatalogue()->find($key);
1 ✔
353
                return match (true) {
354
                        $this->getCatalogue()->find("upgrading.api.$key")?->domain instanceof Map => "upgrading.api.$key",
1 ✔
355
                        $decision?->domain instanceof Map, $decision?->isRequirement() === true => $key,
1 ✔
356
                        default => null,
1 ✔
357
                };
358
        }
359

360

361
        /**
362
         * A rule runs where one of its requirements or facts takes effect and lies in the mask of the run; otherwise it says
363
         * why it does not. A preset may decide what cannot run here; a project deciding it asked for what it cannot get,
364
         * which is an error for the types and a warning for the target and for a package, unless another rule of the
365
         * decision runs.
366
         * @param  class-string<Rule>  $class
367
         * @param  array<string, ResolvedDecision>  $decisions  those the rule declares
368
         * @param  ?InactiveReason  $ruleReason  why the rule does not run whatever its values
369
         * @param  ?\Closure(): Rule  $factory
370
         * @throws ConfigurationException
371
         */
372
        private function resolveRule(
373
                string $class,
374
                array $decisions,
375
                ?InactiveReason $ruleReason,
376
                Values $values,
377
                string $phpTarget,
378
                ?\Closure $factory,
379
                bool $fixRisky,
380
                bool $ofOverride,
381
        ): ResolvedRule
382
        {
383
                $own = array_filter($decisions, fn(ResolvedDecision $decision) => $decision->decision->kind !== DecisionKind::Parameter);
1 ✔
384
                $effective = $ruleReason === null ? array_filter($own, fn(ResolvedDecision $decision) => $decision->inactive === null) : [];
1 ✔
385
                $asked = array_filter($own, function (ResolvedDecision $decision): bool {
1 ✔
386
                        $top = $decision->findTopLayer();
1 ✔
387
                        return $top?->origin?->isProject() === true && !$top->isKept() && $decision->inactive !== null;
1 ✔
388
                });
1 ✔
389
                // a value a group gives a decision its target leaves out is skipped silently, as one of a preset; the types never are
390
                $named = array_keys(array_filter($asked, fn(ResolvedDecision $decision) => !$decision->cascaded));
1 ✔
391
                $asked = array_keys($asked);
1 ✔
392
                $info = RuleInfo::fromRule($class);
1 ✔
393
                if ($asked !== [] && $ruleReason === InactiveReason::Types) {
1 ✔
394
                        throw new ConfigurationException($this->typesAvailable
1 ✔
395
                                ? "Decision `$asked[0]` needs the types of the code; set `typeAnalysis: phpstan` in the configuration and install `phpstan/phpstan` beside DressCode."
1 ✔
396
                                : "Decision `$asked[0]` needs the types of the code, but `phpstan/phpstan` is not installed beside DressCode.", docs: 'types#enable');
1 ✔
397
                } elseif ($named !== [] && $ruleReason === InactiveReason::Php) {
1 ✔
398
                        $this->warnings[$class] = "Decision `$named[0]` needs PHP {$info->requires['php']} and the target is $phpTarget; skipped.";
1 ✔
399
                } elseif ($named !== [] && $ruleReason === InactiveReason::Package) {
1 ✔
400
                        $this->warnings[$class] = "Decision `$named[0]` needs "
1 ✔
401
                                . ($this->project->findUnmetRequirement($info->getRequiredPackages()) ?? throw new \LogicException)->format()
1 ✔
402
                                . '; skipped.';
1 ✔
403
                }
404

405
                [$reason, $message] = match (true) {
1 ✔
406
                        $effective !== []
407
                                && array_any($effective, fn(ResolvedDecision $decision) => $values->isSelected($decision->decision->path)) => [
1 ✔
408
                                        null,
1 ✔
409
                                        null,
410
                                ],
411
                        $effective !== [] => [InactiveReason::Narrowed, 'the run is narrowed to other decisions'],
1 ✔
412
                        $ruleReason === InactiveReason::Php => [
1 ✔
413
                                InactiveReason::Php,
1 ✔
414
                                "it needs PHP {$info->requires['php']} and the target is $phpTarget",
1 ✔
415
                        ],
416
                        $ruleReason === InactiveReason::Package => [InactiveReason::Package, 'it needs a package the project does not have'],
1 ✔
417
                        $ruleReason === InactiveReason::Types
1 ✔
418
                                && array_any(
1 ✔
419
                                        $own,
420
                                        fn(ResolvedDecision $decision) => $decision->decision->isRequirement() && !$decision->value->isKept(),
1 ✔
421
                                ) => [InactiveReason::Types, $this->typesAvailable
1 ✔
422
                                        ? 'it needs the types of the code and the configuration sets no types'
1 ✔
423
                                        : 'it needs the types of the code and phpstan/phpstan is not installed beside DressCode'],
1 ✔
424
                        array_any($own, fn(ResolvedDecision $decision) => $decision->layers !== []) => [
1 ✔
425
                                InactiveReason::TurnedOff,
1 ✔
426
                                'its decisions are `keep`',
1 ✔
427
                        ],
428
                        $ofOverride => [InactiveReason::OnlyOverride, 'only an override turns it on'],
1 ✔
429
                        default => [InactiveReason::NotMentioned, 'no preset or layer of the configuration names its decisions'],
1 ✔
430
                };
431
                // who said the decisive value: of a decision that takes effect, or of the last one turned off
432
                $said = $reason === null ? $effective : array_filter($own, fn(ResolvedDecision $decision) => $decision->layers !== []);
1 ✔
433
                $top = array_values($said)[0]->layers ?? [];
1 ✔
434
                $source = $top === [] ? null : $top[count($top) - 1]->origin;
1 ✔
435
                return new ResolvedRule($class, $source, $reason, $message, $factory, $fixRisky);
1 ✔
436
        }
437

438

439
        /**
440
         * The severity a layer says of each requirement its names cover, the most specific name winning: a decision, a
441
         * deeper group, a shallower one, a section, the class of a rule, a preset.
442
         * @param  array<string, string>  $names  name => `warning` or `error`
443
         * @return array<string, array{int, string}>  path => the rank of the name and the severity
444
         * @throws ConfigurationException
445
         */
446
        private function collectSeverity(array $names): array
447
        {
448
                $severity = $by = [];
1 ✔
449
                foreach ($names as $name => $value) {
1 ✔
450
                        $expanded = $this->expandName($name);
1 ✔
451
                        $paths = array_filter(
1 ✔
452
                                $expanded->paths,
1 ✔
453
                                fn(string $path) => $this->getCatalogue()->find($path)?->kind !== DecisionKind::Parameter,
1 ✔
454
                        );
455
                        if ($paths === []) {
1 ✔
456
                                throw new ConfigurationException("Name `$name` in `severity` covers no decision that reports; a parameter has no severity of its own.");
1 ✔
457
                        }
458

459
                        $rank = match (true) {
1 ✔
460
                                $expanded->preset => 0,
1 ✔
461
                                $expanded->rule !== null => 1,
1 ✔
462
                                $this->getCatalogue()->find($name) !== null => PHP_INT_MAX,
1 ✔
463
                                default => 1 + count(explode('.', $expanded->name)),
1 ✔
464
                        };
465
                        foreach ($paths as $path) {
1 ✔
466
                                [$was, $wasValue] = $severity[$path] ?? [-1, null];
1 ✔
467
                                if ($was === $rank && $wasValue !== $value) {
1 ✔
468
                                        throw new ConfigurationException("Names `{$by[$path]}` and `$name` in `severity` say two severities of `$path`; name it alone.");
1 ✔
469
                                } elseif ($was < $rank) {
1 ✔
470
                                        $severity[$path] = [$rank, $value];
1 ✔
471
                                        $by[$path] = $name;
1 ✔
472
                                }
473
                        }
474
                }
475

476
                return $severity;
1 ✔
477
        }
478

479

480
        /**
481
         * The requirements whose violations only warn: a severity holds for a decision whose value the layer saying it, or
482
         * one below it, said, so a layer writing the value anew without a severity makes it an error again.
483
         * @param  array<string, ResolvedDecision>  $decisions
484
         * @param  list<Layer>  $layers  from the lowest
485
         * @param  list<array<string, array{int, string}>>  $severities  of each layer, by path
486
         * @return array<string, true>
487
         */
488
        private static function findWarningDecisions(array $decisions, array $layers, array $severities): array
489
        {
490
                $warnings = [];
1 ✔
491
                foreach ($decisions as $path => $decision) {
1 ✔
492
                        $top = $decision->findTopLayer()?->origin;
1 ✔
493
                        $from = $top === null ? 0 : max(0, (int) array_search($top, $layers, strict: true));
1 ✔
494
                        for ($i = count($layers) - 1; $i >= $from; $i--) {
1 ✔
495
                                if (isset($severities[$i][$path])) {
1 ✔
496
                                        if ($severities[$i][$path][1] === 'warning') {
1 ✔
497
                                                $warnings[$path] = true;
1 ✔
498
                                        }
499

500
                                        break;
1 ✔
501
                                }
502
                        }
503
                }
504

505
                return $warnings;
1 ✔
506
        }
507

508

509
        /**
510
         * The paths the decisions of the run are narrowed to, those the names of `only` stand for.
511
         * @param  list<ExpandedName>  $narrowed
512
         * @return list<string>
513
         */
514
        private function collectSelection(array $narrowed): array
515
        {
516
                return array_values(array_unique(array_merge(...array_map(fn(ExpandedName $name) => $name->paths, $narrowed))));
1 ✔
517
        }
518

519

520
        /**
521
         * The decisions a name of `suppressionComments` silences.
522
         * @return list<string>
523
         * @throws ConfigurationException
524
         */
525
        private function resolveSuppressedName(string $name): array
526
        {
527
                return $this->registry->expandSuppressedName($name)
1 ✔
528
                        ?: throw new ConfigurationException("`suppressionComments` names `$name`, which is no decision, section or rule.");
1 ✔
529
        }
530

531

532
        /**
533
         * Adds the functions and the constants the namespaces of a layer declare, each spelled the way the first layer
534
         * naming it spells it.
535
         * @param  array<string, array<string, array{string, string}>>  $symbols
536
         * @param  array{functions: list<string>, constants: list<string>}  $namespaces
537
         */
538
        private static function addSymbols(array &$symbols, array $namespaces, string $layer): void
539
        {
540
                foreach ([[SymbolKind::Function, 'functions'], [SymbolKind::Constant, 'constants']] as [$kind, $key]) {
1 ✔
541
                        foreach ($namespaces[$key] as $name) {
1 ✔
542
                                $symbols[$kind->name][self::toSymbolKey($kind, $name)] ??= [$name, $layer];
1 ✔
543
                        }
544
                }
545
        }
1 ✔
546

547

548
        /**
549
         * The key two spellings of one symbol share, the way PHP reads them: a function in any letter case, a constant
550
         * in any letter case of its namespace but not of its own name.
551
         */
552
        public static function toSymbolKey(SymbolKind $kind, string $name): string
553
        {
554
                $name = ltrim($name, '\\');
1 ✔
555
                $pos = (int) strrpos($name, '\\');
1 ✔
556
                return $kind === SymbolKind::Constant
1 ✔
557
                        ? strtolower(substr($name, 0, $pos)) . substr($name, $pos)
1 ✔
558
                        : strtolower($name);
1 ✔
559
        }
560

561

562
        /**
563
         * The versions the code is checked for and the lowest of them, the one a rule asks about. A target older than the
564
         * version the rules are tested from stays as it is, with a warning, the rules of what came later leaving its code
565
         * alone; one without a lowest version is checked as that version.
566
         * @return array{string, string}
567
         */
568
        private function resolvePhpTarget(string $target): array
569
        {
570
                $version = Versions::findLowestVersion($target);
1 ✔
571
                if ($version === null) {
1 ✔
UNCOV
572
                        $this->warnings['php'] = "The target PHP $target names no oldest version; the code is checked as PHP " . Config::MinPhpVersion
×
UNCOV
573
                                . ', so a fix may write syntax the target does not have.';
×
UNCOV
574
                        return [Config::MinPhpVersion, Config::MinPhpVersion];
×
575

576
                } elseif (version_compare($version, Config::MinPhpVersion, '<')) {
1 ✔
577
                        $this->warnings['php'] = "The target PHP $version is older than " . Config::MinPhpVersion
1 ✔
578
                                . ', which DressCode tests; the fixes of PHP 8 constructs stay off.';
1 ✔
579
                }
580

581
                return [$target, $version];
1 ✔
582
        }
583

584

585
        /** The characters of a level of indentation the unit names, a tab where it is `keep`. */
586
        private static function resolveIndent(Value $unit): string
587
        {
588
                return $unit->isKept() || $unit->getWord() === 'tab' ? "\t" : str_repeat(' ', (int) $unit->getWord());
1 ✔
589
        }
590

591

592
        /** The line ending the decision names; null for the one each file uses most. */
593
        private static function resolveLineEnding(Value $lineEnding): ?string
594
        {
595
                return match ($lineEnding->isKept() ? null : $lineEnding->getWord()) {
1 ✔
596
                        'LF' => "\n",
1 ✔
597
                        'CRLF' => "\r\n",
1 ✔
598
                        default => null,
1 ✔
599
                };
600
        }
601

602

603
        /** The widest line, null for none. */
604
        private static function resolveLineLength(Value $length): ?int
605
        {
606
                return $length->content === 'none' ? null : $length->getCountRange()[0];
1 ✔
607
        }
608

609

610
        /**
611
         * The profiles that decide for a file matching the overrides, lowest first, each with the name an error or an origin
612
         * is told by.
613
         * @param  list<int>  $overrides
614
         * @return list<array{Layer, Profile}>
615
         */
616
        private static function listProfiles(Config $config, array $overrides, ?Profile $commandLine = null): array
617
        {
618
                $profiles = [[new Layer(LayerKind::Configuration), $config]];
1 ✔
619
                foreach ($overrides as $index) {
1 ✔
620
                        $profiles[] = [
1 ✔
621
                                new Layer(LayerKind::Override, implode(', ', $config->overrides[$index]->paths)),
1 ✔
622
                                $config->overrides[$index]->profile,
1 ✔
623
                        ];
624
                }
625

626
                if ($commandLine !== null) {
1 ✔
627
                        $profiles[] = [new Layer(LayerKind::CommandLine), $commandLine];
1 ✔
628
                }
629

630
                return $profiles;
1 ✔
631
        }
632

633

634
        /**
635
         * The profiles as layers, lowest first: each of them above what it uses, in the order it names them, and a preset
636
         * where it is named first; what a profile names that is already laid, it names for nothing.
637
         * @param  list<array{Layer, Profile}>  $profiles  who says it and what it says
638
         * @return array{layers: list<array{Layer, Profile}>, repeated: list<array{Layer, Layer, Layer}>}  the layers, each
639
         *   who says it and what it says; and every entry named for nothing, as its layer, the one that laid it and the one
640
         *   that named it again
641
         * @throws ConfigurationException
642
         */
643
        private function collectLayers(array $profiles): array
644
        {
645
                $layers = $visited = $repeated = [];
1 ✔
646
                foreach ($profiles as [$layer, $profile]) {
1 ✔
647
                        $this->collectUsed($profile, $layer, $layers, $visited, $repeated);
1 ✔
648
                        if ($layer->kind === LayerKind::Configuration && $this->laid !== []) {
1 ✔
649
                                $this->collectUsed(new Profile(use: $this->laid), new Layer(LayerKind::Caller), $layers, $visited, $repeated);
1 ✔
650
                        }
651

652
                        $layers[] = [$layer, $profile];
1 ✔
653
                }
654

655
                return ['layers' => $layers, 'repeated' => $repeated];
1 ✔
656
        }
657

658

659
        /**
660
         * Lays the presets the profile uses, each above what it uses in turn, once, where it is named first; a preset
661
         * that comes back to itself through what it uses is an error, unlike one named again by another layer, which
662
         * does nothing.
663
         * @param  list<array{Layer, Profile}>  $layers
664
         * @param  array<string, Layer>  $visited  the name of a preset => the layer that laid it
665
         * @param  list<array{Layer, Layer, Layer}>  $repeated
666
         * @throws ConfigurationException
667
         */
668
        private function collectUsed(Profile $profile, Layer $source, array &$layers, array &$visited, array &$repeated): void
669
        {
670
                foreach ($profile->use as $entry) {
1 ✔
671
                        if ($this->registry->isPluginPackage($entry)) { // it lets the plugin of the package in, which is no layer
1 ✔
672
                                continue;
1 ✔
673
                        }
674

675
                        try {
676
                                // a preset by its name, or a file of the same shape by its path, which is its name too
677
                                $name = str_ends_with($entry, '.neon')
1 ✔
678
                                        ? NeonReader::resolvePresetFile($entry, $this->root)
1 ✔
679
                                        : $this->registry->resolvePreset($entry);
1 ✔
680
                                $file = $this->registry->presets[$name] ?? $name;
1 ✔
681
                        } catch (ConfigurationException $e) {
1 ✔
682
                                throw self::locate($e, $source);
1 ✔
683
                        } catch (\InvalidArgumentException $e) {
1 ✔
684
                                throw self::locate(new ConfigurationException($e->getMessage(), previous: $e), $source);
1 ✔
685
                        }
686

687
                        $layer = new Layer(LayerKind::Preset, $name);
1 ✔
688
                        if (in_array($name, $this->using, true)) {
1 ✔
689
                                $cycle = implode('`, `', array_slice($this->using, array_search($name, $this->using, strict: true) + 1));
1 ✔
690
                                throw new ConfigurationException(($file === $name ? 'Preset file' : 'Preset') . " `$name` uses itself through `$cycle`; a preset cannot come back to itself.");
1 ✔
691
                        } elseif (isset($visited[$name])) {
1 ✔
692
                                $repeated[] = [$layer, $visited[$name], $source];
1 ✔
693
                                continue;
1 ✔
694
                        }
695

696
                        $visited[$name] = $source;
1 ✔
697
                        $preset = $this->presetFiles[$file] ??= NeonReader::readPreset($file, $file === $name ? null : $name);
1 ✔
698
                        $this->using[] = $name;
1 ✔
699
                        try {
700
                                $this->collectUsed($preset, $layer, $layers, $visited, $repeated);
1 ✔
701
                        } finally {
1 ✔
702
                                array_pop($this->using);
1 ✔
703
                        }
704

705
                        $layers[] = [$layer, $preset];
1 ✔
706
                }
707
        }
1 ✔
708

709

710
        /**
711
         * An entry named again where a layer below already laid it does nothing, and the run says so.
712
         * @param  list<array{Layer, Layer, Layer}>  $repeated
713
         */
714
        private function warnRepeated(array $repeated): void
715
        {
716
                foreach ($repeated as [$layer, $laidBy, $namedBy]) {
1 ✔
717
                        // what a preset or the caller lays is not the project's to change, and the command line reaches what an override
718
                        // does not
719
                        if (
720
                                $namedBy->kind !== LayerKind::Preset
1 ✔
721
                                && $namedBy->kind !== LayerKind::Caller
1 ✔
722
                                && $laidBy->kind !== LayerKind::Caller
1 ✔
723
                                && !($laidBy->kind === LayerKind::Override && $namedBy->kind === LayerKind::CommandLine)
1 ✔
724
                        ) {
725
                                $this->warnings['repeated ' . $layer->describe() . ' ' . $namedBy->describe()] = ucfirst($namedBy->formatReference())
1 ✔
726
                                        . ' uses ' . $layer->formatReference()
1 ✔
727
                                        . ', which ' . $laidBy->formatReference() . ' already brings; the entry does nothing.';
1 ✔
728
                        }
729
                }
730
        }
1 ✔
731

732

733
        /** An error said in a layer the reader has to be sent to, named in front of it; the configuration needs no pointing at. */
734
        private static function locate(ConfigurationException $e, Layer $layer): ConfigurationException
735
        {
736
                return $layer->kind === LayerKind::Configuration
1 ✔
737
                        ? $e
1 ✔
738
                        : new ConfigurationException(ucfirst($layer->formatReference()) . ": {$e->getMessage()}", $e->docs, $e);
1 ✔
739
        }
740

741

742
        /**
743
         * The rules a run narrowed by `only` keeps, per name it was given.
744
         * @param  list<string>  $names
745
         * @return list<ExpandedName>
746
         * @throws ConfigurationException
747
         */
748
        private function resolveOnly(array $names): array
749
        {
750
                return array_map($this->expandName(...), $names);
1 ✔
751
        }
752

753

754
        /**
755
         * What a name of `only`, `fixRisky` or `severity` stands for: a decision or a section for the decisions under it,
756
         * a package of upgrading data for what its files set, a library for what the files about it set, both for a name of
757
         * both, a rule for its own, a preset for every decision it and its parents make, each with the rules owning them. A
758
         * section named like a preset is written `section.*`, the bare name being refused as both.
759
         * @throws ConfigurationException
760
         */
761
        public function expandName(string $name): ExpandedName
762
        {
763
                $package = $this->findDataPackage($name);
1 ✔
764
                if ($package !== null) {
1 ✔
765
                        $paths = [];
1 ✔
766
                        foreach ($this->upgradingData as $data) {
1 ✔
767
                                if ($data->layer->package === $package || $data->package === $package) {
1 ✔
768
                                        $keys = [...array_keys($data->maps), ...array_keys($data->requirements)];
1 ✔
769
                                        array_push($paths, ...array_filter(array_map($this->findDataPath(...), $keys)));
1 ✔
770
                                        // the data of PHP are reported under the requirement laying them
771
                                        if ($data->layer->decision !== null) {
1 ✔
772
                                                $paths[] = $data->layer->decision;
1 ✔
773
                                        }
774
                                }
775
                        }
776

777
                        $paths = array_values(array_unique($paths));
1 ✔
778
                        $rules = array_merge(...array_map($this->getCatalogue()->getRulesOf(...), $paths));
1 ✔
779
                        return new ExpandedName($package, false, null, array_values(array_unique($rules)), $paths, package: true);
1 ✔
780
                }
781

782
                $section = str_ends_with($name, '.*') ? substr($name, 0, -2) : null;
1 ✔
783
                $paths = array_keys($this->getCatalogue()->getDecisionsUnder($section ?? $name));
1 ✔
784
                $preset = $paths !== [] && $section === null && !str_contains($name, '.') ? $this->registry->findPreset($name) : null;
1 ✔
785
                if ($preset !== null) {
1 ✔
786
                        throw new ConfigurationException("Name `$name` is both a section of decisions and the preset `$preset`; write `$name.*` for the section or `$preset` for the preset.");
1 ✔
787
                }
788

789
                $preset = null;
1 ✔
790
                if ($paths === [] && $section !== null) {
1 ✔
791
                        throw new ConfigurationException("Unknown section `$section`.");
1 ✔
792
                } elseif ($paths === [] && in_array($name, $this->registry->rules, true)) {
1 ✔
793
                        $requirements = array_filter(
1 ✔
794
                                $this->getCatalogue()->getDecisionsOf($name),
1 ✔
795
                                fn(Decision $decision) => $decision->kind !== DecisionKind::Parameter,
1 ✔
796
                        );
797
                        return new ExpandedName($name, false, $name, [$name], array_keys($requirements));
1 ✔
798
                } elseif ($paths === [] && class_exists($name) && is_subclass_of($name, Rule::class)) {
1 ✔
799
                        throw new ConfigurationException("Rule `$name` is not registered; add it to `rules` of the configuration.");
1 ✔
800
                } elseif ($paths === []) {
1 ✔
801
                        $preset = $this->registry->findPreset($name) ?? throw $this->registry->createUnknownNameException($name);
1 ✔
802
                        foreach ($this->collectLayers([[new Layer(LayerKind::Caller), new Profile(use: [$preset])]])['layers'] as [, $profile]) {
1 ✔
803
                                array_push($paths, ...$this->collectPaths($profile->decisions));
1 ✔
804
                        }
805
                }
806

807
                $paths = array_values(array_unique($paths));
1 ✔
808
                $rules = array_merge(...array_map($this->getCatalogue()->getRulesOf(...), $paths));
1 ✔
809
                return new ExpandedName($preset ?? $section ?? $name, $preset !== null, null, array_values(array_unique($rules)), $paths);
1 ✔
810
        }
811

812

813
        /**
814
         * The full name of the installed package the name stands for, one shipping upgrading files or one they are about,
815
         * one of the core without its vendor too.
816
         */
817
        private function findDataPackage(string $name): ?string
818
        {
819
                $names = [$name, PluginRegistry::qualify($name)];
1 ✔
820
                foreach ($this->upgradingData as $data) {
1 ✔
821
                        if ($data->layer->name !== null && in_array($data->layer->package, $names, true)) {
1 ✔
822
                                return $data->layer->package;
1 ✔
823
                        } elseif (in_array($data->package, $names, true)) {
1 ✔
824
                                return $data->package;
1 ✔
825
                        }
826
                }
827

828
                return null;
1 ✔
829
        }
830

831

832
        /**
833
         * The paths of the decisions a layer makes, every one under a structure it makes as a whole.
834
         * @param  array<mixed>  $tree
835
         * @return list<string>
836
         */
837
        private function collectPaths(array $tree, string $prefix = ''): array
838
        {
839
                $paths = [];
1 ✔
840
                foreach ($tree as $key => $value) {
1 ✔
841
                        $path = $prefix . $key;
1 ✔
842
                        if ($this->getCatalogue()->find($path) !== null) {
1 ✔
843
                                $paths[] = $path;
1 ✔
844
                        } elseif (is_array($value)) {
1 ✔
845
                                array_push($paths, ...$this->collectPaths($value, "$path."));
1 ✔
846
                        } else {
847
                                array_push($paths, ...array_keys($this->getCatalogue()->getDecisionsUnder($path)));
1 ✔
848
                        }
849
                }
850

851
                return $paths;
1 ✔
852
        }
853

854

855
        /**
856
         * A name of `only` that lets in nothing that runs would make a run that checks nothing and says it is clean, and
857
         * a section or a preset of which only some rules run here would make one that checks less than it says; a rule that
858
         * runs only where an override enables it counts as running.
859
         * @param  list<ExpandedName>  $narrowed
860
         * @param  array<class-string<Rule>, ResolvedRule>  $active
861
         * @param  array<class-string<Rule>, ResolvedRule>  $inactive
862
         * @param  array<class-string<Rule>, true>  $ofOverrides
863
         * @param  list<string>  $use  the presets the run lays
864
         * @throws ConfigurationException
865
         */
866
        private function checkOnly(array $narrowed, array $active, array $inactive, array $ofOverrides, array $use): void
867
        {
868
                $skipped = [];
1 ✔
869
                foreach ($narrowed as $expanded) {
1 ✔
870
                        [$class, $rules] = [$expanded->rule, $expanded->rules];
1 ✔
871
                        $running = array_filter($rules, fn(string $rule) => isset($active[$rule]) || isset($ofOverrides[$rule]));
1 ✔
872
                        // a name whose rules the project cannot run, too new for its target or for its packages, is skipped, so that
873
                        // one command of `--only` serves projects of several targets
874
                        if (
875
                                !$running
1 ✔
876
                                && $rules !== []
1 ✔
877
                                && array_all($rules, fn(string $rule) => in_array(
1 ✔
878
                                        $inactive[$rule]->inactiveReason ?? null,
1 ✔
879
                                        [InactiveReason::Php, InactiveReason::Package],
1 ✔
880
                                        true,
1 ✔
881
                                ))
1 ✔
882
                        ) {
883
                                $skipped[$expanded->name] = [
1 ✔
884
                                        'Option `--only` names ' . ($class === null ? $expanded->format() : "rule `$class`")
1 ✔
885
                                        . ", which cannot run here: {$inactive[$rules[array_key_first($rules)]]->inactiveMessage}",
1 ✔
886
                                        $rules,
1 ✔
887
                                ];
888
                                continue;
1 ✔
889
                        }
890

891
                        if ($class !== null) {
1 ✔
892
                                if (!$running) {
1 ✔
893
                                        throw new ConfigurationException("Option `--only` names rule `$class`, which cannot run here: {$inactive[$class]->inactiveMessage}.");
1 ✔
894
                                }
895

896
                                continue;
1 ✔
897
                        }
898

899
                        // left out is what the project turned off, and where it does not use the name what the name would bring; a rule
900
                        // its standard turns off or that cannot run here, too new for the target or needing the types, is not
901
                        $collective = PluginRegistry::abbreviate($expanded->name);
1 ✔
902
                        $laid = in_array($collective, array_map(PluginRegistry::abbreviate(...), $use), true);
1 ✔
903
                        $left = array_filter($rules, fn(string $rule) => match ($inactive[$rule]->inactiveReason ?? null) {
1 ✔
904
                                InactiveReason::TurnedOff => $inactive[$rule]->source?->isProject() === true,
1 ✔
905
                                InactiveReason::NotMentioned => !$laid,
1 ✔
906
                                default => false,
1 ✔
907
                        });
1 ✔
908
                        // a preset the configuration does not use is brought by `--use`, a decision it does not make by `--set`
909
                        $hint = match (true) {
1 ✔
910
                                $expanded->preset => "`--use $collective --only $collective` runs all its rules",
1 ✔
911
                                $expanded->package
1 ✔
912
                                        => 'its upgrading files reach no version of a library the project has, or `upgrading.packages` is not `yes`',
1 ✔
913
                                $this->getCatalogue()->find($collective) !== null => "`--set $collective=<value>` decides it for the run",
1 ✔
914
                                default => '`--set <path>=<value>` decides one of them for the run',
1 ✔
915
                        };
916
                        if (!$running) {
1 ✔
917
                                throw new ConfigurationException('Option `--only` names ' . $expanded->format() . ', which has no rule that runs here' . ($laid ? '.' : "; $hint."));
1 ✔
918
                        } elseif ($left) {
1 ✔
919
                                // a rule left out is named by its requirements, which is what the user writes
920
                                $names = array_map(fn(string $path) => "`$path`", array_keys(array_filter(
1 ✔
921
                                        array_merge(...array_map(fn(string $rule) => $this->getCatalogue()->getDecisionsOf($rule), array_values($left))),
1 ✔
922
                                        fn(Decision $decision) => $decision->kind !== DecisionKind::Parameter,
1 ✔
923
                                )));
924
                                $this->warnings["only $expanded->name"] = 'Option `--only` keeps ' . count($running) . ' of the ' . (count(
1 ✔
925
                                        $running,
1 ✔
926
                                ) + count($left))
1 ✔
927
                                        . ' rules of ' . $expanded->format() . ' that may run here'
1 ✔
928
                                        . ($laid || !$expanded->preset
1 ✔
929
                                                ? ', without ' . implode(', ', array_slice($names, 0, 5)) . (count($names) > 5
1 ✔
930
                                                        ? ' and ' . (count($names) - 5) . ' more'
1 ✔
931
                                                        : '')
1 ✔
932
                                                        . ($laid ? ', which the configuration turns off.' : ", which the configuration does not decide; $hint.")
1 ✔
933
                                                : "; $hint.");
1 ✔
934
                        }
935
                }
936

937
                if ($skipped !== [] && count($skipped) === count($narrowed)) {
1 ✔
938
                        throw new ConfigurationException(reset($skipped)[0] . '.');
1 ✔
939
                }
940

941
                foreach ($skipped as $name => [$message, $rules]) {
1 ✔
942
                        // a rule whose decision the project wrote is said to be skipped already
943
                        if (!array_all($rules, fn(string $rule) => isset($this->warnings[$rule]))) {
1 ✔
944
                                $this->warnings["only $name"] = "$message; it is skipped.";
1 ✔
945
                        }
946
                }
947
        }
1 ✔
948

949

950
        /**
951
         * A name in fixRisky or reportOnly whose rules run nowhere, not even where an override turns them on, makes the entry
952
         * a line that does nothing, which is worth a warning.
953
         * @param  array<class-string<Rule>, ResolvedRule>  $active
954
         * @param  array<class-string<Rule>, true>  $ofOverrides
955
         * @throws ConfigurationException
956
         */
957
        private function checkFixPolicies(Config $config, array $active, array $ofOverrides): void
958
        {
959
                foreach (self::listProfiles($config, array_keys($config->overrides)) as [$layer, $profile]) {
1 ✔
960
                        foreach (['fixRisky' => $profile->fixRisky, 'reportOnly' => $profile->reportOnly] as $key => $entries) {
1 ✔
961
                                foreach ($entries as $entry) {
1 ✔
962
                                        try {
963
                                                $expanded = $this->expandName($entry);
1 ✔
UNCOV
964
                                        } catch (ConfigurationException $e) {
×
UNCOV
965
                                                throw self::locate($e, $layer);
×
966
                                        }
967

968
                                        if (!array_filter($expanded->rules, fn(string $class) => isset($active[$class]) || isset($ofOverrides[$class]))) {
1 ✔
969
                                                $this->warnings["$key $expanded->name"] = ucfirst($expanded->format())
1 ✔
970
                                                        . " is named in `$key` but runs nowhere; the entry does nothing.";
1 ✔
971
                                        }
972
                                }
973
                        }
974
                }
975
        }
1 ✔
976
}
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