CallbackHeap   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 5
dl 0
loc 14
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 3 1
A __construct() 0 3 1
1
<?php namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use SplHeap;
13
14
/**
15
 * The CallbackHeap
16
 *
17
 * A simple class for defining a callback to use as the comparison function,
18
 * when building a heap. Note that this will always incur extra overhead on
19
 * each comparison, so if you need to define a simple heap always running the
20
 * same comparison, it makes more sense to define it in its own class extending
21
 * SplHeap. This class is appropriate when you have a set of comparisons to
22
 * choose from.
23
 *
24
 * @package structures
25
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
26
 */
27
28
class CallbackHeap extends SplHeap implements CollectionInterface
29
{
30
    use Traits\Collection;
31
32
    public $cb;
33
34
    public function __construct(callable $cb)
35
    {
36
        $this->cb = $cb;
37
    }
38
39
    public function compare($a, $b)
40
    {
41
        return call_user_func($this->cb, $a, $b);
42
    }
43
}
44