| Total Complexity | 48 |
| Total Lines | 308 |
| 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 | // next implementation |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @inheritDoc |
||
| 128 | */ |
||
| 129 | public function last() |
||
| 130 | { |
||
| 131 | if ($this->isEmpty()) { |
||
| 132 | throw new UnderflowException(); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this[count($this) - 1]; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritDoc |
||
| 140 | */ |
||
| 141 | public function pop() |
||
| 142 | { |
||
| 143 | if ($this->isEmpty()) { |
||
| 144 | throw new UnderflowException(); |
||
| 145 | } |
||
| 146 | |||
| 147 | // next impplementation |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Pushes all values of either an array or traversable object. |
||
| 152 | */ |
||
| 153 | private function pushAll($values) |
||
| 154 | { |
||
| 155 | foreach ($values as $value) { |
||
| 156 | $this[] = $value; |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->checkCapacity(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritDoc |
||
| 164 | */ |
||
| 165 | public function push(...$values) |
||
| 166 | { |
||
| 167 | $this->pushAll($values); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritDoc |
||
| 172 | */ |
||
| 173 | public function map(callable $callback): SequenceableInterface |
||
| 174 | { |
||
| 175 | $count = count($this); |
||
| 176 | $sfa = new SplFixedArray($count); |
||
| 177 | |||
| 178 | for ($i = 0; $i < $count; $i++) { |
||
| 179 | $sfa[$i] = $callback($this[$i], $i, $this); |
||
| 180 | } |
||
| 181 | |||
| 182 | return new static($sfa); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @inheritDoc |
||
| 187 | */ |
||
| 188 | public function walk(callable $callback): SequenceableInterface |
||
| 189 | { |
||
| 190 | foreach ($this as $i => $elem) { |
||
| 191 | $callback($elem, $i, $this); |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritDoc |
||
| 199 | */ |
||
| 200 | public function filter(callable $callback): SequenceableInterface |
||
| 201 | { |
||
| 202 | $count = count($this); |
||
| 203 | $sfa = new SplFixedArray($count); |
||
| 204 | $newCount = 0; |
||
| 205 | |||
| 206 | foreach ($this as $elem) { |
||
| 207 | if ($callback($elem)) { |
||
| 208 | $sfa[$newCount++] = $elem; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | $sfa->setSize($newCount); |
||
| 213 | return new static($sfa); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritDoc |
||
| 218 | */ |
||
| 219 | public function reduce(callable $callback, $accumulator = null) |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritDoc |
||
| 230 | */ |
||
| 231 | public function join(string $token = ',', string $secondToken = null): string |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @inheritDoc |
||
| 254 | */ |
||
| 255 | public function slice(int $begin = 0, int $end = null): SequenceableInterface |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @inheritDoc |
||
| 263 | */ |
||
| 264 | public function concat(): SequenceableInterface |
||
| 265 | { |
||
| 266 | $args = func_get_args(); |
||
| 267 | array_unshift($args, $this); |
||
| 268 | |||
| 269 | // Concat this iterator, and variadic args |
||
| 270 | $class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
||
| 271 | $concatIt = $class->newInstanceArgs($args); |
||
| 272 | |||
| 273 | // Create as new immutable's iterator |
||
| 274 | return new static($concatIt); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @inheritDoc |
||
| 279 | */ |
||
| 280 | public function find(callable $callback) |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @inheritDoc |
||
| 291 | */ |
||
| 292 | public function sort(callable $callback = null): SequenceableInterface |
||
| 293 | { |
||
| 294 | if ($callback) { |
||
| 295 | return $this->mergeSort($callback); |
||
| 296 | } |
||
| 297 | |||
| 298 | return $this->arraySort(); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @inheritDoc |
||
| 303 | */ |
||
| 304 | public function sorted(callable $callback = null): SequenceableInterface |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @inheritDoc |
||
| 319 | */ |
||
| 320 | public function heapSorted(SplHeap $heap): SequenceableInterface |
||
| 321 | { |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @inheritDoc |
||
| 327 | */ |
||
| 328 | public function heapSort(SplHeap $heap): SequenceableInterface |
||
| 337 | } |
||
| 338 | |||
| 339 | abstract protected function setTraversable(Traversable $traversable); |
||
| 340 | } |
||
| 341 |