Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/CompilingMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class CompilingMatcher
{
/**
* @var array
* @phpstan-var array<string, callable>
* @phpstan-var array<Constraint::OP_*, array<int|string, callable|null>>
*/
private static $compiledCheckerCache = array();
/**
* @var array
* @phpstan-var array<string, bool>
* @phpstan-var array<Constraint::OP_*, array<string, array<int|string, bool>>>
*/
private static $resultCache = array();

Expand Down Expand Up @@ -68,27 +68,29 @@ public static function clear()
*/
public static function match(ConstraintInterface $constraint, $operator, $version)
{
$resultCacheKey = $operator.$constraint.';'.$version;
$constraintString = (string) $constraint;
// Resolve the operator and constraint cache bucket once, so each read or write below
// only needs to look up the version. Using a reference avoids repeating both outer
// array lookups and ensures new results are stored in the static cache, not a local copy.
$resultCache = &self::$resultCache[$operator][$constraintString];

if (isset(self::$resultCache[$resultCacheKey])) {
return self::$resultCache[$resultCacheKey];
if (isset($resultCache[$version])) {
return $resultCache[$version];
}

if (self::$enabled === null) {
self::$enabled = !\in_array('eval', explode(',', (string) ini_get('disable_functions')), true);
}
if (!self::$enabled) {
return self::$resultCache[$resultCacheKey] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version));
return $resultCache[$version] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version));
}

$cacheKey = $operator.$constraint;
if (!isset(self::$compiledCheckerCache[$cacheKey])) {
$compiledCheckerCache = &self::$compiledCheckerCache[$operator][$constraintString];
if (null === $compiledCheckerCache) {
$code = $constraint->compile($operator);
self::$compiledCheckerCache[$cacheKey] = $function = eval('return function($v, $b){return '.$code.';};');
} else {
$function = self::$compiledCheckerCache[$cacheKey];
$compiledCheckerCache = eval('return function($v, $b){return '.$code.';};');
}

return self::$resultCache[$resultCacheKey] = $function($version, strpos($version, 'dev-') === 0);
return $resultCache[$version] = $compiledCheckerCache($version, strpos($version, 'dev-') === 0);
}
}
30 changes: 30 additions & 0 deletions tests/CompilingMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,34 @@ public function testCacheKey()
$this->assertFalse(CompilingMatcher::match(new Constraint('>=', '2.11'), Constraint::OP_EQ, '1.0'));
$this->assertTrue(CompilingMatcher::match(new Constraint('>=', '2.1'), Constraint::OP_EQ, '11.0'));
}

public function testCacheSeparatesOperatorsConstraintsAndVersions()
{
CompilingMatcher::clear();
$constraint = new Constraint('>=', '2');

$this->assertFalse(CompilingMatcher::match($constraint, Constraint::OP_EQ, '1'));
$this->assertTrue(CompilingMatcher::match($constraint, Constraint::OP_GT, '1'));
$this->assertTrue(CompilingMatcher::match(new Constraint('<', '2'), Constraint::OP_EQ, '1'));
$this->assertTrue(CompilingMatcher::match($constraint, Constraint::OP_EQ, '3'));
$this->assertFalse(CompilingMatcher::match($constraint, Constraint::OP_EQ, '1'));
CompilingMatcher::clear();
}

public function testReusesCompiledCheckerAndClearsBothCaches()
{
CompilingMatcher::clear();
$constraint = $this->getMockBuilder('Composer\\Semver\\Constraint\\Constraint')
->setConstructorArgs(array('>=', '1'))
->setMethods(array('compile'))
->getMock();
$constraint->expects($this->exactly(2))->method('compile')->willReturn('$v === "2"');

$this->assertFalse(CompilingMatcher::match($constraint, Constraint::OP_EQ, '1'));
$this->assertFalse(CompilingMatcher::match($constraint, Constraint::OP_EQ, '1'));
$this->assertTrue(CompilingMatcher::match($constraint, Constraint::OP_EQ, '2'));
CompilingMatcher::clear();
$this->assertFalse(CompilingMatcher::match($constraint, Constraint::OP_EQ, '1'));
CompilingMatcher::clear();
}
}
Loading