Test Failed
Push — master ( 671c14...f91498 )
by Martins
03:03
created

Node::interSelect()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MartanLV\Koki;
5
6
/**
7
 * Class Node
8
 * @author yourname
9
 */
10
class Node
11
{
12
    /**
13
     * @var Interval
14
     */
15
    public $interval;
16
    /**
17
     * @var int
18
     */
19
    public $max;
20
    /**
21
     * @var Node
22
     */
23
    public $left;
24
    /**
25
     * @var Node
26
     */
27
    public $right;
28
29
    /**
30
     * undocumented function
31
     *
32
     * @return void
33
     */
34
    public function __construct(IntervalInterface $interval, $left = null, $right = null, $max = 0)
35
    {
36
        $this->interval = $interval;
0 ignored issues
show
Documentation Bug introduced by
$interval is of type MartanLV\Koki\IntervalInterface, but the property $interval was declared to be of type MartanLV\Koki\Interval. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
37
        $this->left = $left;
38
        $this->right = $right;
39
        $this->max = $max ? $max : $interval->high;
0 ignored issues
show
Bug introduced by
Accessing high on the interface MartanLV\Koki\IntervalInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40
    }
41
42
    /**
43
     * returns intervals that fall within interval range
44
     *
45
     * @return void
46
     */
47
    public function all(): array
48
    {
49
        return iterator_to_array($this->yieldAll(), false);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->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 introduced by
$this->yieldAll() of type void is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        return iterator_to_array(/** @scrutinizer ignore-type */ $this->yieldAll(), false);
Loading history...
Bug Best Practice introduced by
The expression return iterator_to_array...his->yieldAll(), false) returns the type array which is incompatible with the documented return type void.
Loading history...
50
    }
51
52
    /**
53
     * undocumented function
54
     *
55
     * @return void
56
     */
57
    public function yieldAll()
58
    {
59
        return $this->yieldSelect(-1, $this->max + 1);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->yieldSelect(-1, $this->max + 1) returns the type Generator which is incompatible with the documented return type void.
Loading history...
60
    }
61
62
    /**
63
     * returns intervals that touches a given range
64
     *
65
     * @return void
66
     */
67
    public function interSelect(int $low, int $high): array
68
    {
69
        return iterator_to_array($this->yieldInterSelect($low, $high), false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return iterator_to_array...ct($low, $high), false) returns the type array which is incompatible with the documented return type void.
Loading history...
70
    }
71
72
    /**
73
     * returns intervals that fall within interval range
74
     *
75
     * @return void
76
     */
77
    public function select(int $low, int $high): array
78
    {
79
        return iterator_to_array($this->yieldSelect($low, $high), false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return iterator_to_array...ct($low, $high), false) returns the type array which is incompatible with the documented return type void.
Loading history...
80
    }
81
82
    /**
83
     * returns intervals that touches a given range
84
     *
85
     * @return generator
0 ignored issues
show
Bug introduced by
The type MartanLV\Koki\generator was not found. Did you mean generator? If so, make sure to prefix the type with \.
Loading history...
86
     */
87 View Code Duplication
    public function yieldInterSelect(int $low, int $high)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        /**
90
         * does current node matches?
91
         */
92
        if ($this->interval->getEnd() - $low < $high && $this->interval->getStart() + $high > $low) {
93
            yield $this->interval;
94
        }
95
96
        /**
97
         * since the node's low value is less than the "select end" value,
98
         * we must search in the right subtree. If it exists.
99
         */
100
        if ($this->right && $this->interval->getStart() < $high) {
101
            yield from $this->right->yieldInterSelect($low, $high);
102
        }
103
        /**
104
         * If the left subtree's max exceeds the quiery's low value,
105
         * so we must search the left subtree as well.
106
         */
107
        if ($this->left && $this->left->max > $low) {
108
            yield from $this->left->yieldSelect($low, $high);
109
        }
110
111
    }
112
113
    /**
114
     * returns intervals that fall within interval range
115
     *
116
     * @return generator
117
     */
118 View Code Duplication
    public function yieldSelect(int $low, int $high)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        /**
121
         * does current node matches?
122
         */
123
        if ($this->interval->getEnd() < $high && $this->interval->getStart() > $low) {
124
            yield $this->interval;
125
        }
126
127
        /**
128
         * since the node's low value is less than the "select end" value,
129
         * we must search in the right subtree. If it exists.
130
         */
131
        if ($this->right && $this->interval->getStart() < $high) {
132
            yield from $this->right->yieldSelect($low, $high);
133
        }
134
        /**
135
         * If the left subtree's max exceeds the quiery's low value,
136
         * so we must search the left subtree as well.
137
         */
138
        if ($this->left && $this->left->max > $low) {
139
            yield from $this->left->yieldSelect($low, $high);
140
        }
141
142
    }
143
}
144