Completed
Push — master ( 15e170...076e98 )
by Siro Díaz
05:30
created

ArrayList::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DataStructures\Lists;
4
5
use DataStructures\Lists\Interfaces\ListInterface;
6
7
class ArrayList implements ListInterface {
8
    private $data;
9
    private $current;
10
    private $position;
11
    private $size;
12
13
    public function __construct() {
14
        $this->data = [];
15
        $this->size = 0;
16
        $this->position = 0;
17
    }
18
19
    /**
20
     * Add a new node in the specified index.
21
     *
22
     * @param integer $index the position.
23
     * @param mixed $data the data to be stored.
24
     */
25
    public function insert($index, $data) {
26
        array_splice($this->data, $index, 0, $data);
27
    }
28
    
29
    /**
30
     * Removes all nodes of the array. It removes from the beginning.
31
     */
32
    public function clear() {
33
        $this->data = [];
34
    }
35
    
36
    public function get($index) {
37
        return $this->data[$index];
38
    }
39
40
    public function getAll() {
41
42
    }
43
44
    public function empty() : bool {
45
        return $this->size === 0;
46
    }
47
    
48
    /**
49
     * Removes and returns the last item in the array.
50
     *
51
     * @return mixed data in node.
52
     */
53
    public function pop() {
54
        return array_pop($this->data);
55
    }
56
57
    /**
58
     * Adds at the end of the list new node containing
59
     * the data to be stored.
60
     *
61
     * @param mixed $data The data
62
     */
63
    public function push($data) {
64
        $this->data[] = $data;
65
    }
66
67
    public function delete($index) {
68
        unset($this->data[$index]);
69
    }
70
    
71
    /**
72
     * Returns the array size.
73
     *
74
     * @return int the length
75
     */
76
    public function size() : int {
77
        return $this->size;
78
    }
79
80
    /**
81
     * Returns array stored in the data attribute.
82
     *
83
     * @return array data stored in all nodes.
84
     */
85
    public function toArray() : array {
86
        return $this->data;
87
    }
88
89
    /**
90
     * Reset the cursor position.
91
     */
92
    public function rewind() {
93
        $this->position = 0;
94
        $this->current = $this->data[$this->position];
95
    }
96
97
    /**
98
     * Returns the current node data.
99
     *
100
     * @return mixed
101
     */
102
    public function current() {
103
        return $this->current;
104
    }
105
106
    /**
107
     * Key or index that indicates the cursor position.
108
     *
109
     * @return integer The current position.
110
     */
111
    public function key() {
112
        return $this->position;
113
    }
114
115
    /**
116
     * Move the cursor to the next node and increments the
117
     * position counter.
118
     */
119
    public function next() {
120
        ++$this->position;
121
        $this->current = $this->data[$this->position];
122
    }
123
124
    /**
125
     * Returns if the current pointer position is valid.
126
     *
127
     * @return boolean true if pointer is not last, else false.
128
     */
129
    public function valid() {
130
        return $this->position < $this->size;
131
    }
132
133
    /**
134
     * Binds to count() method. This is equal to make $this->list->size().
135
     *
136
     * @return integer the list size. 0 if it is empty.
137
     */
138
    public function count() {
139
        return $this->size;
140
    }
141
142
    public function offsetSet($offset, $valor) {
143
        //TODO
144
        if (is_null($offset)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
145
            // $this->contenedor[] = $valor;
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
147
            // $this->contenedor[$offset] = $valor;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
        }
149
    }
150
    
151
    public function offsetExists($offset) {
152
        //TODO
153
        return false;
154
        // return isset($this->contenedor[$offset]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
    }
156
157
    public function offsetUnset($offset) {
158
        //TODO
159
        // unset($this->contenedor[$offset]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
    }
161
162
    public function offsetGet($offset) {
163
        //TODO
164
        return false;
165
        // return isset($this->contenedor[$offset]) ? $this->contenedor[$offset] : null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
166
    }
167
}