Complex classes like Sequence 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Sequence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Sequence implements |
||
48 | Sequenceable, |
||
49 | ArrayAccess, |
||
50 | Immutable, |
||
51 | Countable, |
||
52 | Arrayable, |
||
53 | Invokable, |
||
54 | Iterator |
||
55 | { |
||
56 | use IsImmutable, IsContainer, IsArrayable; |
||
57 | |||
58 | /** |
||
59 | * Delimiter used to fetch slices. |
||
60 | */ |
||
61 | const SLICE_DELIM = ':'; |
||
62 | |||
63 | /** |
||
64 | * Fixed-size data storage array. |
||
65 | * |
||
66 | * @var SplFixedArray |
||
67 | */ |
||
68 | private $data; |
||
69 | |||
70 | /** |
||
71 | * Sequence constructor. |
||
72 | * |
||
73 | * @param array|Traversable $data The data to sequence |
||
|
|||
74 | */ |
||
75 | public function __construct($data = null) |
||
82 | |||
83 | /** |
||
84 | * Invoke sequence. |
||
85 | * |
||
86 | * A sequence is invokable as if it were a function. This allows some pretty useful functionality such as negative |
||
87 | * indexing, sub-sequence selection, etc. |
||
88 | * |
||
89 | * @param int $offset The offset to return |
||
90 | * |
||
91 | * @return mixed |
||
92 | * |
||
93 | * @todo Put all the slice logic into a helper function or several |
||
94 | */ |
||
95 | public function __invoke($offset = null) |
||
112 | |||
113 | /** |
||
114 | * Set data in sequence. |
||
115 | * |
||
116 | * Any array or traversable structure passed in will be re-indexed numerically. |
||
117 | * |
||
118 | * @param Traversable|array $data The sequence data |
||
119 | */ |
||
120 | private function setData($data) |
||
133 | |||
134 | /** |
||
135 | * Get data. |
||
136 | * |
||
137 | * Get the underlying data array. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | protected function getData() |
||
145 | |||
146 | /** |
||
147 | * Return the current element. |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function current() |
||
155 | |||
156 | /** |
||
157 | * Move forward to next element. |
||
158 | */ |
||
159 | public function next() |
||
163 | |||
164 | /** |
||
165 | * Return the key of the current element. |
||
166 | * |
||
167 | * @return mixed|null |
||
168 | */ |
||
169 | public function key() |
||
173 | |||
174 | /** |
||
175 | * Checks if current position is valid. |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | public function valid() |
||
183 | |||
184 | /** |
||
185 | * Rewind the Iterator to the first element. |
||
186 | */ |
||
187 | public function rewind() |
||
191 | |||
192 | /** |
||
193 | * Count elements of an object. |
||
194 | * |
||
195 | * @return int The custom count as an integer. |
||
196 | */ |
||
197 | public function count() |
||
201 | |||
202 | /** |
||
203 | * Get item at collection. |
||
204 | * |
||
205 | * This method functions as the ArrayAccess getter. Depending on whether an int, a negative int, or a string is passed, this |
||
206 | * |
||
207 | * @param int|string $offset Offset (index) to retrieve |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function offsetGet($offset) |
||
221 | |||
222 | /** |
||
223 | * Set offset. |
||
224 | * |
||
225 | * Because Sequence is immutable, this operation is not allowed. Use set() instead. |
||
226 | * |
||
227 | * @param int $offset Numeric offset |
||
228 | * @param mixed $value Value |
||
229 | */ |
||
230 | public function offsetSet($offset, $value) |
||
237 | |||
238 | /** |
||
239 | * Set value at given offset. |
||
240 | * |
||
241 | * Creates a copy of the sequence, setting the specified offset to the specified value (on the copy), and returns it. |
||
242 | * |
||
243 | * @param mixed $offset The index offset to set |
||
244 | * @param mixed $value The value to set it to |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function set($offset, $value) |
||
254 | |||
255 | /** |
||
256 | * |
||
257 | * Because Sequence is immutable, this operation is not allowed. Use set() instead. |
||
258 | * |
||
259 | * @param int $offset Numeric offset |
||
260 | */ |
||
261 | public function offsetUnset($offset) |
||
268 | |||
269 | /** |
||
270 | * Get new sequence without specified indices. |
||
271 | * |
||
272 | * Creates a copy of the sequence, unsetting the specified offset(s) (on the copy), and returns it. |
||
273 | * |
||
274 | * @param int|string|array The offset, range, or set of indices to remove. |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function except($offset) |
||
292 | |||
293 | /** |
||
294 | * Is there a value at specified offset? |
||
295 | * |
||
296 | * Returns true of there is an item in the collection at the specified numerical offset. |
||
297 | * |
||
298 | * @param mixed $offset The index offset to check |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | public function offsetExists($offset) |
||
306 | |||
307 | /** |
||
308 | * Get diff by index. |
||
309 | * |
||
310 | * @param array|Traversable$data The array/traversable |
||
311 | * |
||
312 | * @return static |
||
313 | */ |
||
314 | public function diffKeys($data) |
||
324 | |||
325 | /** |
||
326 | * Get diff by value. |
||
327 | * |
||
328 | * @param array|Traversable$data The array/traversable |
||
329 | * |
||
330 | * @return static |
||
331 | */ |
||
332 | public function diff($data) |
||
342 | |||
343 | /** |
||
344 | * Prepend item to collection. |
||
345 | * |
||
346 | * Prepend an item to this collection (in place). |
||
347 | * |
||
348 | * @param mixed $item Item to prepend to collection |
||
349 | * |
||
350 | * @return Sequence |
||
351 | */ |
||
352 | public function prepend($item) |
||
358 | |||
359 | /** |
||
360 | * Append item to collection. |
||
361 | * |
||
362 | * Append an item to this collection (in place). |
||
363 | * |
||
364 | * @param mixed $item Item to append to collection |
||
365 | * |
||
366 | * @return Sequence |
||
367 | */ |
||
368 | public function append($item) |
||
374 | |||
375 | /** |
||
376 | * Fold (reduce) sequence into a single value. |
||
377 | * |
||
378 | * @param callable $funk A callback function |
||
379 | * @param mixed $initial Initial value for accumulator |
||
380 | * |
||
381 | * @return mixed |
||
382 | */ |
||
383 | public function fold(callable $funk, $initial = null) |
||
391 | |||
392 | /** |
||
393 | * Is collection empty? |
||
394 | * |
||
395 | * You may optionally pass in a callback which will determine if each of the items within the collection are empty. |
||
396 | * If all items in the collection are empty according to this callback, this method will return true. |
||
397 | * |
||
398 | * @param callable $funk The callback |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function isEmpty(callable $funk = null) |
||
411 | |||
412 | /** |
||
413 | * Pipe collection through callback. |
||
414 | * |
||
415 | * Passes entire collection to provided callback and returns the result. |
||
416 | * |
||
417 | * @param callable $funk The callback funkshun |
||
418 | * |
||
419 | * @return mixed |
||
420 | */ |
||
421 | public function pipe(callable $funk) |
||
425 | |||
426 | /** |
||
427 | * Does every item return true? |
||
428 | * |
||
429 | * If callback is provided, this method will return true if all items in collection cause callback to return true. |
||
430 | * Otherwise, it will return true if all items in the collection have a truthy value. |
||
431 | * |
||
432 | * @param callable|null $funk The callback |
||
433 | * |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function every(callable $funk = null) |
||
448 | |||
449 | /** |
||
450 | * Does every item return false? |
||
451 | * |
||
452 | * This method is the exact opposite of "all". |
||
453 | * |
||
454 | * @param callable|null $funk The callback |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function none(callable $funk = null) |
||
470 | |||
471 | /** |
||
472 | * Get first item. |
||
473 | * |
||
474 | * Retrieve the first item in the collection or, if a callback is provided, return the first item that, when passed |
||
475 | * to the callback, returns true. |
||
476 | * |
||
477 | * @param callable|null $funk The callback function |
||
478 | * @param null $default The default value |
||
479 | * |
||
480 | * @return mixed |
||
481 | */ |
||
482 | public function first(callable $funk = null, $default = null) |
||
494 | |||
495 | /** |
||
496 | * Get last item. |
||
497 | * |
||
498 | * Retrieve the last item in the collection or, if a callback is provided, return the last item that, when passed |
||
499 | * to the callback, returns true. |
||
500 | * |
||
501 | * @param callable|null $funk The callback function |
||
502 | * @param null $default The default value |
||
503 | * |
||
504 | * @return mixed |
||
505 | */ |
||
506 | public function last(callable $funk = null, $default = null) |
||
510 | |||
511 | /** |
||
512 | * Get sequence in reverse order. |
||
513 | * |
||
514 | * @return Sequenceable |
||
515 | */ |
||
516 | public function reverse() |
||
520 | |||
521 | /** |
||
522 | * Return new sequence with the first item "bumped" off. |
||
523 | * |
||
524 | * @return Sequenceable |
||
525 | */ |
||
526 | public function bump() |
||
532 | |||
533 | /** |
||
534 | * Return new sequence with the last item "dropped" off. |
||
535 | * |
||
536 | * @return Sequenceable |
||
537 | */ |
||
538 | public function drop() |
||
544 | } |
||
545 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.