| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of composer/semver. |
| 5 |
* |
| 6 |
* (c) Composer <https://github.com/composer> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view |
| 9 |
* the LICENSE file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Composer\Semver\Constraint; |
| 13 |
|
| 14 |
/** |
| 15 |
* Defines a conjunctive or disjunctive set of constraints. |
| 16 |
*/ |
| 17 |
class MultiConstraint implements ConstraintInterface |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var ConstraintInterface[] |
| 21 |
* @phpstan-var non-empty-array<ConstraintInterface> |
| 22 |
*/ |
| 23 |
protected $constraints; |
| 24 |
|
| 25 |
/** @var string|null */ |
| 26 |
protected $prettyString; |
| 27 |
|
| 28 |
/** @var string|null */ |
| 29 |
protected $string; |
| 30 |
|
| 31 |
/** @var bool */ |
| 32 |
protected $conjunctive; |
| 33 |
|
| 34 |
/** @var Bound|null */ |
| 35 |
protected $lowerBound; |
| 36 |
|
| 37 |
/** @var Bound|null */ |
| 38 |
protected $upperBound; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param ConstraintInterface[] $constraints A set of constraints |
| 42 |
* @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive |
| 43 |
* |
| 44 |
* @throws \InvalidArgumentException If less than 2 constraints are passed |
| 45 |
*/ |
| 46 |
public function __construct(array $constraints, $conjunctive = true) |
| 47 |
{ |
| 48 |
if (\count($constraints) < 2) { |
| 49 |
throw new \InvalidArgumentException( |
| 50 |
'Must provide at least two constraints for a MultiConstraint. Use '. |
| 51 |
'the regular Constraint class for one constraint only or MatchAllConstraint for none. You may use '. |
| 52 |
'MultiConstraint::create() which optimizes and handles those cases automatically.' |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
$this->constraints = $constraints; |
| 57 |
$this->conjunctive = $conjunctive; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @return ConstraintInterface[] |
| 62 |
*/ |
| 63 |
public function getConstraints() |
| 64 |
{ |
| 65 |
return $this->constraints; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public function isConjunctive() |
| 72 |
{ |
| 73 |
return $this->conjunctive; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function isDisjunctive() |
| 80 |
{ |
| 81 |
return !$this->conjunctive; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritDoc} |
| 86 |
*/ |
| 87 |
public function compile($otherOperator) |
| 88 |
{ |
| 89 |
$parts = array(); |
| 90 |
foreach ($this->constraints as $constraint) { |
| 91 |
$code = $constraint->compile($otherOperator); |
| 92 |
if ($code === 'true') { |
| 93 |
if (!$this->conjunctive) { |
| 94 |
return 'true'; |
| 95 |
} |
| 96 |
} elseif ($code === 'false') { |
| 97 |
if ($this->conjunctive) { |
| 98 |
return 'false'; |
| 99 |
} |
| 100 |
} else { |
| 101 |
$parts[] = '('.$code.')'; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if (!$parts) { |
| 106 |
return $this->conjunctive ? 'true' : 'false'; |
| 107 |
} |
| 108 |
|
| 109 |
return $this->conjunctive ? implode('&&', $parts) : implode('||', $parts); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param ConstraintInterface $provider |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public function matches(ConstraintInterface $provider) |
| 118 |
{ |
| 119 |
if (false === $this->conjunctive) { |
| 120 |
foreach ($this->constraints as $constraint) { |
| 121 |
if ($provider->matches($constraint)) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
// when matching a conjunctive and a disjunctive multi constraint we have to iterate over the disjunctive one |
| 130 |
// otherwise we'd return true if different parts of the disjunctive constraint match the conjunctive one |
| 131 |
// which would lead to incorrect results, e.g. [>1 and <2] would match [<1 or >2] although they do not intersect |
| 132 |
if ($provider instanceof MultiConstraint && $provider->isDisjunctive()) { |
| 133 |
return $provider->matches($this); |
| 134 |
} |
| 135 |
|
| 136 |
foreach ($this->constraints as $constraint) { |
| 137 |
if (!$provider->matches($constraint)) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* {@inheritDoc} |
| 147 |
*/ |
| 148 |
public function setPrettyString($prettyString) |
| 149 |
{ |
| 150 |
$this->prettyString = $prettyString; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* {@inheritDoc} |
| 155 |
*/ |
| 156 |
public function getPrettyString() |
| 157 |
{ |
| 158 |
if ($this->prettyString) { |
| 159 |
return $this->prettyString; |
| 160 |
} |
| 161 |
|
| 162 |
return (string) $this; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* {@inheritDoc} |
| 167 |
*/ |
| 168 |
public function __toString() |
| 169 |
{ |
| 170 |
if ($this->string !== null) { |
| 171 |
return $this->string; |
| 172 |
} |
| 173 |
|
| 174 |
$constraints = array(); |
| 175 |
foreach ($this->constraints as $constraint) { |
| 176 |
$constraints[] = (string) $constraint; |
| 177 |
} |
| 178 |
|
| 179 |
return $this->string = '[' . implode($this->conjunctive ? ' ' : ' || ', $constraints) . ']'; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* {@inheritDoc} |
| 184 |
*/ |
| 185 |
public function getLowerBound() |
| 186 |
{ |
| 187 |
$this->extractBounds(); |
| 188 |
|
| 189 |
if (null === $this->lowerBound) { |
| 190 |
throw new \LogicException('extractBounds should have populated the lowerBound property'); |
| 191 |
} |
| 192 |
|
| 193 |
return $this->lowerBound; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* {@inheritDoc} |
| 198 |
*/ |
| 199 |
public function getUpperBound() |
| 200 |
{ |
| 201 |
$this->extractBounds(); |
| 202 |
|
| 203 |
if (null === $this->upperBound) { |
| 204 |
throw new \LogicException('extractBounds should have populated the upperBound property'); |
| 205 |
} |
| 206 |
|
| 207 |
return $this->upperBound; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Tries to optimize the constraints as much as possible, meaning |
| 212 |
* reducing/collapsing congruent constraints etc. |
| 213 |
* Does not necessarily return a MultiConstraint instance if |
| 214 |
* things can be reduced to a simple constraint |
| 215 |
* |
| 216 |
* @param ConstraintInterface[] $constraints A set of constraints |
| 217 |
* @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive |
| 218 |
* |
| 219 |
* @return ConstraintInterface |
| 220 |
*/ |
| 221 |
public static function create(array $constraints, $conjunctive = true) |
| 222 |
{ |
| 223 |
if (0 === \count($constraints)) { |
| 224 |
return new MatchAllConstraint(); |
| 225 |
} |
| 226 |
|
| 227 |
if (1 === \count($constraints)) { |
| 228 |
return $constraints[0]; |
| 229 |
} |
| 230 |
|
| 231 |
$optimized = self::optimizeConstraints($constraints, $conjunctive); |
| 232 |
if ($optimized !== null) { |
| 233 |
list($constraints, $conjunctive) = $optimized; |
| 234 |
if (\count($constraints) === 1) { |
| 235 |
return $constraints[0]; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return new self($constraints, $conjunctive); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* @param ConstraintInterface[] $constraints |
| 244 |
* @param bool $conjunctive |
| 245 |
* @return ?array |
| 246 |
* |
| 247 |
* @phpstan-return array{0: list<ConstraintInterface>, 1: bool}|null |
| 248 |
*/ |
| 249 |
private static function optimizeConstraints(array $constraints, $conjunctive) |
| 250 |
{ |
| 251 |
// parse the two OR groups and if they are contiguous we collapse |
| 252 |
// them into one constraint |
| 253 |
// [>= 1 < 2] || [>= 2 < 3] || [>= 3 < 4] => [>= 1 < 4] |
| 254 |
if (!$conjunctive) { |
| 255 |
$left = $constraints[0]; |
| 256 |
$mergedConstraints = array(); |
| 257 |
$optimized = false; |
| 258 |
for ($i = 1, $l = \count($constraints); $i < $l; $i++) { |
| 259 |
$right = $constraints[$i]; |
| 260 |
if ( |
| 261 |
$left instanceof self |
| 262 |
&& $left->conjunctive |
| 263 |
&& $right instanceof self |
| 264 |
&& $right->conjunctive |
| 265 |
&& \count($left->constraints) === 2 |
| 266 |
&& \count($right->constraints) === 2 |
| 267 |
&& ($left0 = (string) $left->constraints[0]) |
| 268 |
&& $left0[0] === '>' && $left0[1] === '=' |
| 269 |
&& ($left1 = (string) $left->constraints[1]) |
| 270 |
&& $left1[0] === '<' |
| 271 |
&& ($right0 = (string) $right->constraints[0]) |
| 272 |
&& $right0[0] === '>' && $right0[1] === '=' |
| 273 |
&& ($right1 = (string) $right->constraints[1]) |
| 274 |
&& $right1[0] === '<' |
| 275 |
&& substr($left1, 2) === substr($right0, 3) |
| 276 |
) { |
| 277 |
$optimized = true; |
| 278 |
$left = new MultiConstraint( |
| 279 |
array( |
| 280 |
$left->constraints[0], |
| 281 |
$right->constraints[1], |
| 282 |
), |
| 283 |
true); |
| 284 |
} else { |
| 285 |
$mergedConstraints[] = $left; |
| 286 |
$left = $right; |
| 287 |
} |
| 288 |
} |
| 289 |
if ($optimized) { |
| 290 |
$mergedConstraints[] = $left; |
| 291 |
return array($mergedConstraints, false); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// TODO: Here's the place to put more optimizations |
| 296 |
|
| 297 |
return null; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* @return void |
| 302 |
*/ |
| 303 |
private function extractBounds() |
| 304 |
{ |
| 305 |
if (null !== $this->lowerBound) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
foreach ($this->constraints as $constraint) { |
| 310 |
if (null === $this->lowerBound || null === $this->upperBound) { |
| 311 |
$this->lowerBound = $constraint->getLowerBound(); |
| 312 |
$this->upperBound = $constraint->getUpperBound(); |
| 313 |
continue; |
| 314 |
} |
| 315 |
|
| 316 |
if ($constraint->getLowerBound()->compareTo($this->lowerBound, $this->isConjunctive() ? '>' : '<')) { |
| 317 |
$this->lowerBound = $constraint->getLowerBound(); |
| 318 |
} |
| 319 |
|
| 320 |
if ($constraint->getUpperBound()->compareTo($this->upperBound, $this->isConjunctive() ? '<' : '>')) { |
| 321 |
$this->upperBound = $constraint->getUpperBound(); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |