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) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get diff by index. |
||
| 312 | * |
||
| 313 | * @param array|Traversable$data The array/traversable |
||
| 314 | * |
||
| 315 | * @return static |
||
| 316 | */ |
||
| 317 | public function diffKeys($data) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get diff by value. |
||
| 330 | * |
||
| 331 | * @param array|Traversable$data The array/traversable |
||
| 332 | * |
||
| 333 | * @return static |
||
| 334 | */ |
||
| 335 | public function diff($data) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Prepend item to collection. |
||
| 348 | * |
||
| 349 | * Prepend an item to this collection (in place). |
||
| 350 | * |
||
| 351 | * @param mixed $item Item to prepend to collection |
||
| 352 | * |
||
| 353 | * @return Sequence |
||
| 354 | */ |
||
| 355 | public function prepend($item) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Append item to collection. |
||
| 364 | * |
||
| 365 | * Append an item to this collection (in place). |
||
| 366 | * |
||
| 367 | * @param mixed $item Item to append to collection |
||
| 368 | * |
||
| 369 | * @return Sequence |
||
| 370 | */ |
||
| 371 | public function append($item) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Fold (reduce) sequence into a single value. |
||
| 380 | * |
||
| 381 | * @param callable $funk A callback function |
||
| 382 | * @param mixed $initial Initial value for accumulator |
||
| 383 | * |
||
| 384 | * @return mixed |
||
| 385 | */ |
||
| 386 | public function fold(callable $funk, $initial = null) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Is collection empty? |
||
| 397 | * |
||
| 398 | * You may optionally pass in a callback which will determine if each of the items within the collection are empty. |
||
| 399 | * If all items in the collection are empty according to this callback, this method will return true. |
||
| 400 | * |
||
| 401 | * @param callable $funk The callback |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function isEmpty(callable $funk = null) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Pipe collection through callback. |
||
| 417 | * |
||
| 418 | * Passes entire collection to provided callback and returns the result. |
||
| 419 | * |
||
| 420 | * @param callable $funk The callback funkshun |
||
| 421 | * |
||
| 422 | * @return mixed |
||
| 423 | */ |
||
| 424 | public function pipe(callable $funk) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Does every item return true? |
||
| 431 | * |
||
| 432 | * If callback is provided, this method will return true if all items in collection cause callback to return true. |
||
| 433 | * Otherwise, it will return true if all items in the collection have a truthy value. |
||
| 434 | * |
||
| 435 | * @param callable|null $funk The callback |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function every(callable $funk = null) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Does every item return false? |
||
| 454 | * |
||
| 455 | * This method is the exact opposite of "all". |
||
| 456 | * |
||
| 457 | * @param callable|null $funk The callback |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function none(callable $funk = null) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Get first item. |
||
| 476 | * |
||
| 477 | * Retrieve the first item in the collection or, if a callback is provided, return the first item that, when passed |
||
| 478 | * to the callback, returns true. |
||
| 479 | * |
||
| 480 | * @param callable|null $funk The callback function |
||
| 481 | * @param null $default The default value |
||
| 482 | * |
||
| 483 | * @return mixed |
||
| 484 | */ |
||
| 485 | public function first(callable $funk = null, $default = null) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get last item. |
||
| 500 | * |
||
| 501 | * Retrieve the last item in the collection or, if a callback is provided, return the last item that, when passed |
||
| 502 | * to the callback, returns true. |
||
| 503 | * |
||
| 504 | * @param callable|null $funk The callback function |
||
| 505 | * @param null $default The default value |
||
| 506 | * |
||
| 507 | * @return mixed |
||
| 508 | */ |
||
| 509 | public function last(callable $funk = null, $default = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get sequence in reverse order. |
||
| 516 | * |
||
| 517 | * @return Sequenceable |
||
| 518 | */ |
||
| 519 | public function reverse() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Return new sequence with the first item "bumped" off. |
||
| 526 | * |
||
| 527 | * @return Sequenceable |
||
| 528 | */ |
||
| 529 | public function bump() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Return new sequence with the last item "dropped" off. |
||
| 538 | * |
||
| 539 | * @return Sequenceable |
||
| 540 | */ |
||
| 541 | public function drop() |
||
| 547 | } |
||
| 548 |
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.