| Total Complexity | 69 |
| Total Lines | 392 |
| Duplicated Lines | 6.63 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | use Collection; |
||
| 35 | use Sort { |
||
| 36 | Sort::heapSort as heapSortWithCallback; |
||
| 37 | Sort::heapSorted as heapSortedWithCallback; |
||
| 38 | } |
||
| 39 | |||
| 40 | |||
| 41 | protected $sfa = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create an fixed array |
||
| 45 | * |
||
| 46 | * @param Traversable $array data |
||
| 47 | */ |
||
| 48 | protected function __construct(Traversable $array) |
||
| 49 | { |
||
| 50 | $this->sfa = $array; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function toArray(): array |
||
| 54 | { |
||
| 55 | return $this->sfa->toArray(); |
||
| 56 | } |
||
| 57 | |||
| 58 | protected function validIndex(int $index) |
||
| 59 | { |
||
| 60 | return $index >= 0 && $index < count($this); |
||
|
|
|||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Countable |
||
| 65 | */ |
||
| 66 | public function count(): int |
||
| 67 | { |
||
| 68 | return count($this->sfa); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Iterator |
||
| 73 | */ |
||
| 74 | public function current() |
||
| 75 | { |
||
| 76 | return $this->sfa->current(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function key(): int |
||
| 80 | { |
||
| 81 | return $this->sfa->key(); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function next() |
||
| 85 | { |
||
| 86 | return $this->sfa->next(); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function rewind() |
||
| 90 | { |
||
| 91 | return $this->sfa->rewind(); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function valid() |
||
| 95 | { |
||
| 96 | return $this->sfa->valid(); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * ArrayAccess |
||
| 101 | */ |
||
| 102 | public function offsetExists($offset): bool |
||
| 103 | { |
||
| 104 | return is_integer($offset) |
||
| 105 | && $this->validIndex($offset) |
||
| 106 | && $this->sfa->offsetExists($offset); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function offsetGet($offset) |
||
| 110 | { |
||
| 111 | return $this->sfa->offsetGet($offset); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function offsetSet($offset, $value) |
||
| 115 | { |
||
| 116 | return is_integer($offset) |
||
| 117 | && $this->validIndex($offset) |
||
| 118 | && $this->sfa->offsetSet($offset, $value); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function offsetUnset($offset) |
||
| 122 | { |
||
| 123 | return is_integer($offset) |
||
| 124 | && $this->validIndex($offset) |
||
| 125 | && $this->sfa->offsetUnset($offset); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function clear() |
||
| 129 | { |
||
| 130 | return $this->sfa->clear(); |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function getMainTraversable(): Traversable |
||
| 134 | { |
||
| 135 | return $this->sfa; |
||
| 136 | } |
||
| 137 | |||
| 138 | protected function setTraversable(Traversable $traversable) |
||
| 139 | { |
||
| 140 | $this->sfa = $traversable; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @inheritDoc |
||
| 145 | */ |
||
| 146 | public static function fromItems(Traversable $array) |
||
| 147 | { |
||
| 148 | // We can only do it this way if we can count it |
||
| 149 | if ($array instanceof Countable) { |
||
| 150 | $sfa = new SplFixedArray(count($array)); |
||
| 151 | |||
| 152 | foreach ($array as $i => $elem) { |
||
| 153 | $sfa[$i] = $elem; |
||
| 154 | } |
||
| 155 | |||
| 156 | return new static($sfa); |
||
| 157 | } |
||
| 158 | |||
| 159 | // If we can't count it, it's simplest to iterate into an array first |
||
| 160 | return static::fromArray(iterator_to_array($array)); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritDoc |
||
| 165 | */ |
||
| 166 | public static function fromArray(array $array) |
||
| 167 | { |
||
| 168 | return new static(SplFixedArray::fromArray($array)); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritDoc |
||
| 173 | */ |
||
| 174 | public function copy() |
||
| 175 | { |
||
| 176 | return static::fromArray($this->toArray()); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritDoc |
||
| 181 | */ |
||
| 182 | public function contains(...$values): bool |
||
| 183 | { |
||
| 184 | foreach ($values as $value) { |
||
| 185 | if (!$this->find($value)) { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritDoc |
||
| 195 | */ |
||
| 196 | public function first() |
||
| 197 | { |
||
| 198 | if ($this->isEmpty()) { |
||
| 199 | throw new UnderflowException(); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $this[0]; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritDoc |
||
| 207 | */ |
||
| 208 | public function get(int $index) |
||
| 209 | { |
||
| 210 | if (! $this->validIndex($index)) { |
||
| 211 | throw new OutOfRangeException(); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $this[$index]; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @inheritDoc |
||
| 219 | */ |
||
| 220 | public function insert(int $index, ...$values) |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritDoc |
||
| 229 | */ |
||
| 230 | public function last() |
||
| 231 | { |
||
| 232 | if ($this->isEmpty()) { |
||
| 233 | throw new UnderflowException(); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this[count($this) - 1]; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Pushes all values of either an array or traversable object. |
||
| 241 | */ |
||
| 242 | private function pushAll($values) |
||
| 243 | { |
||
| 244 | foreach ($values as $value) { |
||
| 245 | $this[] = $value; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @inheritDoc |
||
| 251 | */ |
||
| 252 | public function push(...$values) |
||
| 253 | { |
||
| 254 | $this->pushAll($values); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritDoc |
||
| 259 | */ |
||
| 260 | View Code Duplication | public function map(callable $callback) |
|
| 261 | { |
||
| 262 | $count = count($this); |
||
| 263 | $sfa = new SplFixedArray($count); |
||
| 264 | |||
| 265 | for ($i = 0; $i < $count; $i++) { |
||
| 266 | $sfa[$i] = $callback($this[$i], $i, $this); |
||
| 267 | } |
||
| 268 | |||
| 269 | return new static($sfa); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @inheritDoc |
||
| 274 | */ |
||
| 275 | public function walk(callable $callback) |
||
| 276 | { |
||
| 277 | foreach ($this as $i => $elem) { |
||
| 278 | $callback($elem, $i, $this); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritDoc |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function filter(callable $callback) |
|
| 288 | { |
||
| 289 | $count = count($this); |
||
| 290 | $sfa = new SplFixedArray($count); |
||
| 291 | $newCount = 0; |
||
| 292 | |||
| 293 | foreach ($this as $elem) { |
||
| 294 | if ($callback($elem)) { |
||
| 295 | $sfa[$newCount++] = $elem; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | $sfa->setSize($newCount); |
||
| 300 | return new static($sfa); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritDoc |
||
| 305 | */ |
||
| 306 | public function reduce(callable $callback, $accumulator = null) |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @inheritDoc |
||
| 317 | */ |
||
| 318 | public function join(string $token = ',', string $secondToken = null): string |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @inheritDoc |
||
| 341 | */ |
||
| 342 | public function slice(int $begin = 0, int $end = null) |
||
| 343 | { |
||
| 344 | $it = new SliceIterator($this, $begin, $end); |
||
| 345 | return new static($it); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @inheritDoc |
||
| 350 | */ |
||
| 351 | public function concat() |
||
| 352 | { |
||
| 353 | $args = func_get_args(); |
||
| 354 | array_unshift($args, $this); |
||
| 355 | |||
| 356 | // Concat this iterator, and variadic args |
||
| 357 | $class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
||
| 358 | $concatIt = $class->newInstanceArgs($args); |
||
| 359 | |||
| 360 | // Create as new immutable's iterator |
||
| 361 | return new static($concatIt); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @inheritDoc |
||
| 366 | */ |
||
| 367 | public function find(callable $callback) |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @inheritDoc |
||
| 378 | */ |
||
| 379 | public function sort(callable $callback = null) |
||
| 380 | { |
||
| 381 | if ($callback) { |
||
| 382 | return $this->mergeSort($callback); |
||
| 383 | } |
||
| 384 | |||
| 385 | return $this->arraySort(); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @inheritDoc |
||
| 390 | */ |
||
| 391 | public function sorted(callable $callback = null) |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritDoc |
||
| 406 | */ |
||
| 407 | public function heapSorted(SplHeap $heap) |
||
| 408 | { |
||
| 409 | return $this->copy()->heapSort($heap); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @inheritDoc |
||
| 414 | */ |
||
| 415 | public function heapSort(SplHeap $heap) |
||
| 416 | { |
||
| 417 | foreach ($this as $item) { |
||
| 418 | $heap->insert($item); |
||
| 424 | } |
||
| 425 | } |
||
| 426 |