CallbackComparator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
wmc 2
lcom 1
cbo 0
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A compare() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the phpcommon/comparison package.
5
 *
6
 * (c) Marcos Passos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace PhpCommon\Comparison\Comparator;
13
14
use PhpCommon\Comparison\Comparator;
15
16
/**
17
 * A comparator the uses a callback to compare values.
18
 *
19
 * This comparator is useful when simple, one-off comparators need to be
20
 * created. It can be used to change or extend existing comparators in order to
21
 * achieve new ones that better fit to the underlying needs.
22
 *
23
 * @author Marcos Passos <[email protected]>
24
 */
25
class CallbackComparator implements Comparator
26
{
27
    /**
28
     * @var callable
29
     */
30
    protected $callback;
31
32
    /**
33
     * CallbackComparator constructor.
34
     *
35
     * @param callable $callback
36
     */
37 2
    public function __construct(callable $callback)
38
    {
39 2
        $this->callback = $callback;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function compare($left, $right)
46
    {
47 2
        return call_user_func_array($this->callback, [$left, $right]);
48
    }
49
}
50