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 |
||
| 50 | class Sequence implements |
||
| 51 | Sequenceable, |
||
| 52 | ArrayAccess, |
||
| 53 | Immutable, |
||
| 54 | Countable, |
||
| 55 | Arrayable, |
||
| 56 | Invokable, |
||
| 57 | Iterator |
||
| 58 | { |
||
| 59 | use IsImmutable, |
||
| 60 | IsContainer, |
||
| 61 | IsArrayable, |
||
| 62 | IsSerializable; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Delimiter used to fetch slices. |
||
| 66 | */ |
||
| 67 | const SLICE_DELIM = ':'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Fixed-size data storage array. |
||
| 71 | * |
||
| 72 | * @var SplFixedArray |
||
| 73 | */ |
||
| 74 | private $data; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sequence constructor. |
||
| 78 | * |
||
| 79 | * @param array|Traversable $data The data to sequence |
||
|
|
|||
| 80 | */ |
||
| 81 | public function __construct($data = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Invoke sequence. |
||
| 91 | |||
| 92 | * A sequence is invokable as if it were a function. This allows some pretty useful functionality such as negative |
||
| 93 | * indexing, sub-sequence selection, etc. Basically, any way you invoke a sequence, you're going to get back either |
||
| 94 | * a single value from the sequence or a subset of it. |
||
| 95 | |||
| 96 | * @internal param mixed $funk Either a numerical offset (positive or negative), a range string (start:end), or a |
||
| 97 | * callback to be used as a filter. |
||
| 98 | |||
| 99 | * @return mixed |
||
| 100 | |||
| 101 | * @todo Put all the slice logic into a helper function or several |
||
| 102 | */ |
||
| 103 | public function __invoke() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set data in sequence. |
||
| 129 | * |
||
| 130 | * Any array or traversable structure passed in will be re-indexed numerically. |
||
| 131 | * |
||
| 132 | * @param Traversable|array $data The sequence data |
||
| 133 | */ |
||
| 134 | private function setData($data) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get data. |
||
| 149 | * |
||
| 150 | * Get the underlying data array. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function getData() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return the current element. |
||
| 161 | * |
||
| 162 | * @return mixed |
||
| 163 | */ |
||
| 164 | public function current() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Move forward to next element. |
||
| 171 | */ |
||
| 172 | public function next() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return the key of the current element. |
||
| 179 | * |
||
| 180 | * @return mixed|null |
||
| 181 | */ |
||
| 182 | public function key() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Checks if current position is valid. |
||
| 189 | * |
||
| 190 | * @return boolean |
||
| 191 | */ |
||
| 192 | public function valid() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Rewind the Iterator to the first element. |
||
| 199 | */ |
||
| 200 | public function rewind() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Count elements of an object. |
||
| 207 | * |
||
| 208 | * @return int The custom count as an integer. |
||
| 209 | */ |
||
| 210 | public function count() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get item at collection. |
||
| 217 | * |
||
| 218 | * This method functions as the ArrayAccess getter. Depending on whether an int, a negative int, or a string is passed, this |
||
| 219 | * |
||
| 220 | * @param int|string $offset Offset (index) to retrieve |
||
| 221 | * |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function offsetGet($offset) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set offset. |
||
| 237 | * |
||
| 238 | * Because Sequence is immutable, this operation is not allowed. Use set() instead. |
||
| 239 | * |
||
| 240 | * @param int $offset Numeric offset |
||
| 241 | * @param mixed $value Value |
||
| 242 | */ |
||
| 243 | public function offsetSet($offset, $value) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set value at given offset. |
||
| 253 | * |
||
| 254 | * Creates a copy of the sequence, setting the specified offset to the specified value (on the copy), and returns it. |
||
| 255 | * |
||
| 256 | * @param mixed $offset The index offset to set |
||
| 257 | * @param mixed $value The value to set it to |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function set($offset, $value) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * |
||
| 270 | * Because Sequence is immutable, this operation is not allowed. Use set() instead. |
||
| 271 | * |
||
| 272 | * @param int $offset Numeric offset |
||
| 273 | */ |
||
| 274 | public function offsetUnset($offset) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get new sequence without specified indices. |
||
| 284 | * |
||
| 285 | * Creates a copy of the sequence, unsetting the specified offset(s) (on the copy), and returns it. |
||
| 286 | * |
||
| 287 | * @param int|string|array The offset, range, or set of indices to remove. |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function except($offset) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Is there a value at specified offset? |
||
| 308 | * |
||
| 309 | * Returns true of there is an item in the collection at the specified numerical offset. |
||
| 310 | * |
||
| 311 | * @param mixed $offset The index offset to check |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function offsetExists($offset) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get diff by index. |
||
| 325 | * |
||
| 326 | * @param array|Traversable$data The array/traversable |
||
| 327 | * |
||
| 328 | * @return static |
||
| 329 | */ |
||
| 330 | public function diffKeys($data) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get diff by value. |
||
| 343 | * |
||
| 344 | * @param array|Traversable$data The array/traversable |
||
| 345 | * |
||
| 346 | * @return static |
||
| 347 | */ |
||
| 348 | public function diff($data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Prepend item to collection. |
||
| 361 | * |
||
| 362 | * Prepend an item to this collection (in place). |
||
| 363 | * |
||
| 364 | * @param mixed $item Item to prepend to collection |
||
| 365 | * |
||
| 366 | * @return Sequence |
||
| 367 | */ |
||
| 368 | public function prepend($item) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Append item to collection. |
||
| 377 | * |
||
| 378 | * Append an item to this collection (in place). |
||
| 379 | * |
||
| 380 | * @param mixed $item Item to append to collection |
||
| 381 | * |
||
| 382 | * @return Sequence |
||
| 383 | */ |
||
| 384 | public function append($item) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Fold (reduce) sequence into a single value. |
||
| 393 | * |
||
| 394 | * @param callable $funk A callback function |
||
| 395 | * @param mixed $initial Initial value for accumulator |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function fold(callable $funk, $initial = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Is collection empty? |
||
| 410 | * |
||
| 411 | * You may optionally pass in a callback which will determine if each of the items within the collection are empty. |
||
| 412 | * If all items in the collection are empty according to this callback, this method will return true. |
||
| 413 | * |
||
| 414 | * @param callable $funk The callback |
||
| 415 | * |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function isEmpty(callable $funk = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Pipe collection through callback. |
||
| 430 | * |
||
| 431 | * Passes entire collection to provided callback and returns the result. |
||
| 432 | * |
||
| 433 | * @param callable $funk The callback funkshun |
||
| 434 | * |
||
| 435 | * @return mixed |
||
| 436 | */ |
||
| 437 | public function pipe(callable $funk) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Does every item return true? |
||
| 444 | * |
||
| 445 | * If callback is provided, this method will return true if all items in collection cause callback to return true. |
||
| 446 | * Otherwise, it will return true if all items in the collection have a truthy value. |
||
| 447 | * |
||
| 448 | * @param callable|null $funk The callback |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function every(callable $funk = null) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Does every item return false? |
||
| 467 | * |
||
| 468 | * This method is the exact opposite of "all". |
||
| 469 | * |
||
| 470 | * @param callable|null $funk The callback |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function none(callable $funk = null) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get first item. |
||
| 489 | * |
||
| 490 | * Retrieve the first item in the collection or, if a callback is provided, return the first item that, when passed |
||
| 491 | * to the callback, returns true. |
||
| 492 | * |
||
| 493 | * @param callable|null $funk The callback function |
||
| 494 | * @param null $default The default value |
||
| 495 | * |
||
| 496 | * @return mixed |
||
| 497 | */ |
||
| 498 | public function first(callable $funk = null, $default = null) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Get last item. |
||
| 513 | * |
||
| 514 | * Retrieve the last item in the collection or, if a callback is provided, return the last item that, when passed |
||
| 515 | * to the callback, returns true. |
||
| 516 | * |
||
| 517 | * @param callable|null $funk The callback function |
||
| 518 | * @param null $default The default value |
||
| 519 | * |
||
| 520 | * @return mixed |
||
| 521 | */ |
||
| 522 | public function last(callable $funk = null, $default = null) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get sequence in reverse order. |
||
| 529 | * |
||
| 530 | * @return Sequenceable |
||
| 531 | */ |
||
| 532 | public function reverse() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Return new sequence with the first item "bumped" off. |
||
| 539 | * |
||
| 540 | * @return Sequenceable |
||
| 541 | */ |
||
| 542 | public function bump() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Return new sequence with the last item "dropped" off. |
||
| 551 | * |
||
| 552 | * @return Sequenceable |
||
| 553 | */ |
||
| 554 | public function drop() |
||
| 560 | |||
| 561 | public function unserialize($serialized) |
||
| 565 | } |
||
| 566 |
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.