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

ListAbstract   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 102
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B insert() 0 15 6
insertAt() 0 1 ?
insertEnd() 0 1 ?
insertBeginning() 0 1 ?
A count() 0 3 1
A size() 0 3 1
A empty() 0 3 1
A push() 0 3 1
A unshift() 0 3 1
A shift() 0 3 1
A pop() 0 3 1
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
    protected abstract function insertEnd($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
41
42
    protected abstract function insertBeginning($data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
43
44
    /**
45
     * Binds to count() method. This is equal to make $this->tree->size().
46
     *
47
     * @return integer the tree size. 0 if it is empty.
48
     */
49
    public function count() {
50
        return $this->size;
51
    }
52
53
    /**
54
     * Returns the array size.
55
     *
56
     * @return int the length
57
     */
58
    public function size() : int {
59
        return $this->size;
60
    }
61
62
    /**
63
     * Checks if the list is empty.
64
     *
65
     * @return boolean true if is empty, else false.
66
     */
67
    public function empty() : bool {
68
        return $this->size === 0;
69
    }
70
71
    /**
72
     * Adds at the end of the list new node containing
73
     * the data to be stored.
74
     *
75
     * @param mixed $data The data
76
     */
77
    public function push($data) {
78
        $this->insert($this->size, $data);
79
    }
80
81
    /**
82
     * Adds at the beginning a node in the list.
83
     *
84
     * @param mixed $data
85
     * @return mixed the data stored.
86
     */
87
    public function unshift($data) {
88
        $this->insert(0, $data);
89
    }
90
91
    /**
92
     * Deletes the first node of the list and returns it.
93
     *
94
     * @return mixed the data.
95
     */
96
    public function shift() {
97
        return $this->delete(0);
98
    }
99
100
    /**
101
     * Removes and returns the last node in the list.
102
     *
103
     * @return mixed data in node.
104
     */
105
    public function pop() {
106
        return $this->delete($this->size - 1);
107
    }
108
}