• 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

99.31
/src/Config/ProjectPackages.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 Composer\Semver\VersionParser;
11
use DressCode\Analyses\PhpStanExtension;
12
use DressCode\Engine\{Helpers, ProcessRunner};
13
use DressCode\Tristate;
14
use Nette\Utils\FileSystem;
15
use function is_array, is_string;
16

17

18
/**
19
 * The packages a project stands on, read from its composer.json and from the vendor/composer/installed.json beside it,
20
 * and the versions of each the code must work with. For a package the project requires itself those are the versions its
21
 * constraint allows, because code written for a newer one breaks wherever the constraint lets an older one in, so a
22
 * constraint without a lower bound allows every version; for a package that only comes with another, which the project
23
 * promises nothing about, it is every version too; for a development branch the project requires it is the newest of
24
 * the line its alias names; and any version does for the project itself and for a branch without an alias, whose
25
 * version says nothing. The installed version never answers, being only what happens to be in the vendor directory. A
26
 * package an installed one replaces with its own version, a part of a monorepo installed as the whole, is had in the
27
 * versions of the one replacing it. The `targets` of the configuration say the one version of an installed package
28
 * outright (`withTargets()`).
29
 * @internal
30
 */
31
final readonly class ProjectPackages
32
{
33
        public function __construct(
34
                /** the name of the root package, null where its composer.json gives none */
35
                public ?string $rootName = null,
1 ✔
36
                /** the directory of the root package, null where the project has no composer.json */
37
                public ?string $rootPath = null,
1 ✔
38
                /** @var array<mixed>  what the composer.json of the root package says under extra */
39
                public array $rootExtra = [],
1 ✔
40
                /** @var array<string, string>  package the project requires itself => its constraint */
41
                public array $required = [],
1 ✔
42
                /**
43
                 * @var array<string, array{version: ?string, reference: ?string, path: ?string, extra: array<mixed>, prettyVersion?: string}>
44
                 *   installed package => the version it stands for (null for any), the source it came from, where it lies, its extra
45
                 *   and the version as Composer writes it
46
                 */
47
                public array $installed = [],
1 ✔
48
                /** @var array<string, string>  package => the version the configuration says the code is written for */
49
                private array $targets = [],
1 ✔
50
                /** @var array<string, string>  package => the installed one that replaces it with its own version */
51
                private array $replaced = [],
1 ✔
52
                /** the versions of PHP the code must work with, null where nobody said them */
53
                private ?string $php = null,
1 ✔
54
                /** the version of PHP `config.platform.php` sets, which Composer resolves the packages for; null for none */
55
                public ?string $platformPhp = null,
1 ✔
56
        ) {
57
        }
1 ✔
58

59

60
        public static function read(string $root): self
61
        {
62
                $composerFile = ComposerJson::findFile($root);
1 ✔
63
                if ($composerFile === null) {
1 ✔
64
                        return new self;
1 ✔
65
                }
66

67
                $base = Helpers::canonicalizePath(dirname($composerFile));
1 ✔
68
                $composer = ComposerJson::read($composerFile) ?? [];
1 ✔
69
                $vendorDir = $composer['config']['vendor-dir'] ?? 'vendor';
1 ✔
70
                $vendor = Helpers::canonicalizePath(Helpers::toAbsolutePath(is_string($vendorDir) ? $vendorDir : 'vendor', $base));
1 ✔
71

72
                $required = [];
1 ✔
73
                foreach (['require', 'require-dev'] as $section) {
1 ✔
74
                        foreach (is_array($composer[$section] ?? null) ? $composer[$section] : [] as $package => $constraint) {
1 ✔
75
                                if (is_string($constraint)) {
1 ✔
76
                                        $required[(string) $package] = $constraint;
1 ✔
77
                                }
78
                        }
79
                }
80

81
                $installed = $replaced = [];
1 ✔
82
                $data = ComposerJson::read("$vendor/composer/installed.json") ?? [];
1 ✔
83
                foreach (is_array($data['packages'] ?? null) ? $data['packages'] : $data as $package) {
1 ✔
84
                        if (!is_array($package) || !is_string($package['name'] ?? null)) {
1 ✔
UNCOV
85
                                continue;
×
86
                        }
87

88
                        $reference = $package['source']['reference'] ?? $package['dist']['reference'] ?? null;
1 ✔
89
                        $installed[$package['name']] = [
1 ✔
90
                                'version' => self::findInstalledVersion($package),
1 ✔
91
                                'reference' => is_string($reference) ? $reference : null,
1 ✔
92
                                'path' => is_string($package['install-path'] ?? null)
1 ✔
93
                                        ? Helpers::canonicalizePath(FileSystem::normalizePath("$vendor/composer/{$package['install-path']}"))
1 ✔
94
                                        : null,
95
                                'extra' => is_array($package['extra'] ?? null) ? $package['extra'] : [],
1 ✔
96
                                ...is_string($package['version'] ?? null) ? ['prettyVersion' => $package['version']] : [],
1 ✔
97
                        ];
98
                        foreach (is_array($package['replace'] ?? null) ? $package['replace'] : [] as $name => $constraint) {
1 ✔
99
                                if ($constraint === 'self.version') {
1 ✔
100
                                        $replaced[(string) $name] = $package['name'];
1 ✔
101
                                }
102
                        }
103
                }
104

105
                return new self(
1 ✔
106
                        is_string($composer['name'] ?? null) ? $composer['name'] : null,
1 ✔
107
                        $base,
108
                        is_array($composer['extra'] ?? null) ? $composer['extra'] : [],
1 ✔
109
                        $required,
110
                        $installed,
111
                        replaced: array_diff_key($replaced, $installed),
1 ✔
112
                        platformPhp: is_string($platform = $composer['config']['platform']['php'] ?? null) ? $platform : null,
1 ✔
113
                );
114
        }
115

116

117
        /**
118
         * The same project with the versions of packages its code is written for said outright, which `findConstraint()`
119
         * answers with before it asks the constraint: code is fixed for a version before the project moves to it.
120
         * @param  array<string, string>  $versions  package => version
121
         */
122
        public function withTargets(array $versions): self
123
        {
124
                return new self(
1 ✔
125
                        $this->rootName, $this->rootPath, $this->rootExtra, $this->required,
1 ✔
126
                        $this->installed, $versions, $this->replaced, $this->php, $this->platformPhp,
1 ✔
127
                );
128
        }
129

130

131
        /**
132
         * The same project with the versions of PHP its code must work with, the target as a Composer constraint, a version
133
         * alone being the oldest the code runs on and standing for the later ones too.
134
         */
135
        public function withPhp(string $target): self
136
        {
137
                return new self(
1 ✔
138
                        $this->rootName,
1 ✔
139
                        $this->rootPath,
1 ✔
140
                        $this->rootExtra,
1 ✔
141
                        $this->required,
1 ✔
142
                        $this->installed,
1 ✔
143
                        $this->targets,
1 ✔
144
                        $this->replaced,
1 ✔
145
                        Versions::isVersion($target) ? ">=$target" : $target,
1 ✔
146
                        $this->platformPhp,
1 ✔
147
                );
148
        }
149

150

151
        /**
152
         * Whether the project has the package: it is the project itself, it is installed, an installed one replaces it, or
153
         * it is PHP, whose versions the project said.
154
         */
155
        public function has(string $package): bool
156
        {
157
                return $package === $this->rootName
1 ✔
158
                        || isset($this->installed[$package])
1 ✔
159
                        || isset($this->replaced[$package])
1 ✔
160
                        || ($package === PackageDiscovery::Php && $this->php !== null);
1 ✔
161
        }
162

163

164
        /**
165
         * Whether the project is a library, which says so by its `type`, a project giving none being one as Composer reads
166
         * it, and has no lock of its own; maybe for one with a lock, as an application has.
167
         */
168
        public function isLibrary(): Tristate
169
        {
170
                return match (true) {
171
                        $this->findType() !== 'library' => Tristate::No,
1 ✔
172
                        $this->hasOwnLock() => Tristate::Maybe,
1 ✔
173
                        default => Tristate::Yes,
1 ✔
174
                };
175
        }
176

177

178
        /** The type of the project, `library` where its composer.json gives none, as Composer reads it. */
179
        public function findType(): string
180
        {
181
                $type = ComposerJson::read("$this->rootPath/composer.json")['type'] ?? null;
1 ✔
182
                return is_string($type) ? $type : 'library';
1 ✔
183
        }
184

185

186
        /**
187
         * Whether the project keeps a `composer.lock` of its own: one its git ignores is what an install of a library leaves,
188
         * unless git ignores `composer.json` with it, the project then lying in a directory another repository ignores.
189
         */
190
        private function hasOwnLock(): bool
191
        {
192
                if ($this->rootPath === null || !is_file("$this->rootPath/composer.lock")) {
1 ✔
193
                        return false;
1 ✔
194
                }
195

196
                [, $ignored] = ProcessRunner::run(
1 ✔
197
                        ['git', '-C', $this->rootPath, 'check-ignore', 'composer.lock', 'composer.json'],
1 ✔
198
                        $this->rootPath,
1 ✔
199
                        timeout: 60,
1 ✔
200
                        errors: false,
1 ✔
201
                );
202
                return preg_split('~\R~', trim($ignored)) !== ['composer.lock'];
1 ✔
203
        }
204

205

206
        /**
207
         * The versions the lock of the project holds, a branch without a version of its own among them as null; null for a
208
         * project without a lock.
209
         * @return ?array<string, ?string>
210
         */
211
        public function readLockedVersions(): ?array
212
        {
213
                $lock = $this->rootPath === null ? null : ComposerJson::read("$this->rootPath/composer.lock");
1 ✔
214
                if ($lock === null) {
1 ✔
215
                        return null;
1 ✔
216
                }
217

218
                $versions = [];
1 ✔
219
                foreach ([...(array) ($lock['packages'] ?? []), ...(array) ($lock['packages-dev'] ?? [])] as $package) {
1 ✔
220
                        if (is_array($package) && is_string($package['name'] ?? null)) {
1 ✔
221
                                $version = is_string($package['version'] ?? null) ? ltrim($package['version'], 'v') : null;
1 ✔
222
                                $versions[$package['name']] = $version !== null && Versions::isVersion($version) ? $version : null;
1 ✔
223
                        }
224
                }
225

226
                return $versions;
1 ✔
227
        }
228

229

230
        /**
231
         * The versions of the package the code must work with, as a Composer constraint: the target, the constraint the
232
         * project requires it with, `*` for a package it does not require, or the version a required branch stands for,
233
         * `^3.1` or `3.3.9999999.9999999`; null where any version does, and for a package the project does not have, which
234
         * `has()` tells apart.
235
         */
236
        public function findConstraint(string $package): ?string
237
        {
238
                $required = $this->required[$package] ?? null;
1 ✔
239
                if ($package === PackageDiscovery::Php) {
1 ✔
240
                        return $this->php;
1 ✔
241
                } elseif (isset($this->replaced[$package])) {
1 ✔
242
                        return $this->targets[$package] ?? $required ?? $this->findConstraint($this->replaced[$package]);
1 ✔
243
                } elseif ($package === $this->rootName || !isset($this->installed[$package])) {
1 ✔
244
                        return null;
1 ✔
245
                }
246

247
                return $this->targets[$package] ?? match (true) {
1 ✔
248
                        $required === null => '*',
1 ✔
249
                        Versions::isBranch($required) => $this->installed[$package]['version'],
1 ✔
250
                        default => $required,
1 ✔
251
                };
252
        }
253

254

255
        /**
256
         * The lowest version of the package the code must work with, `3.1` or `3.2.1`; null where it has no lower bound, where
257
         * any version does, and for a package the project does not have.
258
         */
259
        public function findVersion(string $package): ?string
260
        {
261
                $constraint = $this->findConstraint($package);
1 ✔
262
                return $constraint === null ? null : Versions::findLowestVersion($constraint);
1 ✔
263
        }
264

265

266
        /**
267
         * The installed packages newer than the oldest version the project supports, by as little as a patch, where a
268
         * declaration may say what the oldest version does not, a deprecation and its replacement above all.
269
         * @return array<string, array{string, string}>  package => the version installed and the lowest one supported
270
         */
271
        public function findNewerInstalled(): array
272
        {
273
                $newer = [];
1 ✔
274
                foreach ($this->installed as $package => ['version' => $installed]) {
1 ✔
275
                        $lowest = $this->findVersion($package);
1 ✔
276
                        if ($installed !== null && $lowest !== null && version_compare($lowest, $installed, '<')) {
1 ✔
277
                                $newer[$package] = [$installed, $lowest];
1 ✔
278
                        }
279
                }
280

281
                return $newer;
1 ✔
282
        }
283

284

285
        /**
286
         * The directories of the installed packages whose declarations need not hold for the oldest version the project
287
         * supports: those `findNewerInstalled()` lists and those the project supports from no particular version.
288
         * @return list<string>
289
         */
290
        public function findPathsAheadOfSupport(): array
291
        {
292
                $newer = $this->findNewerInstalled();
1 ✔
293
                $paths = [];
1 ✔
294
                foreach ($this->installed as $package => ['path' => $path]) {
1 ✔
295
                        if ($path !== null && (isset($newer[$package]) || $this->isUnbounded($package))) {
1 ✔
296
                                $paths[] = $path;
1 ✔
297
                        }
298
                }
299

300
                return $paths;
1 ✔
301
        }
302

303

304
        /**
305
         * Whether the project supports the package from no particular version, not requiring it or requiring it without
306
         * a lower bound.
307
         */
308
        public function isUnbounded(string $package): bool
309
        {
310
                $constraint = $this->findConstraint($package);
1 ✔
311
                return $constraint !== null && Versions::findLowestVersion($constraint) === null;
1 ✔
312
        }
313

314

315
        /**
316
         * The extensions of PHPStan the project has, the packages naming configuration files in `extra.phpstan.includes`, each
317
         * with those files PHPStan includes by itself, which `phpstan/extension-installer` makes it do, none without that
318
         * package or for a package it ignores.
319
         * @return list<PhpStanExtension>
320
         */
321
        public function findPhpStanExtensions(): array
322
        {
323
                $ignored = (array) ($this->rootExtra['phpstan/extension-installer']['ignore'] ?? []);
1 ✔
324
                $extensions = [];
1 ✔
325
                foreach ($this->installed as $package => $installed) {
1 ✔
326
                        ['path' => $path, 'extra' => $extra, 'reference' => $reference] = $installed;
1 ✔
327
                        $includes = $extra['phpstan']['includes'] ?? null;
1 ✔
328
                        if ($path === null || !is_array($includes)) {
1 ✔
329
                                continue;
1 ✔
330
                        }
331

332
                        $version = $installed['prettyVersion'] ?? $installed['version'] ?? '*';
1 ✔
333
                        $branch = str_starts_with($version, 'dev-') || str_ends_with($version, '-dev');
1 ✔
334
                        $extensions[] = new PhpStanExtension(
1 ✔
335
                                $package,
336
                                $branch && $reference !== null ? $version . ' ' . substr($reference, 0, 7) : $version,
1 ✔
337
                                $path,
338
                                isset($this->installed['phpstan/extension-installer']) && !in_array($package, $ignored, true)
1 ✔
339
                                        ? array_map(fn(string $file) => "$path/$file", array_values(array_filter($includes, is_string(...))))
1 ✔
340
                                        : [],
1 ✔
341
                        );
342
                }
343

344
                return $extensions;
1 ✔
345
        }
346

347

348
        /**
349
         * The first of the required packages the project does not meet; null where it meets every one.
350
         * @param  array<string, string>  $required  package => Composer constraint
351
         */
352
        public function findUnmetRequirement(array $required): ?UnmetRequirement
353
        {
354
                foreach ($required as $package => $constraint) {
1 ✔
355
                        if (!$this->has($package)) {
1 ✔
356
                                return new UnmetRequirement($package, $constraint, null);
1 ✔
357
                        }
358

359
                        $current = $this->findConstraint($package);
1 ✔
360
                        if ($current !== null && !Versions::isSubset($current, $constraint)) {
1 ✔
361
                                return new UnmetRequirement($package, $constraint, $current);
1 ✔
362
                        }
363
                }
364

365
                return null;
1 ✔
366
        }
367

368

369
        /**
370
         * What the packages contribute to the identity of a result: every installed one with the version it stands for
371
         * and the reference of the source it came from, so that a package upgraded under the same constraint invalidates
372
         * what was cached with the files it had before. A constraint the project widens or narrows is not here, because
373
         * that is answered by the rules the resolution comes to.
374
         * @return array<string, array{?string, ?string}>
375
         */
376
        public function getIdentity(): array
377
        {
378
                $identity = array_map(fn(array $package) => [$package['version'], $package['reference']], $this->installed);
1 ✔
379
                ksort($identity);
1 ✔
380
                return $identity;
1 ✔
381
        }
382

383

384
        /**
385
         * The version an installed package stands for: its normalized version, the alias of its branch for a development
386
         * branch, and null for a development branch without one.
387
         * @param  array<mixed>  $package  its entry in installed.json
388
         */
389
        private static function findInstalledVersion(array $package): ?string
390
        {
391
                $version = $package['version_normalized'] ?? null;
1 ✔
392
                if (!is_string($version)) {
1 ✔
393
                        return null;
1 ✔
394
                } elseif (!str_starts_with($version, 'dev-') && !str_starts_with($version, '9999999')) {
1 ✔
395
                        return Versions::shortenVersion($version);
1 ✔
396
                }
397

398
                // Composer reads the alias 3.3-dev of a branch as the newest of the 3.3 line
399
                $alias = $package['extra']['branch-alias'][$package['version'] ?? ''] ?? null;
1 ✔
400
                $branch = is_string($alias) && str_ends_with($alias, '-dev')
1 ✔
401
                        ? (new VersionParser)->normalizeBranch(substr($alias, 0, -4))
1 ✔
402
                        : null;
1 ✔
403
                return $branch !== null && str_ends_with($branch, '-dev') ? Versions::shortenVersion($branch) : null;
1 ✔
404
        }
405
}
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