-
-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathComposerJsonPackageVersionResolver.php
More file actions
158 lines (124 loc) · 4.61 KB
/
Copy pathComposerJsonPackageVersionResolver.php
File metadata and controls
158 lines (124 loc) · 4.61 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
<?php
declare(strict_types=1);
namespace Rector\Composer;
use Composer\Semver\VersionParser;
use Rector\FileSystem\JsonFileSystem;
use Rector\Skipper\FileSystem\PathNormalizer;
use UnexpectedValueException;
/**
* @see \Rector\Tests\Composer\ComposerJsonPackageVersionResolverTest
*/
final class ComposerJsonPackageVersionResolver
{
/**
* @var null|array<string, string>
*/
private ?array $packageVersionConstraints = null;
/**
* @var array<string, bool>
*/
private array $packageMultiMajorVersions = [];
/**
* @var array<string, null|string>
*/
private array $packageNamesByFilePath = [];
private readonly string $composerJsonFilePath;
public function __construct(?string $composerJsonFilePath = null)
{
$this->composerJsonFilePath = $composerJsonFilePath ?? getcwd() . '/composer.json';
}
public function hasPackageMultiMajorVersions(string $filePath): bool
{
$packageName = $this->resolvePackageName($filePath);
if ($packageName === null) {
return false;
}
return $this->packageMultiMajorVersions[$packageName] ??= $this->resolveHasPackageMultiMajorVersions(
$packageName
);
}
private function resolveHasPackageMultiMajorVersions(string $packageName): bool
{
$versionConstraint = $this->resolvePackageVersionConstraints()[$packageName] ?? null;
if ($versionConstraint === null) {
return false;
}
$versionConstraintParts = preg_split('#\s*\|\|?\s*#', $versionConstraint);
if ($versionConstraintParts === false || count($versionConstraintParts) < 2) {
return false;
}
$majorVersions = [];
$versionParser = new VersionParser();
foreach ($versionConstraintParts as $versionConstraintPart) {
if (preg_match('#\d+#', $versionConstraintPart) !== 1) {
continue;
}
try {
$constraint = $versionParser->parseConstraints($versionConstraintPart);
} catch (UnexpectedValueException) {
continue;
}
$lowerMajorVersion = $this->resolveMajorVersion($constraint->getLowerBound()->getVersion());
if ($lowerMajorVersion === null) {
continue;
}
$majorVersions[$lowerMajorVersion] = true;
}
return count($majorVersions) > 1;
}
private function resolvePackageName(string $filePath): ?string
{
if (array_key_exists($filePath, $this->packageNamesByFilePath)) {
return $this->packageNamesByFilePath[$filePath];
}
$normalizedFilePath = PathNormalizer::normalize($filePath);
$vendorPosition = strpos($normalizedFilePath, '/vendor/');
if ($vendorPosition === false) {
return $this->packageNamesByFilePath[$filePath] = null;
}
$vendorRelativePath = substr($normalizedFilePath, $vendorPosition + strlen('/vendor/'));
$pathParts = explode('/', $vendorRelativePath);
if (count($pathParts) < 2) {
return $this->packageNamesByFilePath[$filePath] = null;
}
return $this->packageNamesByFilePath[$filePath] = $pathParts[0] . '/' . $pathParts[1];
}
/**
* @return array<string, string>
*/
private function resolvePackageVersionConstraints(): array
{
if ($this->packageVersionConstraints !== null) {
return $this->packageVersionConstraints;
}
if (! file_exists($this->composerJsonFilePath)) {
return $this->packageVersionConstraints = [];
}
$composerJson = JsonFileSystem::readFilePath($this->composerJsonFilePath);
$packageVersionConstraints = [];
foreach (['require', 'require-dev'] as $requireKey) {
$requires = $composerJson[$requireKey] ?? [];
if (! is_array($requires)) {
continue;
}
foreach ($requires as $packageName => $versionConstraint) {
if (! is_string($packageName)) {
continue;
}
if (! is_string($versionConstraint)) {
continue;
}
$packageVersionConstraints[$packageName] = $versionConstraint;
}
}
return $this->packageVersionConstraints = $packageVersionConstraints;
}
private function resolveMajorVersion(string $version): ?int
{
$match = preg_match('#^(\d+)\.#', $version, $matches);
if ($match !== 1) {
return null;
}
return (int) $matches[1];
}
}