Completed
Push — master ( 93d881...ef3ef7 )
by Siro Díaz
01:33
created

ListAbstract::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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