SortedLinkedList   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 34
c 0
b 0
f 0
dl 0
loc 161
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A popFront() 0 3 1
A popBack() 0 3 1
A insert() 0 3 1
A toArray() 0 3 1
A clear() 0 4 1
A findLeftPosition() 0 11 3
A count() 0 3 1
A __clone() 0 3 1
A getList() 0 3 1
A delete() 0 3 1
A refresh() 0 4 1
A __construct() 0 17 3
1
<?php
2
3
namespace Smoren\Containers\Structs;
4
5
use Closure;
6
use Countable;
7
use Exception;
8
use IteratorAggregate;
9
use Smoren\Containers\Exceptions\LinkedListException;
10
use Smoren\Containers\Exceptions\SortedLinkedListException;
11
12
abstract class SortedLinkedList implements IteratorAggregate, Countable
13
{
14
    /**
15
     * @var LinkedList data source
16
     */
17
    protected LinkedList $list;
18
    /**
19
     * @var Closure comparator
20
     */
21
    protected Closure $comparator;
22
23
    /**
24
     * SortedLinkedList constructor.
25
     * @param array|LinkedList $input default data list
26
     * @throws Exception
27
     */
28
    public function __construct($input = [])
29
    {
30
        if($input instanceof LinkedList) {
31
            $this->list = $input;
32
        } elseif(is_array($input)) {
0 ignored issues
show
introduced by
The condition is_array($input) is always true.
Loading history...
33
            $this->list = new LinkedList($input);
34
        } else {
35
            $linkedListType = LinkedList::class;
36
            $givenType = get_class($input);
37
            throw new SortedLinkedListException(
38
                "input must be instance of array or $linkedListType, given {$givenType}",
39
                SortedLinkedListException::STATUS_BAD_LINKED_LIST_TYPE
40
            );
41
        }
42
43
        $this->comparator = $this->getComparator();
44
        $this->refresh();
45
    }
46
47
    /**
48
     * Inserts element into collection
49
     * @param mixed $data element data value
50
     * @return LinkedListItem
51
     */
52
    public function insert($data): LinkedListItem
53
    {
54
        return $this->list->pushAfter($this->findLeftPosition($data), $data);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findLeftPosition($data) targeting Smoren\Containers\Struct...ist::findLeftPosition() 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...
55
    }
56
57
    /**
58
     * Converts collection to array
59
     * @return array
60
     */
61
    public function toArray(): array
62
    {
63
        return $this->list->toArray();
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function count(): int
70
    {
71
        return $this->list->count();
72
    }
73
74
    /**
75
     * @inheritDoc
76
     * @return LinkedListIterator
77
     */
78
    public function getIterator(): LinkedListIterator
79
    {
80
        return $this->list->getIterator();
81
    }
82
83
    /**
84
     * Returns source list
85
     * @return LinkedList
86
     */
87
    public function getList(): LinkedList
88
    {
89
        return $this->list;
90
    }
91
92
    /**
93
     * Removes element from the front of list
94
     * @return mixed data value of removed element
95
     * @throws LinkedListException
96
     */
97
    public function popFront()
98
    {
99
        return $this->list->popFront();
100
    }
101
102
    /**
103
     * Removes element from the back of list
104
     * @return mixed data value of removed element
105
     * @throws LinkedListException
106
     */
107
    public function popBack()
108
    {
109
        return $this->list->popBack();
110
    }
111
112
    /**
113
     * Removes element from target element position
114
     * @param LinkedListItem $item target element position
115
     * @return LinkedListItem old position of element
116
     */
117
    public function delete(LinkedListItem $item): LinkedListItem
118
    {
119
        return $this->list->delete($item);
120
    }
121
122
    /**
123
     * Clears list
124
     * @return $this
125
     */
126
    public function clear(): self
127
    {
128
        $this->list->clear();
129
        return $this;
130
    }
131
132
    /**
133
     * Refreshes items order
134
     * @return $this
135
     * @throws Exception
136
     */
137
    public function refresh(): self
138
    {
139
        $this->list->sort($this->comparator);
140
        return $this;
141
    }
142
143
    /**
144
     * Returns comparator function for sorting and position search
145
     * @return callable
146
     */
147
    abstract protected function getComparator(): callable;
148
149
    /**
150
     * Returns position max element which is less than argument (using comparator)
151
     * @param mixed $data element data value
152
     * @return LinkedListItem|null
153
     */
154
    protected function findLeftPosition($data): ?LinkedListItem
155
    {
156
        $position = null;
157
        foreach($this->list as $pos => $val) {
158
            if(!($this->comparator)($data, $val)) {
159
                break;
160
            }
161
            $position = $pos;
162
        }
163
164
        return $position;
165
    }
166
167
    /**
168
     * Clones object
169
     */
170
    public function __clone()
171
    {
172
        $this->list = clone $this->list;
173
    }
174
}
175