Completed
Pull Request — master (#16)
by Nikola
01:32
created

ComparisonConstraint::assert()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 14
cts 15
cp 0.9333
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 14
nc 7
nop 1
crap 7.0145
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version\Constraint;
6
7
use Version\Version;
8
use Version\Exception\InvalidConstraintException;
9
10
/** @noinspection PhpInconsistentReturnPointsInspection */
11
12
/**
13
 * @author Nikola Posa <[email protected]>
14
 */
15
class ComparisonConstraint implements ConstraintInterface
16
{
17
    public const OPERATOR_EQ = '=';
18
    public const OPERATOR_NEQ = '!=';
19
    public const OPERATOR_GT = '>';
20
    public const OPERATOR_GTE = '>=';
21
    public const OPERATOR_LT = '<';
22
    public const OPERATOR_LTE = '<=';
23
24
    /**
25
     * @var string
26
     */
27
    protected $operator;
28
29
    /**
30
     * @var Version
31
     */
32
    protected $operand;
33
34
    /**
35
     * @var array
36
     */
37
    private static $validOperators = [
38
        self::OPERATOR_EQ,
39
        self::OPERATOR_NEQ,
40
        self::OPERATOR_GT,
41
        self::OPERATOR_GTE,
42
        self::OPERATOR_LT,
43
        self::OPERATOR_LTE,
44
    ];
45
46 15
    public function __construct(string $operator, Version $operand)
47
    {
48 15
        if (! in_array($operator, static::$validOperators, true)) {
0 ignored issues
show
Bug introduced by
Since $validOperators is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $validOperators to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
49 1
            throw InvalidConstraintException::forOperator($operator);
50
        }
51
52 14
        $this->operator = $operator;
53 14
        $this->operand = $operand;
54 14
    }
55
56 11
    public static function fromString(string $constraintString) : ConstraintInterface
57
    {
58 11
        static $parser = null;
59
60 11
        if (null === $parser) {
61 1
            $parser = new ComparisonConstraintParser();
62
        }
63
64 11
        return $parser->parse($constraintString);
65
    }
66
67 5
    public function getOperator() : string
68
    {
69 5
        return $this->operator;
70
    }
71
72 5
    public function getOperand() : Version
73
    {
74 5
        return $this->operand;
75
    }
76
77 10
    public function assert(Version $version) : bool
78
    {
79 10
        switch ($this->operator) {
80 10
            case self::OPERATOR_EQ:
81 2
                return $version->isEqualTo($this->operand);
82 8
            case self::OPERATOR_NEQ:
83 1
                return !$version->isEqualTo($this->operand);
84 7
            case self::OPERATOR_GT:
85 2
                return $version->isGreaterThan($this->operand);
86 5
            case self::OPERATOR_GTE:
87 3
                return $version->isGreaterOrEqualTo($this->operand);
88 3
            case self::OPERATOR_LT:
89 2
                return $version->isLessThan($this->operand);
90 1
            case self::OPERATOR_LTE:
91 1
                return $version->isLessOrEqualTo($this->operand);
92
        }
93
    }
94
}
95