Completed
Push — master ( d291df...b0e701 )
by brian
01:56
created

src/Comparator/ComparatorFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @copyright   (c) 2014-2017 brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ptlis\SemanticVersion\Comparator;
13
14
/**
15
 * Factory to build comparator instances from string representation.
16
 */
17
final class ComparatorFactory
18
{
19
    /** @var ComparatorInterface[] */
20
    private $comparatorStringMap;
21
22
23
    /**
24
     * Constructor.
25
     */
26 2
    public function __construct()
27
    {
28 2
        $this->comparatorStringMap = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('>' => new \ptlis\...n\Comparator\EqualTo()) of type array<string,object<ptli...Comparator\\EqualTo>"}> is incompatible with the declared type array<integer,object<ptl...r\ComparatorInterface>> of property $comparatorStringMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29 2
            '>' => new GreaterThan(),
30 2
            '>=' => new GreaterOrEqualTo(),
31 2
            '<' => new LessThan(),
32 2
            '<=' => new LessOrEqualTo(),
33 2
            '=' => new EqualTo()
34 2
        ];
35 2
    }
36
37
    /**
38
     * Build a comparator instance from it's string representation
39
     *
40
     * @param string $comparatorString
41
     *
42
     * @return ComparatorInterface|null
43
     */
44 2
    public function get($comparatorString)
45
    {
46 2
        if (!$this->isComparator($comparatorString)) {
47 1
            throw new \RuntimeException('Unknown comparator "' . $comparatorString . '" encountered"');
48
        }
49
50 1
        return $this->comparatorStringMap[$comparatorString];
51
    }
52
53
    /**
54
     * Returns true if the provided string is a valid comparator.
55
     *
56
     * @param string $comparatorString
57
     *
58
     * @return bool
59
     */
60 2
    public function isComparator($comparatorString)
61
    {
62 2
        return array_key_exists($comparatorString, $this->comparatorStringMap);
63
    }
64
}
65