function MultiConstraint::optimizeConstraints
@phpstan-return array{0: list<ConstraintInterface>, 1: bool}|null
Parameters
ConstraintInterface[] $constraints:
bool $conjunctive:
Return value
?array
1 call to MultiConstraint::optimizeConstraints()
- MultiConstraint::create in vendor/
composer/ semver/ src/ Constraint/ MultiConstraint.php - Tries to optimize the constraints as much as possible, meaning reducing/collapsing congruent constraints etc. Does not necessarily return a MultiConstraint instance if things can be reduced to a simple constraint
File
-
vendor/
composer/ semver/ src/ Constraint/ MultiConstraint.php, line 249
Class
- MultiConstraint
- Defines a conjunctive or disjunctive set of constraints.
Namespace
Composer\Semver\ConstraintCode
private static function optimizeConstraints(array $constraints, $conjunctive) {
// parse the two OR groups and if they are contiguous we collapse
// them into one constraint
// [>= 1 < 2] || [>= 2 < 3] || [>= 3 < 4] => [>= 1 < 4]
if (!$conjunctive) {
$left = $constraints[0];
$mergedConstraints = array();
$optimized = false;
for ($i = 1, $l = \count($constraints); $i < $l; $i++) {
$right = $constraints[$i];
if ($left instanceof self && $left->conjunctive && $right instanceof self && $right->conjunctive && \count($left->constraints) === 2 && \count($right->constraints) === 2 && ($left0 = (string) $left->constraints[0]) && $left0[0] === '>' && $left0[1] === '=' && ($left1 = (string) $left->constraints[1]) && $left1[0] === '<' && ($right0 = (string) $right->constraints[0]) && $right0[0] === '>' && $right0[1] === '=' && ($right1 = (string) $right->constraints[1]) && $right1[0] === '<' && substr($left1, 2) === substr($right0, 3)) {
$optimized = true;
$left = new MultiConstraint(array(
$left->constraints[0],
$right->constraints[1],
), true);
}
else {
$mergedConstraints[] = $left;
$left = $right;
}
}
if ($optimized) {
$mergedConstraints[] = $left;
return array(
$mergedConstraints,
false,
);
}
}
// TODO: Here's the place to put more optimizations
return null;
}