-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathComposerHelper.php
More file actions
173 lines (136 loc) · 4.55 KB
/
ComposerHelper.php
File metadata and controls
173 lines (136 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php declare(strict_types = 1);
namespace PHPStan\Internal;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\File\FileReader;
use function basename;
use function getenv;
use function is_file;
use function is_string;
use function preg_match;
use function substr;
use function trim;
final class ComposerHelper
{
public const UNKNOWN_VERSION = 'Unknown version';
private static ?string $phpstanVersion = null;
private static ?string $betterReflectionVersion = null;
private static ?string $phpstormStubsVersion = null;
private static ?string $phpDocParserVersion = null;
/** @var array<string, mixed[]> */
private static array $decodedCache = [];
/** @var array<string, mixed>|null */
private static ?array $installed;
/** @return array<string, mixed>|null */
public static function getComposerConfig(string $root): ?array
{
if (isset(self::$decodedCache[$root])) {
return self::$decodedCache[$root];
}
$composerJsonPath = self::getComposerJsonPath($root);
if ($composerJsonPath === null) {
return null;
}
try {
$composerJsonContents = FileReader::read($composerJsonPath);
return self::$decodedCache[$root] ??= Json::decode($composerJsonContents, Json::FORCE_ARRAY);
} catch (CouldNotReadFileException | JsonException) {
return null;
}
}
public static function getComposerJsonPath(string $root): ?string
{
$envComposer = getenv('COMPOSER');
$fileName = is_string($envComposer) ? $envComposer : 'composer.json';
$fileName = basename(trim($fileName));
$path = $root . '/' . $fileName;
if (!is_file($path)) {
return null;
}
return $path;
}
/**
* @param array<string, mixed> $composerConfig
*/
public static function getVendorDirFromComposerConfig(string $root, array $composerConfig): string
{
$vendorDirectory = $composerConfig['config']['vendor-dir'] ?? 'vendor';
return $root . '/' . trim($vendorDirectory, '/');
}
/**
* @param array<string, mixed> $composerConfig
*/
public static function getBinDirFromComposerConfig(string $root, array $composerConfig): string
{
$vendorDirectory = $composerConfig['config']['bin-dir'] ?? 'vendor/bin';
return $root . '/' . trim($vendorDirectory, '/');
}
/**
* @return array<string, mixed>
*/
private static function getInstalled(): array
{
return self::$installed ??= require __DIR__ . '/../../vendor/composer/installed.php';
}
public static function getPhpStanVersion(): string
{
if (self::$phpstanVersion !== null) {
return self::$phpstanVersion;
}
$installed = self::getInstalled();
$rootPackage = $installed['root'] ?? null;
if ($rootPackage === null) {
return self::$phpstanVersion = self::UNKNOWN_VERSION;
}
return self::$phpstanVersion = self::processPackageVersion($rootPackage);
}
public static function getBetterReflectionVersion(): string
{
if (self::$betterReflectionVersion !== null) {
return self::$betterReflectionVersion;
}
$installed = self::getInstalled();
$rootPackage = $installed['versions']['ondrejmirtes/better-reflection'] ?? null;
if ($rootPackage === null) {
return self::$betterReflectionVersion = self::UNKNOWN_VERSION;
}
return self::$betterReflectionVersion = self::processPackageVersion($rootPackage);
}
public static function getPhpStormStubsVersion(): string
{
if (self::$phpstormStubsVersion !== null) {
return self::$phpstormStubsVersion;
}
$installed = self::getInstalled();
$rootPackage = $installed['versions']['jetbrains/phpstorm-stubs'] ?? null;
if ($rootPackage === null) {
return self::$phpstormStubsVersion = self::UNKNOWN_VERSION;
}
return self::$phpstormStubsVersion = self::processPackageVersion($rootPackage);
}
public static function getPhpDocParserVersion(): string
{
if (self::$phpDocParserVersion !== null) {
return self::$phpDocParserVersion;
}
$installed = self::getInstalled();
$rootPackage = $installed['versions']['phpstan/phpdoc-parser'] ?? null;
if ($rootPackage === null) {
return self::$phpDocParserVersion = self::UNKNOWN_VERSION;
}
return self::$phpDocParserVersion = self::processPackageVersion($rootPackage);
}
/**
* @param array<string, mixed> $package
* @return string
*/
private static function processPackageVersion(array $package): string
{
if (preg_match('/[^v\d.]/', $package['pretty_version']) === 0) {
// Handles tagged versions, see https://github.com/Jean85/pretty-package-versions/blob/2.0.5/src/Version.php#L31
return $package['pretty_version'];
}
return $package['pretty_version'] . '@' . substr((string) $package['reference'], 0, 7);
}
}