Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LockFileChecker
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 check
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
8
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
21namespace MediaWiki\Composer;
22
23use Composer\Semver\Semver;
24use Status;
25use Wikimedia\Composer\ComposerJson;
26use Wikimedia\Composer\ComposerLock;
27
28/**
29 * Used to check whether composer-installed dependencies (no-dev) are up-to-date
30 * @since 1.42
31 */
32class LockFileChecker {
33    /** @var ComposerJson */
34    private $composerJson;
35
36    /** @var ComposerJson */
37    private $composerLock;
38
39    /**
40     * @param ComposerJson $composerJson
41     * @param ComposerLock $composerLock
42     */
43    public function __construct( ComposerJson $composerJson, ComposerLock $composerLock ) {
44        $this->composerJson = $composerJson;
45        $this->composerLock = $composerLock;
46    }
47
48    /**
49     * This method will return a {@link Status} instance,
50     * you can use {@link Status::isGood()} to simply determine that
51     * the composer-installed dependencies are up-to-date.
52     * @return Status
53     */
54    public function check(): Status {
55        $status = Status::newGood();
56        $requiredButOld = [];
57        $requiredButMissing = [];
58
59        $installed = $this->composerLock->getInstalledDependencies();
60        foreach ( $this->composerJson->getRequiredDependencies() as $name => $version ) {
61            // Not installed at all.
62            if ( !isset( $installed[$name] ) ) {
63                $requiredButMissing[] = [
64                    'name' => $name,
65                    'wantedVersion' => $version
66                ];
67                continue;
68            }
69
70            // Installed; need to check it's the right version
71            if ( !SemVer::satisfies( $installed[$name]['version'], $version ) ) {
72                $requiredButOld[] = [
73                    'name' => $name,
74                    'wantedVersion' => $version,
75                    'suppliedVersion' => $installed[$name]['version']
76                ];
77            }
78
79            // We're happy; loop to the next dependency.
80        }
81
82        if ( count( $requiredButOld ) === 0 && count( $requiredButMissing ) === 0 ) {
83            // We couldn't find any out-of-date or missing dependencies, so assume everything is ok!
84            return $status;
85        }
86
87        foreach ( $requiredButOld as [
88            "name" => $name,
89            "suppliedVersion" => $suppliedVersion,
90            "wantedVersion" => $wantedVersion
91        ] ) {
92            $status->error( 'composer-deps-outdated', $name, $suppliedVersion, $wantedVersion );
93        }
94
95        foreach ( $requiredButMissing as [
96            "name" => $name,
97            "wantedVersion" => $wantedVersion
98        ] ) {
99            $status->error( 'composer-deps-notinstalled', $name, $wantedVersion );
100        }
101
102        return $status;
103    }
104}