AbstractSorter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 6
c 2
b 0
f 2
lcom 1
cbo 0
dl 0
loc 58
ccs 11
cts 11
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 4 1
invoke() 0 1 ?
A compare() 0 7 3
A order() 0 4 1
1
<?php
2
namespace TRex\Collection;
3
4
abstract class AbstractSorter
5
{
6
    const ASC_ORDER = 1;
7
    const DESC_ORDER = -1;
8
9
    /**
10
     * @var int
11
     */
12
    private $order;
13
14
    /**
15
     * @param int $order
16
     */
17 2
    public function __construct($order = self::ASC_ORDER)
18
    {
19 2
        $this->order = $order;
20 2
    }
21
22
    /**
23
     * @param mixed $object1
24
     * @param mixed $object2
25
     * @return int
26
     */
27 2
    public function __invoke($object1, $object2)
28
    {
29 2
        return $this->compare($this->invoke($object1), $this->invoke($object2));
30
    }
31
32
    /**
33
     * calls the property/method/key to extract the value to sort.
34
     *
35
     * @param mixed $object
36
     * @return mixed
37
     */
38
    abstract protected function invoke($object);
39
40
    /**
41
     * @param mixed $value1
42
     * @param mixed $value2
43
     * @return int
44
     */
45 2
    private function compare($value1, $value2)
46
    {
47 2
        if ($value1 == $value2) {
48 2
            return 0;
49
        }
50 2
        return $this->order(($value1 < $value2) ? -1 : 1);
51
    }
52
53
    /**
54
     * @param int $value
55
     * @return int
56
     */
57 2
    private function order($value)
58
    {
59 2
        return $this->order * $value;
60
    }
61
}
62
63