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 |
||
| 37 | class Sequence implements |
||
| 38 | Sequenceable, |
||
| 39 | ArrayAccess, |
||
| 40 | Immutable, |
||
| 41 | Countable, |
||
| 42 | Arrayable, |
||
| 43 | Invokable, |
||
| 44 | Iterator |
||
| 45 | { |
||
| 46 | use IsImmutable, IsContainer, IsArrayable; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Delimiter used to fetch slices. |
||
| 50 | */ |
||
| 51 | const SLICE_DELIM = ':'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Fixed-size data storage array. |
||
| 55 | * |
||
| 56 | * @var SplFixedArray |
||
| 57 | */ |
||
| 58 | private $data; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Sequence constructor. |
||
| 62 | * |
||
| 63 | * @param array|Traversable $data The data to sequence |
||
|
|
|||
| 64 | */ |
||
| 65 | public function __construct($data = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Invoke sequence. |
||
| 75 | * A sequence is invokable as if it were a function. This allows some pretty useful functionality such as negative |
||
| 76 | * indexing, sub-sequence selection, etc. |
||
| 77 | * |
||
| 78 | * @internal param int $offset The offset to return |
||
| 79 | * |
||
| 80 | * @return mixed |
||
| 81 | * |
||
| 82 | * @todo Put all the slice logic into a helper function or several |
||
| 83 | */ |
||
| 84 | public function __invoke() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set data in sequence. |
||
| 122 | * |
||
| 123 | * Any array or traversable structure passed in will be re-indexed numerically. |
||
| 124 | * |
||
| 125 | * @param Traversable|array $data The sequence data |
||
| 126 | */ |
||
| 127 | private function setData($data) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get data. |
||
| 143 | * |
||
| 144 | * Get the underlying data array. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | protected function getData() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return the current element |
||
| 155 | * @link http://php.net/manual/en/iterator.current.php |
||
| 156 | * @return mixed Can return any type. |
||
| 157 | * @since 5.0.0 |
||
| 158 | */ |
||
| 159 | public function current() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Move forward to next element |
||
| 166 | * @link http://php.net/manual/en/iterator.next.php |
||
| 167 | * @return void Any returned value is ignored. |
||
| 168 | * @since 5.0.0 |
||
| 169 | */ |
||
| 170 | public function next() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the key of the current element |
||
| 177 | * @link http://php.net/manual/en/iterator.key.php |
||
| 178 | * @return mixed scalar on success, or null on failure. |
||
| 179 | * @since 5.0.0 |
||
| 180 | */ |
||
| 181 | public function key() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Checks if current position is valid |
||
| 188 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 189 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
| 190 | * Returns true on success or false on failure. |
||
| 191 | * @since 5.0.0 |
||
| 192 | */ |
||
| 193 | public function valid() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Rewind the Iterator to the first element |
||
| 200 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 201 | * @return void Any returned value is ignored. |
||
| 202 | * @since 5.0.0 |
||
| 203 | */ |
||
| 204 | public function rewind() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Count elements of an object |
||
| 211 | * @link http://php.net/manual/en/countable.count.php |
||
| 212 | * @return int The custom count as an integer. |
||
| 213 | * </p> |
||
| 214 | * <p> |
||
| 215 | * The return value is cast to an integer. |
||
| 216 | * @since 5.1.0 |
||
| 217 | */ |
||
| 218 | public function count() |
||
| 222 | |||
| 223 | public function offsetGet($offset) |
||
| 237 | |||
| 238 | public function offsetSet($offset, $value) |
||
| 242 | |||
| 243 | public function offsetUnset($offset) |
||
| 247 | |||
| 248 | public function offsetExists($offset) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Prepend item to collection. |
||
| 255 | * Prepend an item to this collection (in place). |
||
| 256 | * @param mixed $item Item to prepend to collection |
||
| 257 | * @return Sequence |
||
| 258 | */ |
||
| 259 | public function prepend($item) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Append item to collection. |
||
| 268 | * Append an item to this collection (in place). |
||
| 269 | * @param mixed $item Item to append to collection |
||
| 270 | * @return Sequence |
||
| 271 | */ |
||
| 272 | public function append($item) |
||
| 278 | |||
| 279 | public function fold(callable $funk, $initial = null) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Is collection empty? |
||
| 290 | * You may optionally pass in a callback which will determine if each of the items within the collection are empty. |
||
| 291 | * If all items in the collection are empty according to this callback, this method will return true. |
||
| 292 | * |
||
| 293 | * @param callable $funk The callback |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function isEmpty(callable $funk = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Pipe collection through callback. |
||
| 309 | * Passes entire collection to provided callback and returns the result. |
||
| 310 | * @param callable $funk The callback funkshun |
||
| 311 | * @return mixed |
||
| 312 | */ |
||
| 313 | public function pipe(callable $funk) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Does every item return true? |
||
| 320 | * If callback is provided, this method will return true if all items in collection cause callback to return true. |
||
| 321 | * Otherwise, it will return true if all items in the collection have a truthy value. |
||
| 322 | * @param callable|null $funk The callback |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function every(callable $funk = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Does every item return false? |
||
| 340 | * This method is the exact opposite of "all". |
||
| 341 | * @param callable|null $funk The callback |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function none(callable $funk = null) |
||
| 356 | |||
| 357 | public function first(callable $funk = null, $default = null) |
||
| 369 | |||
| 370 | public function last(callable $funk = null, $default = null) |
||
| 374 | |||
| 375 | public function reverse() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Return new sequence with the first item "bumped" off. |
||
| 382 | * @return Sequenceable |
||
| 383 | */ |
||
| 384 | public function bump() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return new sequence with the last item "dropped" off. |
||
| 391 | * @return Sequenceable |
||
| 392 | */ |
||
| 393 | public function drop() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get collection as a sequence. |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function toSeq() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get collection as a dictionary. |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function toDict() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get collection as a set. |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | public function toSet() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get collection as a map. |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function toMap() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get collection as a list. |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | public function toList() |
||
| 442 | } |
This check looks for
@paramannotations 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
arrayand 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.