| Total Complexity | 43 |
| Total Lines | 220 |
| 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 Functional; |
||
| 36 | |||
| 37 | |||
| 38 | protected $sfa = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create an fixed array |
||
| 42 | * |
||
| 43 | * @param Traversable $array data |
||
| 44 | */ |
||
| 45 | protected function __construct(Traversable $array) |
||
| 46 | { |
||
| 47 | $this->sfa = $array; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function toArray(): array |
||
| 51 | { |
||
| 52 | return $this->sfa->toArray(); |
||
| 53 | } |
||
| 54 | |||
| 55 | protected function validIndex(int $index) |
||
| 56 | { |
||
| 57 | return $index >= 0 && $index < count($this); |
||
|
|
|||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Countable |
||
| 62 | */ |
||
| 63 | public function count(): int |
||
| 64 | { |
||
| 65 | return count($this->sfa); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Iterator |
||
| 70 | */ |
||
| 71 | public function current() |
||
| 72 | { |
||
| 73 | return $this->sfa->current(); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function key(): int |
||
| 77 | { |
||
| 78 | return $this->sfa->key(); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function next() |
||
| 82 | { |
||
| 83 | return $this->sfa->next(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function rewind() |
||
| 87 | { |
||
| 88 | return $this->sfa->rewind(); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function valid() |
||
| 92 | { |
||
| 93 | return $this->sfa->valid(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * ArrayAccess |
||
| 98 | */ |
||
| 99 | public function offsetExists($offset): bool |
||
| 100 | { |
||
| 101 | return is_integer($offset) |
||
| 102 | && $this->validIndex($offset) |
||
| 103 | && $this->sfa->offsetExists($offset); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function offsetGet($offset) |
||
| 107 | { |
||
| 108 | return $this->sfa->offsetGet($offset); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function offsetSet($offset, $value) |
||
| 112 | { |
||
| 113 | return is_integer($offset) |
||
| 114 | && $this->validIndex($offset) |
||
| 115 | && $this->sfa->offsetSet($offset, $value); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function offsetUnset($offset) |
||
| 119 | { |
||
| 120 | return is_integer($offset) |
||
| 121 | && $this->validIndex($offset) |
||
| 122 | && $this->sfa->offsetUnset($offset); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function clear() |
||
| 126 | { |
||
| 127 | return $this->sfa->clear(); |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function getMainTraversable(): Traversable |
||
| 131 | { |
||
| 132 | return $this->sfa; |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function setTraversable(Traversable $traversable) |
||
| 136 | { |
||
| 137 | $this->sfa = $traversable; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | */ |
||
| 143 | public static function fromItems(Traversable $array) |
||
| 144 | { |
||
| 145 | // We can only do it this way if we can count it |
||
| 146 | if ($array instanceof Countable) { |
||
| 147 | $sfa = new SplFixedArray(count($array)); |
||
| 148 | |||
| 149 | foreach ($array as $i => $elem) { |
||
| 150 | $sfa[$i] = $elem; |
||
| 151 | } |
||
| 152 | |||
| 153 | return new static($sfa); |
||
| 154 | } |
||
| 155 | |||
| 156 | // If we can't count it, it's simplest to iterate into an array first |
||
| 157 | return static::fromArray(iterator_to_array($array)); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @inheritDoc |
||
| 162 | */ |
||
| 163 | public static function fromArray(array $array) |
||
| 164 | { |
||
| 165 | return new static(SplFixedArray::fromArray($array)); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritDoc |
||
| 170 | */ |
||
| 171 | public function copy() |
||
| 172 | { |
||
| 173 | return static::fromArray($this->toArray()); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritDoc |
||
| 178 | */ |
||
| 179 | public function contains(...$values): bool |
||
| 180 | { |
||
| 181 | foreach ($values as $value) { |
||
| 182 | if (!$this->find($value)) { |
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return true; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @inheritDoc |
||
| 192 | */ |
||
| 193 | public function first() |
||
| 194 | { |
||
| 195 | if ($this->isEmpty()) { |
||
| 196 | throw new UnderflowException(); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $this[0]; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | public function get(int $index) |
||
| 206 | { |
||
| 207 | if (! $this->validIndex($index)) { |
||
| 208 | throw new OutOfRangeException(); |
||
| 209 | } |
||
| 210 | |||
| 211 | return $this[$index]; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritDoc |
||
| 216 | */ |
||
| 217 | public function insert(int $index, ...$values) |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritDoc |
||
| 226 | */ |
||
| 227 | public function last() |
||
| 228 | { |
||
| 229 | if ($this->isEmpty()) { |
||
| 230 | throw new UnderflowException(); |
||
| 231 | } |
||
| 232 | |||
| 233 | return $this[count($this) - 1]; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Pushes all values of either an array or traversable object. |
||
| 238 | */ |
||
| 239 | private function pushAll($values) |
||
| 240 | { |
||
| 241 | foreach ($values as $value) { |
||
| 242 | $this[] = $value; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritDoc |
||
| 248 | */ |
||
| 249 | public function push(...$values) |
||
| 252 | } |
||
| 253 | } |
||
| 254 |