| Total Complexity | 46 |
| Total Lines | 294 |
| 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 | use Collection; |
||
| 35 | use Sort { |
||
| 36 | Sort::heapSort as heapSortWithCallback; |
||
| 37 | Sort::heapSorted as heapSortedWithCallback; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritDoc |
||
| 42 | */ |
||
| 43 | public static function fromItems(Traversable $array): SequenceableInterface |
||
| 44 | { |
||
| 45 | // We can only do it this way if we can count it |
||
| 46 | if ($array instanceof Countable) { |
||
| 47 | $sfa = new SplFixedArray(count($array)); |
||
| 48 | |||
| 49 | foreach ($array as $i => $elem) { |
||
| 50 | $sfa[$i] = $elem; |
||
| 51 | } |
||
| 52 | |||
| 53 | return new static($sfa); |
||
| 54 | } |
||
| 55 | |||
| 56 | // If we can't count it, it's simplest to iterate into an array first |
||
| 57 | return static::fromArray(iterator_to_array($array)); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritDoc |
||
| 62 | */ |
||
| 63 | public static function fromArray(array $array): SequenceableInterface |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritDoc |
||
| 70 | */ |
||
| 71 | public function copy(): CollectionInterface |
||
| 72 | { |
||
| 73 | return static::fromArray($this->toArray()); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | public function contains(...$values): bool |
||
| 80 | { |
||
| 81 | foreach ($values as $value) { |
||
| 82 | if (!$this->find($value)) { |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return true; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritDoc |
||
| 92 | */ |
||
| 93 | public function first() |
||
| 94 | { |
||
| 95 | if ($this->isEmpty()) { |
||
| 96 | throw new UnderflowException(); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this[0]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritDoc |
||
| 104 | */ |
||
| 105 | public function get(int $index) |
||
| 106 | { |
||
| 107 | if (! $this->validIndex($index)) { |
||
| 108 | throw new OutOfRangeException(); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $this[$index]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @inheritDoc |
||
| 116 | */ |
||
| 117 | public function insert(int $index, ...$values) |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritDoc |
||
| 126 | */ |
||
| 127 | public function last() |
||
| 128 | { |
||
| 129 | if ($this->isEmpty()) { |
||
| 130 | throw new UnderflowException(); |
||
| 131 | } |
||
| 132 | |||
| 133 | return $this[count($this) - 1]; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Pushes all values of either an array or traversable object. |
||
| 138 | */ |
||
| 139 | private function pushAll($values) |
||
| 140 | { |
||
| 141 | foreach ($values as $value) { |
||
| 142 | $this[] = $value; |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->checkCapacity(); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritDoc |
||
| 150 | */ |
||
| 151 | public function push(...$values) |
||
| 152 | { |
||
| 153 | $this->pushAll($values); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritDoc |
||
| 158 | */ |
||
| 159 | public function map(callable $callback): SequenceableInterface |
||
| 160 | { |
||
| 161 | $count = count($this); |
||
| 162 | $sfa = new SplFixedArray($count); |
||
| 163 | |||
| 164 | for ($i = 0; $i < $count; $i++) { |
||
| 165 | $sfa[$i] = $callback($this[$i], $i, $this); |
||
| 166 | } |
||
| 167 | |||
| 168 | return new static($sfa); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritDoc |
||
| 173 | */ |
||
| 174 | public function walk(callable $callback): SequenceableInterface |
||
| 175 | { |
||
| 176 | foreach ($this as $i => $elem) { |
||
| 177 | $callback($elem, $i, $this); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritDoc |
||
| 185 | */ |
||
| 186 | public function filter(callable $callback): SequenceableInterface |
||
| 187 | { |
||
| 188 | $count = count($this); |
||
| 189 | $sfa = new SplFixedArray($count); |
||
| 190 | $newCount = 0; |
||
| 191 | |||
| 192 | foreach ($this as $elem) { |
||
| 193 | if ($callback($elem)) { |
||
| 194 | $sfa[$newCount++] = $elem; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $sfa->setSize($newCount); |
||
| 199 | return new static($sfa); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | public function reduce(callable $callback, $accumulator = null) |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritDoc |
||
| 216 | */ |
||
| 217 | public function join(string $token = ',', string $secondToken = null): string |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @inheritDoc |
||
| 240 | */ |
||
| 241 | public function slice(int $begin = 0, int $end = null): SequenceableInterface |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritDoc |
||
| 249 | */ |
||
| 250 | public function concat(): SequenceableInterface |
||
| 251 | { |
||
| 252 | $args = func_get_args(); |
||
| 253 | array_unshift($args, $this); |
||
| 254 | |||
| 255 | // Concat this iterator, and variadic args |
||
| 256 | $class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
||
| 257 | $concatIt = $class->newInstanceArgs($args); |
||
| 258 | |||
| 259 | // Create as new immutable's iterator |
||
| 260 | return new static($concatIt); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritDoc |
||
| 265 | */ |
||
| 266 | public function find(callable $callback) |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritDoc |
||
| 277 | */ |
||
| 278 | public function sort(callable $callback = null): SequenceableInterface |
||
| 279 | { |
||
| 280 | if ($callback) { |
||
| 281 | return $this->mergeSort($callback); |
||
| 282 | } |
||
| 283 | |||
| 284 | return $this->arraySort(); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @inheritDoc |
||
| 289 | */ |
||
| 290 | public function sorted(callable $callback = null): SequenceableInterface |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritDoc |
||
| 305 | */ |
||
| 306 | public function heapSorted(SplHeap $heap): SequenceableInterface |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @inheritDoc |
||
| 313 | */ |
||
| 314 | public function heapSort(SplHeap $heap): SequenceableInterface |
||
| 323 | } |
||
| 324 | |||
| 325 | abstract protected function setTraversable(Traversable $traversable); |
||
| 326 | } |
||
| 327 |