| 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)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             // $this->contenedor[] = $valor; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } else { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             // $this->contenedor[$offset] = $valor; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     public function offsetExists($offset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         //TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         // return isset($this->contenedor[$offset]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function offsetUnset($offset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         //TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         // unset($this->contenedor[$offset]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function offsetGet($offset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         //TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         // return isset($this->contenedor[$offset]) ? $this->contenedor[$offset] : null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  | } | 
            
                        
This check looks for the bodies of
ifstatements 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
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.