| Total Complexity | 49 |
| Total Lines | 256 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IteratorCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IteratorCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class IteratorCollection extends Variable implements \ArrayAccess |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $elements; |
||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | |||
| 39 | //put your code here |
||
| 40 | |||
| 41 | /** |
||
| 42 | * IteratorCollection constructor. |
||
| 43 | * @param array $elements |
||
| 44 | */ |
||
| 45 | public function __construct(array $elements = array()) |
||
| 46 | { |
||
| 47 | |||
| 48 | parent::__construct($elements); |
||
| 49 | |||
| 50 | $this->elements = $elements; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function setElementsFromTrustedSource(array $elements) |
||
| 54 | { |
||
| 55 | $this->elements = $elements; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public function toArray(): array |
||
| 63 | { |
||
| 64 | return $this->elements; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return \ArrayObject |
||
| 69 | */ |
||
| 70 | public function getIterator() |
||
| 71 | { |
||
| 72 | |||
| 73 | return new \ArrayObject($this->elements); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function count() |
||
| 77 | { |
||
| 78 | return count($this->elements); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function current() |
||
| 82 | { |
||
| 83 | return current($this->elements); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function contains($element) |
||
| 87 | { |
||
| 88 | return in_array($element, $this->elements, true); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | public function next() |
||
| 97 | } |
||
| 98 | |||
| 99 | public function last() |
||
| 100 | { |
||
| 101 | return end($this->elements); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function first() |
||
| 105 | { |
||
| 106 | return reset($this->elements); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function key() |
||
| 110 | { |
||
| 111 | return key($this->elements); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function valid() |
||
| 115 | { |
||
| 116 | return $this->offsetExists($this->elements); |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | public function offsetGet($offset) |
||
| 121 | { |
||
| 122 | return $this->get($offset); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function offsetSet($offset, $value) |
||
| 126 | { |
||
| 127 | if (!isset($offset)) { |
||
| 128 | $this->set($offset, $value); |
||
| 129 | } |
||
| 130 | $this->set($offset, $value); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function offsetUnset($offset) |
||
| 134 | { |
||
| 135 | $this->remove($offset); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function containsKey($key) |
||
| 139 | { |
||
| 140 | return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function offsetExists($offset) |
||
| 144 | { |
||
| 145 | return $this->containsKey($offset); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function remove($key) |
||
| 149 | { |
||
| 150 | if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) { |
||
| 151 | return false; |
||
| 152 | } else { |
||
| 153 | $removed = $this->elements[$key]; |
||
| 154 | unset($this->elements[$key]); |
||
| 155 | |||
| 156 | return $removed; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | public function removeEleme($element) |
||
| 161 | { |
||
| 162 | $key = array_search($element, $this->elements, true); |
||
| 163 | if (false === $key) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | unset($this->elements[$key]); |
||
| 167 | return true; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function add($value) |
||
| 171 | { |
||
| 172 | $this->elements[] = $value; |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function set($key, $value) |
||
| 178 | { |
||
| 179 | $this->elements[$key] = $value; |
||
| 180 | |||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function ksort() |
||
| 185 | { |
||
| 186 | |||
| 187 | return ksort($this->elements); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function natSort() |
||
| 191 | { |
||
| 192 | return natsort($this->elements); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function natcasesort() |
||
| 196 | { |
||
| 197 | return natcasesort($this->elements); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function exists(Closure $p) |
||
| 201 | { |
||
| 202 | foreach ($this->elements as $key => $element) { |
||
| 203 | if ($p($key, $element)) { |
||
| 204 | return true; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return int|string |
||
| 212 | * return the position of the element |
||
| 213 | */ |
||
| 214 | public function indexOf($element) |
||
| 215 | { |
||
| 216 | return array_search($element, $this->elements); |
||
|
|
|||
| 217 | } |
||
| 218 | |||
| 219 | public function isEmpty() |
||
| 220 | { |
||
| 221 | return empty($this->elements); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function getValues() |
||
| 228 | } |
||
| 229 | |||
| 230 | public function getKey() |
||
| 231 | { |
||
| 232 | return array_keys($this->elements); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function get($key) |
||
| 236 | { |
||
| 237 | return isset($this->elements[$key]) ? $this->elements[$key] : null; |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | public function slice($start, $end) |
||
| 242 | { |
||
| 243 | if ($start < 0 || !is_int($start)) { |
||
| 244 | throw new \InvalidArgumentException("Start must be a no-negative integer"); |
||
| 245 | } |
||
| 246 | |||
| 247 | if ($end < 0 || !is_int($end)) { |
||
| 248 | throw new \InvalidArgumentException("End must be a positive integer"); |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($start > $end) { |
||
| 252 | throw new \InvalidArgumentException("End must be geater than start"); |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($end > $this->count() + 1) { |
||
| 256 | throw new \InvalidArgumentException("End must be less than the count of the items in the Collection"); |
||
| 267 | |||
| 268 | } |
||
| 269 | |||
| 270 | public function reverse() |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | public function find($value) |
||
| 288 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.