-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
VersionConstraintNormalizer.php
377 lines (311 loc) 路 11.3 KB
/
VersionConstraintNormalizer.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
declare(strict_types=1);
/**
* Copyright (c) 2018-2024 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/json-normalizer
*/
namespace Ergebnis\Json\Normalizer\Vendor\Composer;
use Composer\Semver;
use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Format;
use Ergebnis\Json\Normalizer\Normalizer;
final class VersionConstraintNormalizer implements Normalizer
{
private const PROPERTIES_THAT_SHOULD_BE_NORMALIZED = [
'conflict',
'provide',
'replace',
'require',
'require-dev',
];
private Semver\VersionParser $versionParser;
public function __construct(Semver\VersionParser $versionParser)
{
$this->versionParser = $versionParser;
}
public function normalize(Json $json): Json
{
$decoded = $json->decoded();
if (!\is_object($decoded)) {
return $json;
}
$objectPropertiesThatShouldBeNormalized = \array_intersect_key(
\get_object_vars($decoded),
\array_flip(self::PROPERTIES_THAT_SHOULD_BE_NORMALIZED),
);
if ([] === $objectPropertiesThatShouldBeNormalized) {
return $json;
}
foreach ($objectPropertiesThatShouldBeNormalized as $name => $value) {
$packages = (array) $value;
if ([] === $packages) {
continue;
}
$decoded->{$name} = \array_map(function (string $versionConstraint): string {
$versionConstraint = self::trim($versionConstraint);
$versionConstraint = self::removeExtraSpaces($versionConstraint);
try {
$this->versionParser->parseConstraints($versionConstraint);
} catch (\UnexpectedValueException $exception) {
return $versionConstraint;
}
return $this->normalizeVersionConstraint($versionConstraint);
}, $packages);
}
/** @var string $encoded */
$encoded = \json_encode(
$decoded,
Format\JsonEncodeOptions::default()->toInt(),
);
return Json::fromString($encoded);
}
private function normalizeVersionConstraint(string $versionConstraint): string
{
$versionConstraint = self::normalizeVersionConstraintSeparators($versionConstraint);
$versionConstraint = self::removeLeadingVersionPrefix($versionConstraint);
$versionConstraint = self::assertDevPrefixSuffixPosition($versionConstraint);
$versionConstraint = self::replaceWildcardXWithAsterisk($versionConstraint);
$versionConstraint = self::replaceWildcardWithTilde($versionConstraint);
$versionConstraint = self::replaceTildeWithCaret($versionConstraint);
$versionConstraint = self::removeDuplicateVersionConstraints($versionConstraint);
$versionConstraint = self::removeUselessInlineAliases($versionConstraint);
$versionConstraint = self::sortVersionConstraints($versionConstraint);
return self::removeOverlappingVersionConstraints($versionConstraint);
}
private static function trim(string $versionConstraint): string
{
return \trim($versionConstraint);
}
private static function removeExtraSpaces(string $versionConstraint): string
{
return \preg_replace(
'/ +/',
' ',
$versionConstraint,
);
}
private static function normalizeVersionConstraintSeparators(string $versionConstraint): string
{
$orConstraints = self::splitIntoOrConstraints($versionConstraint);
return self::joinOrConstraints(...\array_map(static function (string $orConstraint): string {
$andConstraints = self::splitIntoAndConstraints($orConstraint);
return self::joinAndConstraints(...$andConstraints);
}, $orConstraints));
}
private static function replaceWildcardXWithAsterisk(string $versionConstraint): string
{
// '1.x.x' -> '1.*'
$versionConstraint = self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^(\d+)\.[xX]\.[xX]$}',
'$1.*',
);
// '1.x' -> '1.*'
$versionConstraint = self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^(\d+)\.[xX]$}',
'$1.*',
);
// 'x' -> '*'
return self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^[xX]$}',
'*',
);
}
private static function replaceWildcardWithTilde(string $versionConstraint): string
{
return self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^(\d+(?:\.\d+)*)\.[*xX]$}',
'~$1.0',
);
}
private static function replaceTildeWithCaret(string $versionConstraint): string
{
return self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^~(\d+(?:\.\d+)?)$}',
'^$1',
);
}
private static function removeDuplicateVersionConstraints(string $versionConstraint): string
{
$orConstraints = self::splitIntoOrConstraints($versionConstraint);
return self::joinOrConstraints(...\array_unique(\array_map(static function (string $orConstraint): string {
$andConstraints = self::splitIntoAndConstraints($orConstraint);
return self::joinAndConstraints(...\array_unique($andConstraints));
}, $orConstraints)));
}
private static function removeLeadingVersionPrefix(string $versionConstraint): string
{
return self::applyRegularExpressionReplacementToVersionsInTurn(
$versionConstraint,
'{^(|[!<>]=|[~<>^])v(\d+.*(?<!-dev))$}',
'$1$2',
);
}
private static function assertDevPrefixSuffixPosition(string $versionConstraint): string
{
$split = \explode(
' ',
$versionConstraint,
);
foreach ($split as &$part) {
if (\strlen($part) <= 4) {
continue;
}
if (\strpos($part, 'dev-') === 0) {
$branch = \substr($part, 4);
} elseif (\substr($part, -4) === '-dev') {
$branch = \substr($part, 0, -4);
} else {
continue;
}
// @see https://github.com/composer/semver/blob/3.4.0/src/VersionParser.php#L216
if (\preg_match('{^v?\d+(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?$}i', $branch)) {
$part = $branch . '-dev';
} else {
$part = 'dev-' . $branch;
}
}
return \implode(
' ',
$split,
);
}
private static function removeOverlappingVersionConstraints(string $versionConstraint): string
{
$orConstraints = self::splitIntoOrConstraints($versionConstraint);
$regex = '{^[~^]?\d+(?:\.\d+)*$}';
$count = \count($orConstraints);
for ($i = 0; $i < $count; ++$i) {
$a = $orConstraints[$i];
if (!\is_string($a)) {
continue;
}
if (1 !== \preg_match($regex, $a)) {
continue;
}
for ($j = $i + 1; $j < $count; ++$j) {
$b = $orConstraints[$j];
if (!\is_string($b)) {
continue;
}
if (1 !== \preg_match($regex, $b)) {
continue;
}
if (Semver\Semver::satisfies(\ltrim($a, '^~'), $b) || Semver\Semver::satisfies(\ltrim($b, '^~'), $a)) {
if ('^' === $a[0]) {
$orConstraints[$j] = null;
} elseif ('^' === $b[0]) {
$orConstraints[$i] = null;
} elseif ('~' === $a[0]) {
$orConstraints[$j] = null;
} elseif ('~' === $b[0]) {
$orConstraints[$i] = null;
}
}
}
}
return self::joinOrConstraints(...\array_filter($orConstraints, static function (?string $orConstraint): bool {
return \is_string($orConstraint);
}));
}
private static function removeUselessInlineAliases(string $normalized): string
{
return \preg_replace_callback(
'{(\S+)\s+as\s+(\S+)}',
static function (array $matches): string {
if ($matches[1] === $matches[2]) {
return $matches[1];
}
return $matches[0];
},
$normalized,
);
}
private static function sortVersionConstraints(string $versionConstraint): string
{
$normalize = static function (string $versionConstraint): string {
return \trim($versionConstraint, '<>=!~^');
};
$sort = static function (string $a, string $b) use ($normalize): int {
return \strnatcmp(
$normalize($a),
$normalize($b),
);
};
$orConstraints = self::splitIntoOrConstraints($versionConstraint);
$orConstraints = \array_map(static function (string $orConstraint) use ($sort): string {
$andConstraints = self::splitIntoAndConstraints($orConstraint);
\usort($andConstraints, $sort);
return self::joinAndConstraints(...$andConstraints);
}, $orConstraints);
\usort($orConstraints, $sort);
return self::joinOrConstraints(...$orConstraints);
}
/**
* @see https://github.com/composer/semver/blob/3.3.2/src/VersionParser.php#L257
*
* @return list<string>
*/
private static function splitIntoOrConstraints(string $versionConstraint): array
{
return \preg_split(
'{\s*\|\|?\s*}',
$versionConstraint,
);
}
private static function joinOrConstraints(string ...$orConstraints): string
{
return \implode(
' || ',
$orConstraints,
);
}
/**
* @see https://github.com/composer/semver/blob/3.3.2/src/VersionParser.php#L264
*
* @return list<string>
*/
private static function splitIntoAndConstraints(string $orConstraint): array
{
return \preg_split(
'{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}',
$orConstraint,
);
}
private static function joinAndConstraints(string ...$andConstraints): string
{
return \implode(
' ',
$andConstraints,
);
}
/**
* @param non-empty-string $find
*/
private static function applyRegularExpressionReplacementToVersionsInTurn(string $versionConstraint, string $find, string $replace): string
{
$split = \explode(
' ',
$versionConstraint,
);
foreach ($split as &$part) {
$part = \preg_replace(
$find,
$replace,
$part,
);
}
return \implode(
' ',
$split,
);
}
}