| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Composer Merge plugin. |
| 5 |
* |
| 6 |
* Copyright (C) 2021 Bryan Davis, Wikimedia Foundation, and contributors |
| 7 |
* |
| 8 |
* This software may be modified and distributed under the terms of the MIT |
| 9 |
* license. See the LICENSE file for details. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Wikimedia\Composer\Merge\V2; |
| 13 |
|
| 14 |
use Composer\Semver\Constraint\ConstraintInterface; |
| 15 |
use Composer\Semver\Constraint\EmptyConstraint; |
| 16 |
use Composer\Semver\Constraint\MultiConstraint as SemverMultiConstraint; |
| 17 |
use function count; |
| 18 |
|
| 19 |
/** |
| 20 |
* Adapted from Composer's v2 MultiConstraint::create for Composer v1 |
| 21 |
* @link https://github.com/composer/semver/blob/3.2.4/src/Constraint/MultiConstraint.php |
| 22 |
* @author Chauncey McAskill <[email protected]> |
| 23 |
*/ |
| 24 |
class MultiConstraint extends SemverMultiConstraint |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Tries to optimize the constraints as much as possible, meaning |
| 28 |
* reducing/collapsing congruent constraints etc. |
| 29 |
* Does not necessarily return a MultiConstraint instance if |
| 30 |
* things can be reduced to a simple constraint |
| 31 |
* |
| 32 |
* @param ConstraintInterface[] $constraints A set of constraints |
| 33 |
* @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive |
| 34 |
* |
| 35 |
* @return ConstraintInterface |
| 36 |
*/ |
| 37 |
public static function create(array $constraints, $conjunctive = true) |
| 38 |
{ |
| 39 |
if (count($constraints) === 0) { |
| 40 |
// EmptyConstraint only exists in composer 1.x. Configure as to run phan against composer 2.x |
| 41 |
// @phan-suppress-next-line PhanTypeMismatchReturn, PhanUndeclaredClassMethod |
| 42 |
return new EmptyConstraint(); |
| 43 |
} |
| 44 |
|
| 45 |
if (count($constraints) === 1) { |
| 46 |
return $constraints[0]; |
| 47 |
} |
| 48 |
|
| 49 |
$optimized = self::optimizeConstraints($constraints, $conjunctive); |
| 50 |
if ($optimized !== null) { |
| 51 |
list($constraints, $conjunctive) = $optimized; |
| 52 |
if (count($constraints) === 1) { |
| 53 |
return $constraints[0]; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return new self($constraints, $conjunctive); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @return array|null |
| 62 |
*/ |
| 63 |
private static function optimizeConstraints(array $constraints, $conjunctive) |
| 64 |
{ |
| 65 |
// parse the two OR groups and if they are contiguous we collapse |
| 66 |
// them into one constraint |
| 67 |
// [>= 1 < 2] || [>= 2 < 3] || [>= 3 < 4] => [>= 1 < 4] |
| 68 |
if (!$conjunctive) { |
| 69 |
$left = $constraints[0]; |
| 70 |
$mergedConstraints = []; |
| 71 |
$optimized = false; |
| 72 |
for ($i = 1, $l = count($constraints); $i < $l; $i++) { |
| 73 |
$right = $constraints[$i]; |
| 74 |
if ($left instanceof SemverMultiConstraint |
| 75 |
&& $left->conjunctive |
| 76 |
&& $right instanceof SemverMultiConstraint |
| 77 |
&& $right->conjunctive |
| 78 |
&& count($left->constraints) === 2 |
| 79 |
&& count($right->constraints) === 2 |
| 80 |
&& ($left0 = (string) $left->constraints[0]) |
| 81 |
&& $left0[0] === '>' && $left0[1] === '=' |
| 82 |
&& ($left1 = (string) $left->constraints[1]) |
| 83 |
&& $left1[0] === '<' |
| 84 |
&& ($right0 = (string) $right->constraints[0]) |
| 85 |
&& $right0[0] === '>' && $right0[1] === '=' |
| 86 |
&& ($right1 = (string) $right->constraints[1]) |
| 87 |
&& $right1[0] === '<' |
| 88 |
&& substr($left1, 2) === substr($right0, 3) |
| 89 |
) { |
| 90 |
$optimized = true; |
| 91 |
$left = new MultiConstraint( |
| 92 |
[ |
| 93 |
$left->constraints[0], |
| 94 |
$right->constraints[1], |
| 95 |
], |
| 96 |
true |
| 97 |
); |
| 98 |
} else { |
| 99 |
$mergedConstraints[] = $left; |
| 100 |
$left = $right; |
| 101 |
} |
| 102 |
} |
| 103 |
if ($optimized) { |
| 104 |
$mergedConstraints[] = $left; |
| 105 |
return [$mergedConstraints, false]; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// TODO: Here's the place to put more optimizations |
| 110 |
|
| 111 |
return null; |
| 112 |
} |
| 113 |
} |
| 114 |
// vim:sw=4:ts=4:sts=4:et: |