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

dg / dresscode / 42439021063

08 Nov 2026 02:24AM UTC coverage: 96.937% (-0.002%) from 96.939%
42439021063

push

github

dg
long lines: the head of a foreach too wide spreads what it iterates before what it assigns

Of two constructs on a line with no common one to spread the last spreads first, so a `foreach` iterating a long
array literal spread the destructuring after `as` and kept the array whole, `[…] as [` above `$location,`. What
a `foreach` iterates now spreads first, its measure not looking past the `as`, and what it assigns waits while the
iterated expression on its line has something to spread, spreading only where the line left, `] as [$a, $b]) {`, is
still too wide; what follows the iterated expression no longer keeps it from spreading, the destructuring spreading
next. The search for a split beginning at a token is shared, `NodeHelpers::beginsSplit()`.

b243d2e8-3e44-4152-8c16-82d9156e6db2

33 of 36 new or added lines in 1 file covered. (91.67%)

96 existing lines in 23 files now uncovered.

30032 of 30981 relevant lines covered (96.94%)

0.97 hits per line

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

99.27
/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;
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
                        is_file("$this->rootPath/composer.lock") => 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
         * The versions the lock of the project holds, a branch without a version of its own among them as null; null for a
188
         * project without a lock.
189
         * @return ?array<string, ?string>
190
         */
191
        public function readLockedVersions(): ?array
192
        {
193
                $lock = $this->rootPath === null ? null : ComposerJson::read("$this->rootPath/composer.lock");
1 ✔
194
                if ($lock === null) {
1 ✔
195
                        return null;
1 ✔
196
                }
197

198
                $versions = [];
1 ✔
199
                foreach ([...(array) ($lock['packages'] ?? []), ...(array) ($lock['packages-dev'] ?? [])] as $package) {
1 ✔
200
                        if (is_array($package) && is_string($package['name'] ?? null)) {
1 ✔
201
                                $version = is_string($package['version'] ?? null) ? ltrim($package['version'], 'v') : null;
1 ✔
202
                                $versions[$package['name']] = $version !== null && Versions::isVersion($version) ? $version : null;
1 ✔
203
                        }
204
                }
205

206
                return $versions;
1 ✔
207
        }
208

209

210
        /**
211
         * The versions of the package the code must work with, as a Composer constraint: the target, the constraint the
212
         * project requires it with, `*` for a package it does not require, or the version a required branch stands for,
213
         * `^3.1` or `3.3.9999999.9999999`; null where any version does, and for a package the project does not have, which
214
         * `has()` tells apart.
215
         */
216
        public function findConstraint(string $package): ?string
217
        {
218
                $required = $this->required[$package] ?? null;
1 ✔
219
                if ($package === PackageDiscovery::Php) {
1 ✔
220
                        return $this->php;
1 ✔
221
                } elseif (isset($this->replaced[$package])) {
1 ✔
222
                        return $this->targets[$package] ?? $required ?? $this->findConstraint($this->replaced[$package]);
1 ✔
223
                } elseif ($package === $this->rootName || !isset($this->installed[$package])) {
1 ✔
224
                        return null;
1 ✔
225
                }
226

227
                return $this->targets[$package] ?? match (true) {
1 ✔
228
                        $required === null => '*',
1 ✔
229
                        Versions::isBranch($required) => $this->installed[$package]['version'],
1 ✔
230
                        default => $required,
1 ✔
231
                };
232
        }
233

234

235
        /**
236
         * 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
237
         * any version does, and for a package the project does not have.
238
         */
239
        public function findVersion(string $package): ?string
240
        {
241
                $constraint = $this->findConstraint($package);
1 ✔
242
                return $constraint === null ? null : Versions::findLowestVersion($constraint);
1 ✔
243
        }
244

245

246
        /**
247
         * The installed packages newer than the oldest version the project supports, by as little as a patch, where a
248
         * declaration may say what the oldest version does not, a deprecation and its replacement above all.
249
         * @return array<string, array{string, string}>  package => the version installed and the lowest one supported
250
         */
251
        public function findNewerInstalled(): array
252
        {
253
                $newer = [];
1 ✔
254
                foreach ($this->installed as $package => ['version' => $installed]) {
1 ✔
255
                        $lowest = $this->findVersion($package);
1 ✔
256
                        if ($installed !== null && $lowest !== null && version_compare($lowest, $installed, '<')) {
1 ✔
257
                                $newer[$package] = [$installed, $lowest];
1 ✔
258
                        }
259
                }
260

261
                return $newer;
1 ✔
262
        }
263

264

265
        /**
266
         * The directories of the installed packages whose declarations need not hold for the oldest version the project
267
         * supports: those `findNewerInstalled()` lists and those the project supports from no particular version.
268
         * @return list<string>
269
         */
270
        public function findPathsAheadOfSupport(): array
271
        {
272
                $newer = $this->findNewerInstalled();
1 ✔
273
                $paths = [];
1 ✔
274
                foreach ($this->installed as $package => ['path' => $path]) {
1 ✔
275
                        if ($path !== null && (isset($newer[$package]) || $this->isUnbounded($package))) {
1 ✔
276
                                $paths[] = $path;
1 ✔
277
                        }
278
                }
279

280
                return $paths;
1 ✔
281
        }
282

283

284
        /**
285
         * Whether the project supports the package from no particular version, not requiring it or requiring it without
286
         * a lower bound.
287
         */
288
        public function isUnbounded(string $package): bool
289
        {
290
                $constraint = $this->findConstraint($package);
1 ✔
291
                return $constraint !== null && Versions::findLowestVersion($constraint) === null;
1 ✔
292
        }
293

294

295
        /**
296
         * The extensions of PHPStan the project has, the packages naming configuration files in `extra.phpstan.includes`, each
297
         * with those files PHPStan includes by itself, which `phpstan/extension-installer` makes it do, none without that
298
         * package or for a package it ignores.
299
         * @return list<PhpStanExtension>
300
         */
301
        public function findPhpStanExtensions(): array
302
        {
303
                $ignored = (array) ($this->rootExtra['phpstan/extension-installer']['ignore'] ?? []);
1 ✔
304
                $extensions = [];
1 ✔
305
                foreach ($this->installed as $package => $installed) {
1 ✔
306
                        ['path' => $path, 'extra' => $extra, 'reference' => $reference] = $installed;
1 ✔
307
                        $includes = $extra['phpstan']['includes'] ?? null;
1 ✔
308
                        if ($path === null || !is_array($includes)) {
1 ✔
309
                                continue;
1 ✔
310
                        }
311

312
                        $version = $installed['prettyVersion'] ?? $installed['version'] ?? '*';
1 ✔
313
                        $branch = str_starts_with($version, 'dev-') || str_ends_with($version, '-dev');
1 ✔
314
                        $extensions[] = new PhpStanExtension(
1 ✔
315
                                $package,
316
                                $branch && $reference !== null ? $version . ' ' . substr($reference, 0, 7) : $version,
1 ✔
317
                                $path,
318
                                isset($this->installed['phpstan/extension-installer']) && !in_array($package, $ignored, true)
1 ✔
319
                                        ? array_map(fn(string $file) => "$path/$file", array_values(array_filter($includes, is_string(...))))
1 ✔
320
                                        : [],
1 ✔
321
                        );
322
                }
323

324
                return $extensions;
1 ✔
325
        }
326

327

328
        /**
329
         * The first of the required packages the project does not meet; null where it meets every one.
330
         * @param  array<string, string>  $required  package => Composer constraint
331
         */
332
        public function findUnmetRequirement(array $required): ?UnmetRequirement
333
        {
334
                foreach ($required as $package => $constraint) {
1 ✔
335
                        if (!$this->has($package)) {
1 ✔
336
                                return new UnmetRequirement($package, $constraint, null);
1 ✔
337
                        }
338

339
                        $current = $this->findConstraint($package);
1 ✔
340
                        if ($current !== null && !Versions::isSubset($current, $constraint)) {
1 ✔
341
                                return new UnmetRequirement($package, $constraint, $current);
1 ✔
342
                        }
343
                }
344

345
                return null;
1 ✔
346
        }
347

348

349
        /**
350
         * What the packages contribute to the identity of a result: every installed one with the version it stands for
351
         * and the reference of the source it came from, so that a package upgraded under the same constraint invalidates
352
         * what was cached with the files it had before. A constraint the project widens or narrows is not here, because
353
         * that is answered by the rules the resolution comes to.
354
         * @return array<string, array{?string, ?string}>
355
         */
356
        public function getIdentity(): array
357
        {
358
                $identity = array_map(fn(array $package) => [$package['version'], $package['reference']], $this->installed);
1 ✔
359
                ksort($identity);
1 ✔
360
                return $identity;
1 ✔
361
        }
362

363

364
        /**
365
         * The version an installed package stands for: its normalized version, the alias of its branch for a development
366
         * branch, and null for a development branch without one.
367
         * @param  array<mixed>  $package  its entry in installed.json
368
         */
369
        private static function findInstalledVersion(array $package): ?string
370
        {
371
                $version = $package['version_normalized'] ?? null;
1 ✔
372
                if (!is_string($version)) {
1 ✔
373
                        return null;
1 ✔
374
                } elseif (!str_starts_with($version, 'dev-') && !str_starts_with($version, '9999999')) {
1 ✔
375
                        return Versions::shortenVersion($version);
1 ✔
376
                }
377

378
                // Composer reads the alias 3.3-dev of a branch as the newest of the 3.3 line
379
                $alias = $package['extra']['branch-alias'][$package['version'] ?? ''] ?? null;
1 ✔
380
                $branch = is_string($alias) && str_ends_with($alias, '-dev')
1 ✔
381
                        ? (new VersionParser)->normalizeBranch(substr($alias, 0, -4))
1 ✔
382
                        : null;
1 ✔
383
                return $branch !== null && str_ends_with($branch, '-dev') ? Versions::shortenVersion($branch) : null;
1 ✔
384
        }
385
}
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