Passed
Push — master ( f91498...28f3a4 )
by Martins
02:11
created

Tree::yieldInterSelect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartanLV\Koki;
6
7
/**
8
 * Augmented tree
9
 * Class StaticTree
10
 * @author yourname
11
 */
12
class Tree extends Node
13
{
14
    /**
15
     * undocumented function
16
     *
17
     * @param $intervals array of Interval
18
     * @return void
19
     */
20
    public function __construct(array $intervals, bool $preSorted = false)
21
    {
22
        !$preSorted && usort($intervals, function (IntervalInterface $i0, IntervalInterface $i) {
23
            return $i0->getStart() <=> $i->getStart();
24
        });
25
        $this->root = $this->toTree($intervals);
0 ignored issues
show
Bug Best Practice introduced by
The property root does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
    }
27
28
    /**
29
     * An augmented tree can be built from a simple ordered tree,
30
     * for example a binary search tree or self-balancing binary search tree,
31
     * ordered by the 'low' values of the intervals.
32
     */
33
    public function toTree(array $intervals)
34
    {
35
        $max = 0;
36
        $len = count($intervals);
37
        $mid = (int)floor($len / 2);
38
39
        if (!$len) {
40
            return null;
41
        }
42
43
        array_walk($intervals, function (IntervalInterface $el) use (&$max) {
44
            if ($max < $el->getEnd()) {
45
                $max = $el->getEnd();
46
            }
47
        });
48
        $l_intervals = array_splice($intervals, 0, $mid);
49
        $r_intervals = array_splice($intervals, -$mid);
50
        $interval = $intervals ? reset($intervals) : array_pop($l_intervals);
51
        $interval = $interval ?? array_pop($r_intervals);
52
53
        return new Node(
54
            $interval,
55
            $this->toTree($l_intervals),
56
            $this->toTree($r_intervals),
57
            $max
58
        );
59
    }
60
61
    /**
62
     * returns intervals that exclusively fall within range given
63
     * to retrieve eather bound inclusevely, just modify parameters
64
     *
65
     * @return void
66
     */
67
    public function select(int $low, int $high): array
68
    {
69
        return $this->root ? $this->root->select($low, $high) : [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->root ? $th...($low, $high) : array() returns the type array which is incompatible with the documented return type void.
Loading history...
70
    }
71
72
    /**
73
     * understandable var dump
74
     */
75
    public function __debugInfo()
76
    {
77
        $arr = $this->all();
78
79
        usort($arr, function (IntervalInterface $i0, IntervalInterface $i) {
80
            return $i0->getStart() <=> $i->getStart();
81
        });
82
83
        return $arr;
84
    }
85
86
    public function yieldInterSelect(int $low, int $high)
87
    {
88
        return $this->root ? $this->root->yieldInterSelect($low, $high) : [];
89
    }
90
91
    public function interSelect(int $low, int $high): array
92
    {
93
        return $this->root ? $this->root->interSelect($low, $high) : [];
94
    }
95
96
    public function yieldSelect(int $low, int $high)
97
    {
98
        return $this->root ? $this->root->yieldSelect($low, $high) : [];
99
    }
100
101
    /**
102
     *
103
     * @return void
104
     */
105
    public function all(): array
106
    {
107
        return $this->root ? $this->root->all() : [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->root ? $this->root->all() : array() returns the type array which is incompatible with the documented return type void.
Loading history...
108
    }
109
110
    /**
111
     * undocumented function
112
     *
113
     * @return void
114
     */
115
    public function yieldAll()
116
    {
117
        return $this->root ? $this->root->yieldAll() : [];
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->root->yieldAll() targeting MartanLV\Koki\Node::yieldAll() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->root ? $th...t->yieldAll() : array() returns the type array which is incompatible with the documented return type void.
Loading history...
118
    }
119
}
120