Comparator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 98
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 8 2
A by() 0 4 1
A defaultComparator() 0 8 2
A ensure() 0 5 4
B compare() 0 12 5
A reverse() 0 4 1
A otherwise() 0 4 1
A innerCallback() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Comparable;
12
13
use Cubiche\Core\Delegate\AbstractCallable;
14
use Cubiche\Core\Visitor\VisiteeTrait;
15
16
/**
17
 * Comparator class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
class Comparator extends AbstractCallable implements ComparatorInterface
23
{
24
    use VisiteeTrait;
25
26
    /**
27
     * @var ComparatorInterface
28
     */
29
    private static $default = null;
30
31
    /**
32
     * @param callable $comparator
33
     *
34
     * @return \Cubiche\Core\Comparable\ComparatorInterface
35
     */
36
    public static function from(callable $comparator)
37
    {
38
        if ($comparator instanceof ComparatorInterface) {
39
            return $comparator;
40
        }
41
42
        return new CallbackComparator($comparator);
43
    }
44
45
    /**
46
     * @param callable  $selector
47
     * @param Direction $direction
48
     *
49
     * @return \Cubiche\Core\Comparable\ComparatorInterface
50
     */
51
    public static function by(callable $selector, Direction $direction = null)
52
    {
53
        return new SelectorComparator($selector, $direction);
54
    }
55
56
    /**
57
     * @return \Cubiche\Core\Comparable\ComparatorInterface
58
     */
59
    public static function defaultComparator()
60
    {
61
        if (self::$default === null) {
62
            self::$default = new self();
63
        }
64
65
        return self::$default;
66
    }
67
68
    /**
69
     * @param callable $comparator
70
     * @param callable $default
71
     *
72
     * @return \Cubiche\Core\Comparable\ComparatorInterface
73
     */
74
    public static function ensure(callable $comparator = null, callable $default = null)
75
    {
76
        return $comparator !== null || $default !== null ?
77
            self::from($comparator !== null ? $comparator : $default) : self::defaultComparator();
0 ignored issues
show
Bug introduced by
It seems like $comparator !== null ? $comparator : $default can also be of type null; however, Cubiche\Core\Comparable\Comparator::from() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function compare($a, $b)
84
    {
85
        if ($a instanceof ComparableInterface) {
86
            return $a->compareTo($b);
87
        }
88
89
        if ($b instanceof ComparableInterface) {
90
            return -1 * $b->compareTo($a);
91
        }
92
93
        return $a < $b ? -1 : ($a == $b ? 0 : 1);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function reverse()
100
    {
101
        return new ReverseComparator($this);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function otherwise(callable $comparator)
108
    {
109
        return new MultiComparator($this, $comparator);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    protected function innerCallback()
116
    {
117
        return array($this, 'compare');
118
    }
119
}
120