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

dg / dresscode / 42928610199

10 Nov 2026 11:53PM UTC coverage: 97.024% (-0.6%) from 97.596%
42928610199

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

147 existing lines in 29 files now uncovered.

30283 of 31212 relevant lines covered (97.02%)

0.97 hits per line

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

98.81
/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(
1 ✔
115
                                                $profile->decisions, $layer,
1 ✔
116
                                        );
117
                                        $decisionLayers[] = [$layer, $profile->decisions];
1 ✔
118
                                }
119

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

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

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

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

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

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

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

167
                $ofOverrides = $this->rulesOfOverrides[$config] ?? [];
1 ✔
168

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

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

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

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

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

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

255

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

268
                return $standards;
1 ✔
269
        }
270

271

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

281

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

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

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

336
                return $layers;
1 ✔
337
        }
338

339

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

347

348
        /**
349
         * 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
350
         * or a requirement it turns on by its path.
351
         */
352
        private function findDataPath(string $key): ?string
353
        {
354
                $decision = $this->getCatalogue()->find($key);
1 ✔
355
                return match (true) {
356
                        $this->getCatalogue()->find("upgrading.api.$key")?->domain instanceof Map => "upgrading.api.$key",
1 ✔
357
                        $decision?->domain instanceof Map, $decision?->isRequirement() === true => $key,
1 ✔
358
                        default => null,
1 ✔
359
                };
360
        }
361

362

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

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

440

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

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

478
                return $severity;
1 ✔
479
        }
480

481

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

502
                                        break;
1 ✔
503
                                }
504
                        }
505
                }
506

507
                return $warnings;
1 ✔
508
        }
509

510

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

521

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

533

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

549

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

563

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

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

583
                return [$target, $version];
1 ✔
584
        }
585

586

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

593

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

604

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

611

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

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

632
                return $profiles;
1 ✔
633
        }
634

635

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

654
                        $layers[] = [$layer, $profile];
1 ✔
655
                }
656

657
                return ['layers' => $layers, 'repeated' => $repeated];
1 ✔
658
        }
659

660

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

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

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

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

707
                        $layers[] = [$layer, $preset];
1 ✔
708
                }
709
        }
1 ✔
710

711

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

734

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

743

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

755

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

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

784
                $section = str_ends_with($name, '.*') ? substr($name, 0, -2) : null;
1 ✔
785
                $paths = array_keys($this->getCatalogue()->getDecisionsUnder($section ?? $name));
1 ✔
786
                $preset = $paths !== [] && $section === null && !str_contains($name, '.') ? $this->registry->findPreset($name) : null;
1 ✔
787
                if ($preset !== null) {
1 ✔
788
                        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 ✔
789
                }
790

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

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

814

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

830
                return null;
1 ✔
831
        }
832

833

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

853
                return $paths;
1 ✔
854
        }
855

856

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

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

898
                                continue;
1 ✔
899
                        }
900

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

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

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

951

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

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