Completed
Push — master ( bb3a8c...43409a )
by Siro Díaz
01:31
created

ListAbstract::deleteBeginning()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists;
10
11
use DataStructures\Lists\Interfaces\ListInterface;
12
use OutOfBoundsException;
13
14
/**
15
 * ListAbstract
16
 *
17
 * ListAbstract is a superclass that implements common operations in lists.
18
 * Also define abstract methods that are designed for implement a template method
19
 * design pattern.
20
 * 
21
 * @author Siro Diaz Palazon <[email protected]>
22
 */
23
abstract class ListAbstract implements ListInterface {
24
    protected $size;
25
26
    /**
27
     * Insert a node in the specified list position.
28
     *
29
     * @param integer $index position
30
     * @param mixed $data data to be saved
31
     */
32
    public function insert($index, $data) {
33
        if($index < 0) {
34
            throw new OutOfBoundsException();
35
        }
36
37
        if($index === 0) {
38
            $this->insertBeginning($data);
39
        } else if($index >= $this->size) {
40
            $this->insertEnd($data);
41
        } else if($index > 0 && $index < $this->size) {
42
            $this->insertAt($index, $data);
43
        }
44
        
45
        $this->size++;
46
    }
47
48
    /**
49
     * Add a new node in the specified index.
50
     *
51
     * @param integer $index the position.
52
     * @param mixed $data the data to be stored.
53
     */
54
    protected abstract function insertAt($index, $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
55
56
    /**
57
     * Add a new node in the specified index.
58
     *
59
     * @param mixed $data the data to be stored.
60
     */
61
    protected abstract function insertEnd($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
62
63
    /**
64
     * Inserts at the beginning of the list.
65
     *
66
     * @param mixed $data
67
     */
68
    protected abstract function insertBeginning($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
69
70
    /**
71
     * Removes all nodes of the list. It removes from the beginning.
72
     */
73
    public function clear() {
74
        while($this->head !== null) {
0 ignored issues
show
Bug introduced by
The property head does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
            $this->shift();
76
        }
77
    }
78
79
    /**
80
     * Binds to count() method. This is equal to make $this->tree->size().
81
     *
82
     * @return integer the tree size. 0 if it is empty.
83
     */
84
    public function count() {
85
        return $this->size;
86
    }
87
88
    /**
89
     * Returns the array size.
90
     *
91
     * @return int the length
92
     */
93
    public function size() : int {
94
        return $this->size;
95
    }
96
97
    /**
98
     * Checks if the list is empty.
99
     *
100
     * @return boolean true if is empty, else false.
101
     */
102
    public function empty() : bool {
103
        return $this->size === 0;
104
    }
105
106
    /**
107
     * Adds at the end of the list new node containing
108
     * the data to be stored.
109
     *
110
     * @param mixed $data The data
111
     */
112
    public function push($data) {
113
        $this->insert($this->size, $data);
114
    }
115
116
    /**
117
     * Adds at the beginning a node in the list.
118
     *
119
     * @param mixed $data
120
     * @return mixed the data stored.
121
     */
122
    public function unshift($data) {
123
        $this->insert(0, $data);
124
    }
125
126
    /**
127
     * Deletes the first node of the list and returns it.
128
     *
129
     * @return mixed the data.
130
     */
131
    public function shift() {
132
        return $this->delete(0);
133
    }
134
135
    /**
136
     * Removes and returns the last node in the list.
137
     *
138
     * @return mixed data in node.
139
     */
140
    public function pop() {
141
        return $this->delete($this->size - 1);
142
    }
143
144
    /**
145
     * Delete a node in the given position and returns it back.
146
     *
147
     * @param integer $index the position.
148
     * @throws OutOfBoundsException if index is negative
149
     *  or is greater than the size of the list.
150
     */
151
    public function delete($index) {
152
        if($index < 0 || ($index > 0 && $index > $this->size - 1)) {
153
            throw new OutOfBoundsException();
154
        }
155
156
        // if the list is empty
157
        if($this->empty()) {
158
            return null;
159
        }
160
        
161
        $nodeData = null;
0 ignored issues
show
Unused Code introduced by
$nodeData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
        if($index === 0) {
163
            $nodeData = $this->deleteBeginning();
164
        } else if($index === $this->size - 1) {
165
            $nodeData = $this->deleteEnd();
166
        } else {
167
            $nodeData = $this->deleteAt($index);
168
        }
169
170
        $this->size--;
171
        return $nodeData;
172
    }
173
174
    /**
175
     * Deletes at the beginnig of the list and returns the data stored.
176
     *
177
     * @return mixed the data stored in the node.
178
     */
179
    protected abstract function deleteBeginning();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
180
181
    /**
182
     * Deletes at the specified position and returns the data stored.
183
     *
184
     * @param integer $index the position.
185
     * @return mixed the data stored in the node.
186
     */
187
    protected abstract function deleteAt($index);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
188
189
    /**
190
     * Deletes at the end of the list and returns the data stored.
191
     *
192
     * @return mixed the data stored in the node.
193
     */
194
    protected abstract function deleteEnd();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
195
196
    /**
197
     * Converts/exports the list content into array type.
198
     *
199
     * @return array data stored in all nodes.
200
     */
201
    public function toArray() : array {
202
        $arr = [];
203
        foreach($this->getAll() as $node) {
204
            $arr[] = $node;
205
        }
206
207
        return $arr;
208
    }
209
210
    /**
211
     * Gets the data stored in the position especified.
212
     *
213
     * @param integer $index Index that must be greater than 0
214
     *  and lower than the list size.
215
     * @return mixed The data stored in the given index
216
     * @throws OutOfBoundsException if index is out bounds.
217
     */
218
    public function get($index) {
219
        $node = $this->search($index);
220
        if($node === null) {
221
            return null;
222
        }
223
224
        return $node->data;
225
    }
226
227
    /**
228
     * {@inheritDoc}
229
     */
230
    public function getLast() {
231
        $lastNode = $this->searchLast();
0 ignored issues
show
Bug introduced by
The method searchLast() does not exist on DataStructures\Lists\ListAbstract. Did you maybe mean search()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
232
        return $lastNode === null ? null : $lastNode->data;
233
    }
234
235
    /**
236
     * Gets the node stored in the position especified.
237
     * If index is 0 or (size - 1) the method is O(1) else O(n).
238
     *
239
     * @param integer $index the position.
240
     * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1)
241
     * @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index.
242
     */
243
    protected abstract function search($index);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
244
}