CompilingMatcher::match()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 20
rs 9.4555
c 0
b 0
f 0
cc 5
nc 7
nop 3
1
<?php
2
3
namespace HumbugBox451\Composer\Semver;
4
5
use HumbugBox451\Composer\Semver\Constraint\Constraint;
6
use HumbugBox451\Composer\Semver\Constraint\ConstraintInterface;
7
/** @internal */
8
class CompilingMatcher
9
{
10
    /**
11
    @phpstan-var
12
    */
13
    private static $compiledCheckerCache = array();
14
    /**
15
    @phpstan-var
16
    */
17
    private static $resultCache = array();
18
    private static $enabled;
19
    /**
20
    @phpstan-var
21
    */
22
    private static $transOpInt = array(Constraint::OP_EQ => Constraint::STR_OP_EQ, Constraint::OP_LT => Constraint::STR_OP_LT, Constraint::OP_LE => Constraint::STR_OP_LE, Constraint::OP_GT => Constraint::STR_OP_GT, Constraint::OP_GE => Constraint::STR_OP_GE, Constraint::OP_NE => Constraint::STR_OP_NE);
23
    public static function clear()
24
    {
25
        self::$resultCache = array();
26
        self::$compiledCheckerCache = array();
27
    }
28
    /**
29
    @phpstan-param
30
    */
31
    public static function match(ConstraintInterface $constraint, $operator, $version)
32
    {
33
        $resultCacheKey = $operator . $constraint . ';' . $version;
34
        if (isset(self::$resultCache[$resultCacheKey])) {
35
            return self::$resultCache[$resultCacheKey];
36
        }
37
        if (self::$enabled === null) {
38
            self::$enabled = !\in_array('eval', \explode(',', (string) \ini_get('disable_functions')), \true);
39
        }
40
        if (!self::$enabled) {
41
            return self::$resultCache[$resultCacheKey] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version));
42
        }
43
        $cacheKey = $operator . $constraint;
44
        if (!isset(self::$compiledCheckerCache[$cacheKey])) {
45
            $code = $constraint->compile($operator);
46
            self::$compiledCheckerCache[$cacheKey] = $function = eval('return function($v, $b){return ' . $code . ';};');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
47
        } else {
48
            $function = self::$compiledCheckerCache[$cacheKey];
49
        }
50
        return self::$resultCache[$resultCacheKey] = $function($version, \strpos($version, 'dev-') === 0);
51
    }
52
}
53