Completed
Push — master ( 555aef...4f8fd0 )
by Siro Díaz
01:26
created

ListAbstract::delete()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 14
nc 5
nop 1
1
<?php
2
3
namespace DataStructures\Lists;
4
5
use DataStructures\Lists\Interfaces\ListInterface;
6
use OutOfBoundsException;
7
8
abstract class ListAbstract implements ListInterface {
9
    protected $size;
10
11
    /**
12
     * Insert a node in the specified list position.
13
     *
14
     * @param integer $index position
15
     * @param mixed $data data to be saved
16
     */
17
    public function insert($index, $data) {
18
        if($index < 0) {
19
            throw new OutOfBoundsException();
20
        }
21
22
        if($index === 0) {
23
            $this->insertBeginning($data);
24
        } else if($index >= $this->size) {
25
            $this->insertEnd($data);
26
        } else if($index > 0 && $index < $this->size) {
27
            $this->insertAt($index, $data);
28
        }
29
        
30
        $this->size++;
31
    }
32
33
    /**
34
     * Add a new node in the specified index.
35
     *
36
     * @param integer $index the position.
37
     * @param mixed $data the data to be stored.
38
     */
39
    protected abstract function insertAt($index, $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
40
41
    /**
42
     * Add a new node in the specified index.
43
     *
44
     * @param mixed $data the data to be stored.
45
     */
46
    protected abstract function insertEnd($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
47
48
    /**
49
     * Inserts at the beginning of the list.
50
     *
51
     * @param mixed $data
52
     */
53
    protected abstract function insertBeginning($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
54
55
    /**
56
     * Removes all nodes of the list. It removes from the beginning.
57
     */
58
    public function clear() {
59
        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...
60
            $this->shift();
61
        }
62
    }
63
64
    /**
65
     * Binds to count() method. This is equal to make $this->tree->size().
66
     *
67
     * @return integer the tree size. 0 if it is empty.
68
     */
69
    public function count() {
70
        return $this->size;
71
    }
72
73
    /**
74
     * Returns the array size.
75
     *
76
     * @return int the length
77
     */
78
    public function size() : int {
79
        return $this->size;
80
    }
81
82
    /**
83
     * Checks if the list is empty.
84
     *
85
     * @return boolean true if is empty, else false.
86
     */
87
    public function empty() : bool {
88
        return $this->size === 0;
89
    }
90
91
    /**
92
     * Adds at the end of the list new node containing
93
     * the data to be stored.
94
     *
95
     * @param mixed $data The data
96
     */
97
    public function push($data) {
98
        $this->insert($this->size, $data);
99
    }
100
101
    /**
102
     * Adds at the beginning a node in the list.
103
     *
104
     * @param mixed $data
105
     * @return mixed the data stored.
106
     */
107
    public function unshift($data) {
108
        $this->insert(0, $data);
109
    }
110
111
    /**
112
     * Deletes the first node of the list and returns it.
113
     *
114
     * @return mixed the data.
115
     */
116
    public function shift() {
117
        return $this->delete(0);
118
    }
119
120
    /**
121
     * Removes and returns the last node in the list.
122
     *
123
     * @return mixed data in node.
124
     */
125
    public function pop() {
126
        return $this->delete($this->size - 1);
127
    }
128
129
    /**
130
     * Delete a node in the given position and returns it back.
131
     *
132
     * @param integer $index the position.
133
     * @throws OutOfBoundsException if index is negative
134
     *  or is greater than the size of the list.
135
     */
136
    public function delete($index) {
137
        if($index < 0 || ($index > 0 && $index > $this->size - 1)) {
138
            throw new OutOfBoundsException();
139
        }
140
141
        // if the list is empty
142
        if($this->empty()) {
143
            return null;
144
        }
145
        
146
        $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...
147
        if($index === 0) {
148
            $nodeData = $this->deleteBeginning();
149
        } else if($index === $this->size - 1) {
150
            $nodeData = $this->deleteEnd();
151
        } else {
152
            $nodeData = $this->deleteAt($index);
153
        }
154
155
        $this->size--;
156
        return $nodeData;
157
    }
158
159
    protected abstract function deleteBeginning();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
160
161
    protected abstract function deleteAt($index);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
162
163
    protected abstract function deleteEnd();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
164
}