FallbackVersions::getVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 11
rs 10
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PackageVersions;
6
7
use Generator;
8
use OutOfBoundsException;
9
use UnexpectedValueException;
10
use function array_key_exists;
11
use function array_merge;
12
use function basename;
13
use function file_exists;
14
use function file_get_contents;
15
use function getcwd;
16
use function iterator_to_array;
17
use function json_decode;
18
use function json_encode;
19
use function sprintf;
20
21
/**
22
 * @internal
23
 *
24
 * This is a fallback for {@see \PackageVersions\Versions::getVersion()}
25
 * Do not use this class directly: it is intended to be only used when
26
 * {@see \PackageVersions\Versions} fails to be generated, which typically
27
 * happens when running composer with `--no-scripts` flag)
28
 */
29
final class FallbackVersions
30
{
31
    public const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN';
32
33
    private function __construct()
34
    {
35
    }
36
37
    /**
38
     * @throws OutOfBoundsException If a version cannot be located.
39
     * @throws UnexpectedValueException If the composer.lock file could not be located.
40
     */
41 5
    public static function getVersion(string $packageName) : string
42
    {
43 5
        $versions = iterator_to_array(self::getVersions(self::getPackageData()));
44
45 4
        if (! array_key_exists($packageName, $versions)) {
46 1
            throw new OutOfBoundsException(
47 1
                'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
48
            );
49
        }
50
51 3
        return $versions[$packageName];
52
    }
53
54
    /**
55
     * @return mixed[]
56
     *
57
     * @throws UnexpectedValueException
58
     */
59 5
    private static function getPackageData() : array
60
    {
61
        $checkedPaths = [
62
            // The top-level project's ./vendor/composer/installed.json
63 5
            getcwd() . '/vendor/composer/installed.json',
64
            __DIR__ . '/../../../../composer/installed.json',
65
            // The top-level project's ./composer.lock
66 5
            getcwd() . '/composer.lock',
67
            __DIR__ . '/../../../../../composer.lock',
68
            // This package's composer.lock
69
            __DIR__ . '/../../composer.lock',
70
        ];
71
72 5
        $packageData = [];
73 5
        foreach ($checkedPaths as $path) {
74 5
            if (! file_exists($path)) {
75 5
                continue;
76
            }
77
78 4
            $data = json_decode(file_get_contents($path), true);
79 4
            switch (basename($path)) {
80 4
                case 'installed.json':
81
                    // composer 2.x installed.json format
82 3
                    if (isset($data['packages'])) {
83
                        $packageData[] = $data['packages'];
84
                    } else {
85
                        // composer 1.x installed.json format
86 3
                        $packageData[] = $data;
87
                    }
88
89 3
                    break;
90 3
                case 'composer.lock':
91 3
                    $packageData[] = $data['packages'] + ($data['packages-dev'] ?? []);
92 3
                    break;
93
                default:
94
                    // intentionally left blank
95
            }
96
        }
97
98 5
        if ($packageData !== []) {
99 4
            return array_merge(...$packageData);
100
        }
101
102 1
        throw new UnexpectedValueException(sprintf(
103
            'PackageVersions could not locate the `vendor/composer/installed.json` or your `composer.lock` '
104
            . 'location. This is assumed to be in %s. If you customized your composer vendor directory and ran composer '
105
            . 'installation with --no-scripts or if you deployed without the required composer files, then you are on '
106 1
            . 'your own, and we can\'t really help you. Fix your shit and cut the tooling some slack.',
107 1
            json_encode($checkedPaths)
108
        ));
109
    }
110
111
    /**
112
     * @param mixed[] $packageData
113
     *
114
     * @return Generator&string[]
115
     *
116
     * @psalm-return Generator<string, string>
117
     */
118 4
    private static function getVersions(array $packageData) : Generator
119
    {
120 4
        foreach ($packageData as $package) {
121 4
            yield $package['name'] => $package['version'] . '@' . (
122 4
                $package['source']['reference'] ?? $package['dist']['reference'] ?? ''
123
            );
124
        }
125
126 4
        yield self::ROOT_PACKAGE_NAME => self::ROOT_PACKAGE_NAME;
127 4
    }
128
}
129