| Total Complexity | 52 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Sequenceable 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 Sequenceable, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Mbh\Collection\Traits; |
||
| 32 | trait Sequenceable |
||
| 33 | { |
||
| 34 | protected $sfa = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Create an fixed array |
||
| 38 | * |
||
| 39 | * @param Traversable $array data |
||
| 40 | */ |
||
| 41 | protected function __construct(Traversable $array) |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritDoc |
||
| 48 | */ |
||
| 49 | public static function fromArray(array $array) |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritDoc |
||
| 56 | */ |
||
| 57 | public static function fromItems(Traversable $array) |
||
| 58 | { |
||
| 59 | if (!$array instanceof Countable) { |
||
| 60 | return static::fromArray(iterator_to_array($array)); |
||
| 61 | } |
||
| 62 | |||
| 63 | $sfa = new SplFixedArray(count($array)); |
||
| 64 | |||
| 65 | foreach ($array as $i => $elem) { |
||
| 66 | $sfa[$i] = $elem; |
||
| 67 | } |
||
| 68 | |||
| 69 | return new static($sfa); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritDoc |
||
| 74 | */ |
||
| 75 | public function contains(...$values): bool |
||
| 76 | { |
||
| 77 | foreach ($values as $value) { |
||
| 78 | if ($this->search($value) !== null) { |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | return true; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | public function copy() |
||
| 90 | { |
||
| 91 | return static::fromArray($this->toArray()); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | public function first() |
||
| 98 | { |
||
| 99 | if ($this->isEmpty()) { |
||
| 100 | throw new UnderflowException(); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $this[0]; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritDoc |
||
| 108 | */ |
||
| 109 | public function get(int $index) |
||
| 110 | { |
||
| 111 | if (!$this->validIndex($index)) { |
||
| 112 | throw new OutOfRangeException(); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $this[$index]; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | public function insert(int $index, ...$values) |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritDoc |
||
| 135 | */ |
||
| 136 | public function last() |
||
| 137 | { |
||
| 138 | if ($this->isEmpty()) { |
||
| 139 | throw new UnderflowException(); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this[$this->count() - 1]; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @inheritDoc |
||
| 147 | */ |
||
| 148 | public function pop() |
||
| 149 | { |
||
| 150 | if ($this->isEmpty()) { |
||
| 151 | throw new UnderflowException(); |
||
| 152 | } |
||
| 153 | |||
| 154 | $value = $this->last(); |
||
| 155 | unset($this[$this->count() - 1]); |
||
| 156 | |||
| 157 | $this->checkCapacity(); |
||
| 158 | |||
| 159 | return $value; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Pushes all values of either an array or traversable object. |
||
| 164 | */ |
||
| 165 | private function pushAll(...$args) |
||
| 166 | { |
||
| 167 | $size = $this->getSize(); |
||
| 168 | |||
| 169 | foreach ($args as &$values) { |
||
| 170 | foreach ($values as $value) { |
||
| 171 | $this->setSize(++$size); |
||
| 172 | $this[$size - 1] = $value; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->checkCapacity(); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritDoc |
||
| 181 | */ |
||
| 182 | public function push(...$values) |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritDoc |
||
| 189 | */ |
||
| 190 | public function remove(int $index) |
||
| 191 | { |
||
| 192 | if (! $this->validIndex($index)) { |
||
| 193 | throw new OutOfRangeException(); |
||
| 194 | } |
||
| 195 | $value = array_splice($this->array, $index, 1, null)[0]; |
||
| 196 | $this->checkCapacity(); |
||
| 197 | return $value; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritDoc |
||
| 202 | */ |
||
| 203 | public function set(int $index, $value) |
||
| 204 | { |
||
| 205 | if (! $this->validIndex($index)) { |
||
| 206 | throw new OutOfRangeException(); |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->sfa->offsetSet($index, $value); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function toArray(): array |
||
| 213 | { |
||
| 214 | return $this->sfa->toArray(); |
||
| 215 | } |
||
| 216 | |||
| 217 | protected function validIndex(int $index) |
||
| 218 | { |
||
| 219 | return $index >= 0 && $index < $this->getSize(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Countable |
||
| 224 | */ |
||
| 225 | public function count(): int |
||
| 226 | { |
||
| 227 | return count($this->sfa); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Iterator |
||
| 232 | */ |
||
| 233 | public function current() |
||
| 234 | { |
||
| 235 | return $this->sfa->current(); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function key(): int |
||
| 239 | { |
||
| 240 | return $this->sfa->key(); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function next() |
||
| 244 | { |
||
| 245 | return $this->sfa->next(); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function rewind() |
||
| 249 | { |
||
| 250 | return $this->sfa->rewind(); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function valid() |
||
| 254 | { |
||
| 255 | return $this->sfa->valid(); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * ArrayAccess |
||
| 260 | */ |
||
| 261 | public function offsetExists($offset): bool |
||
| 262 | { |
||
| 263 | return is_integer($offset) |
||
| 264 | && $this->validIndex($offset) |
||
| 265 | && $this->sfa->offsetExists($offset); |
||
| 266 | } |
||
| 267 | |||
| 268 | public function offsetGet($offset) |
||
| 269 | { |
||
| 270 | return $this->sfa->offsetGet($offset); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function offsetSet($offset, $value) |
||
| 274 | { |
||
| 275 | if ($offset === null) { |
||
| 276 | $this->push($value); |
||
| 277 | } elseif (is_integer($offset)) { |
||
| 278 | $this->set($offset, $value); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | public function offsetUnset($offset) |
||
| 283 | { |
||
| 284 | return is_integer($offset) |
||
| 285 | && $this->validIndex($offset) |
||
| 286 | && $this->sfa->offsetUnset($offset); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function clear() |
||
| 292 | } |
||
| 293 | |||
| 294 | protected function getMainTraversable(): Traversable |
||
| 295 | { |
||
| 296 | return $this->sfa; |
||
| 297 | } |
||
| 298 | |||
| 299 | protected function setTraversable(Traversable $traversable) |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Gets the size of the array. |
||
| 306 | * |
||
| 307 | * @return int |
||
| 308 | */ |
||
| 309 | protected function getSize(): int |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Change the size of an array to the new size of size. |
||
| 316 | * If size is less than the current array size, any values after the |
||
| 317 | * new size will be discarded. If size is greater than the current |
||
| 318 | * array size, the array will be padded with NULL values. |
||
| 319 | * |
||
| 320 | * @param int $size The new array size. This should be a value between 0 |
||
| 321 | * and PHP_INT_MAX. |
||
| 322 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 323 | */ |
||
| 324 | protected function setSize(int $size): bool |
||
| 327 | } |
||
| 328 | |||
| 329 | abstract protected function checkCapacity(); |
||
| 330 | |||
| 331 | abstract public function isEmpty(): bool; |
||
| 332 | |||
| 333 | abstract public function search($value); |
||
| 334 | } |
||
| 335 |