Total Complexity | 50 |
Total Lines | 284 |
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) |
||
50 | { |
||
51 | return new static(SplFixedArray::fromArray($array)); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @inheritDoc |
||
56 | */ |
||
57 | public static function fromItems(Traversable $array) |
||
58 | { |
||
59 | // We can only do it this way if we can count it |
||
60 | if ($array instanceof Countable) { |
||
61 | $sfa = new SplFixedArray(count($array)); |
||
62 | |||
63 | foreach ($array as $i => $elem) { |
||
64 | $sfa[$i] = $elem; |
||
65 | } |
||
66 | |||
67 | return new static($sfa); |
||
68 | } |
||
69 | |||
70 | // If we can't count it, it's simplest to iterate into an array first |
||
71 | return static::fromArray(iterator_to_array($array)); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | public function contains(...$values): bool |
||
78 | { |
||
79 | foreach ($values as $value) { |
||
80 | if (!$this->find($value)) { |
||
81 | return false; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return true; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function copy() |
||
92 | { |
||
93 | return static::fromArray($this->toArray()); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @inheritDoc |
||
98 | */ |
||
99 | public function first() |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @inheritDoc |
||
110 | */ |
||
111 | public function get(int $index) |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | public function insert(int $index, ...$values) |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @inheritDoc |
||
144 | */ |
||
145 | public function last() |
||
146 | { |
||
147 | if ($this->isEmpty()) { |
||
148 | throw new UnderflowException(); |
||
149 | } |
||
150 | |||
151 | return $this[$this->count() - 1]; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @inheritDoc |
||
156 | */ |
||
157 | public function pop() |
||
158 | { |
||
159 | if ($this->isEmpty()) { |
||
160 | throw new UnderflowException(); |
||
161 | } |
||
162 | |||
163 | $value = $this->last(); |
||
164 | unset($this[$this->count() - 1]); |
||
165 | |||
166 | $this->checkCapacity(); |
||
167 | |||
168 | return $value; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Pushes all values of either an array or traversable object. |
||
173 | */ |
||
174 | private function pushAll($values) |
||
175 | { |
||
176 | $size = $this->getSize(); |
||
177 | |||
178 | foreach ($values as $value) { |
||
179 | $this->setSize(++$size); |
||
180 | $this[$size - 1] = $value; |
||
181 | } |
||
182 | |||
183 | $this->checkCapacity(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | public function push(...$values) |
||
190 | { |
||
191 | $this->pushAll($values); |
||
192 | } |
||
193 | |||
194 | public function toArray(): array |
||
195 | { |
||
196 | return $this->sfa->toArray(); |
||
197 | } |
||
198 | |||
199 | protected function validIndex(int $index) |
||
200 | { |
||
201 | return $index >= 0 && $index < $this->getSize(); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Countable |
||
206 | */ |
||
207 | public function count(): int |
||
208 | { |
||
209 | return count($this->sfa); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Iterator |
||
214 | */ |
||
215 | public function current() |
||
216 | { |
||
217 | return $this->sfa->current(); |
||
218 | } |
||
219 | |||
220 | public function key(): int |
||
221 | { |
||
222 | return $this->sfa->key(); |
||
223 | } |
||
224 | |||
225 | public function next() |
||
226 | { |
||
227 | return $this->sfa->next(); |
||
228 | } |
||
229 | |||
230 | public function rewind() |
||
231 | { |
||
232 | return $this->sfa->rewind(); |
||
233 | } |
||
234 | |||
235 | public function valid() |
||
236 | { |
||
237 | return $this->sfa->valid(); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * ArrayAccess |
||
242 | */ |
||
243 | public function offsetExists($offset): bool |
||
244 | { |
||
245 | return is_integer($offset) |
||
246 | && $this->validIndex($offset) |
||
247 | && $this->sfa->offsetExists($offset); |
||
248 | } |
||
249 | |||
250 | public function offsetGet($offset) |
||
251 | { |
||
252 | return $this->sfa->offsetGet($offset); |
||
253 | } |
||
254 | |||
255 | public function offsetSet($offset, $value) |
||
256 | { |
||
257 | if ($offset === null) { |
||
258 | $this->push($value); |
||
259 | } elseif (is_integer($offset) && $this->validIndex($offset)) { |
||
260 | $this->sfa->offsetSet($offset, $value); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | public function offsetUnset($offset) |
||
265 | { |
||
266 | return is_integer($offset) |
||
267 | && $this->validIndex($offset) |
||
268 | && $this->sfa->offsetUnset($offset); |
||
269 | } |
||
270 | |||
271 | public function clear() |
||
274 | } |
||
275 | |||
276 | protected function getMainTraversable(): Traversable |
||
277 | { |
||
278 | return $this->sfa; |
||
279 | } |
||
280 | |||
281 | protected function setTraversable(Traversable $traversable) |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Gets the size of the array. |
||
288 | * |
||
289 | * @return int |
||
290 | */ |
||
291 | protected function getSize(): int |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Change the size of an array to the new size of size. |
||
298 | * If size is less than the current array size, any values after the |
||
299 | * new size will be discarded. If size is greater than the current |
||
300 | * array size, the array will be padded with NULL values. |
||
301 | * |
||
302 | * @param int $size The new array size. This should be a value between 0 |
||
303 | * and PHP_INT_MAX. |
||
304 | * @return bool Returns TRUE on success or FALSE on failure. |
||
305 | */ |
||
306 | protected function setSize(int $size): bool |
||
309 | } |
||
310 | |||
311 | abstract protected function checkCapacity(); |
||
312 | |||
313 | abstract public function isEmpty(): bool; |
||
314 | |||
315 | abstract public function find(callable $callback); |
||
316 | } |
||
317 |