Debian Codegraph @

includes/composer/LockFileChecker.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Composer;
22
23 use Composer\Semver\Semver;
24 use Wikimedia\Composer\ComposerJson;
25 use Wikimedia\Composer\ComposerLock;
26
27 /**
28 * Used to check whether composer-installed dependencies (no-dev) are up-to-date
29 *
30 * @internal For use by CheckComposerLockUpToDate and Installer
31 * @since 1.42
32 */
33 class LockFileChecker {
34 private ComposerJson $composerJson;
35 private ComposerLock $composerLock;
36
37 public function __construct( ComposerJson $composerJson, ComposerLock $composerLock ) {
38 $this->composerJson = $composerJson;
39 $this->composerLock = $composerLock;
40 }
41
42 /**
43 * @return string[]|null Array of error messages, or null when Composer-installed dependencies are up-to-date.
44 */
45 public function check(): ?array {
46 $errors = [];
47 $requiredButOld = [];
48 $requiredButMissing = [];
49
50 $installed = $this->composerLock->getInstalledDependencies();
51 foreach ( $this->composerJson->getRequiredDependencies() as $name => $version ) {
52 // Not installed at all.
53 if ( !isset( $installed[$name] ) ) {
54 $requiredButMissing[] = [
55 'name' => $name,
56 'wantedVersion' => $version
57 ];
58 continue;
59 }
60
61 // Installed; need to check it's the right version
62 if ( !SemVer::satisfies( $installed[$name]['version'], $version ) ) {
63 $requiredButOld[] = [
64 'name' => $name,
65 'wantedVersion' => $version,
66 'suppliedVersion' => $installed[$name]['version']
67 ];
68 }
69
70 // We're happy; loop to the next dependency.
71 }
72
73 foreach ( $requiredButOld as [
74 "name" => $name,
75 "suppliedVersion" => $suppliedVersion,
76 "wantedVersion" => $wantedVersion
77 ] ) {
78 $errors[] = "$name: $suppliedVersion installed, $wantedVersion required.";
79 }
80
81 foreach ( $requiredButMissing as [
82 "name" => $name,
83 "wantedVersion" => $wantedVersion
84 ] ) {
85 $errors[] = "$name: not installed, $wantedVersion required.";
86 }
87
88 return $errors ?: null;
89 }
90 }