Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Collection implements \Iterator, \ArrayAccess, \Countable { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | protected $position = 0; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Array of objects |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $items = []; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $initialContentHash; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array $items |
||
| 36 | */ |
||
| 37 | 450 | public function __construct(array $items = []) { |
|
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | 144 | public function __clone() { |
|
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Extract each value from token |
||
| 59 | 435 | * |
|
| 60 | 435 | * @return string |
|
| 61 | 435 | */ |
|
| 62 | public function __toString() { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | 9 | * |
|
| 69 | 9 | * @param string $string |
|
| 70 | * @return Collection |
||
| 71 | * @throws Exception |
||
| 72 | */ |
||
| 73 | public static function createFromString($string) : Collection { |
||
| 77 | 450 | ||
| 78 | |||
| 79 | /** |
||
| 80 | * Return number of items in this collection |
||
| 81 | * |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | public function count() { |
||
| 87 | 441 | ||
| 88 | |||
| 89 | /** |
||
| 90 | * Add one item to begin of collection |
||
| 91 | * This item is accessible via `$collection->getFirst();` |
||
| 92 | * |
||
| 93 | * @param $item |
||
| 94 | 450 | * @return $this |
|
| 95 | 450 | */ |
|
| 96 | public function prepend(Token $item) : self { |
||
| 100 | |||
| 101 | 435 | ||
| 102 | 300 | /** |
|
| 103 | * Add one item to the end of collection |
||
| 104 | 450 | * This item is accessible via `$collection->getLast();` |
|
| 105 | * |
||
| 106 | * @param $item |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function append(Token $item) : self { |
||
| 113 | |||
| 114 | 9 | ||
| 115 | 9 | /** |
|
| 116 | 9 | * @param int $index |
|
| 117 | * @param array $items |
||
| 118 | 9 | * @return $this |
|
| 119 | 9 | * @throws \InvalidArgumentException |
|
| 120 | */ |
||
| 121 | 9 | public function addAfter($index, $items) : self { |
|
| 142 | |||
| 143 | 6 | ||
| 144 | 6 | /** |
|
| 145 | 6 | * Truncate current list of items and add new |
|
| 146 | 6 | * |
|
| 147 | 4 | * @param array $items |
|
| 148 | 6 | * @return $this |
|
| 149 | */ |
||
| 150 | public function setItems(array $items) : self { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Remove part of items from collection |
||
| 165 | * Works as array_slice |
||
| 166 | * |
||
| 167 | * |
||
| 168 | * @param int $offset |
||
| 169 | * @param int|null $length |
||
| 170 | 9 | * @return $this |
|
| 171 | 9 | */ |
|
| 172 | 9 | public function slice(int $offset, int $length = null) : self { |
|
| 176 | 6 | ||
| 177 | |||
| 178 | 9 | /** |
|
| 179 | * Take part of items and return new collection |
||
| 180 | * Works as array_slice |
||
| 181 | * At this point items in 2 collection is same |
||
| 182 | * |
||
| 183 | * @param int $offset |
||
| 184 | * @param null $length |
||
| 185 | * @return Collection |
||
| 186 | */ |
||
| 187 | public function extractItems(int $offset, $length = null) : Collection { |
||
| 191 | 20 | ||
| 192 | 30 | ||
| 193 | /** |
||
| 194 | * Rewind current collection |
||
| 195 | */ |
||
| 196 | public function rewind() { |
||
| 200 | |||
| 201 | 177 | ||
| 202 | /** |
||
| 203 | 177 | * Return last item from collection |
|
| 204 | 177 | * |
|
| 205 | 177 | * @return Token|null |
|
| 206 | */ |
||
| 207 | 177 | public function getLast() { |
|
| 211 | 118 | ||
| 212 | |||
| 213 | /** |
||
| 214 | 177 | * Return first item from collection |
|
| 215 | * @return Token|null |
||
| 216 | */ |
||
| 217 | public function getFirst() { |
||
| 221 | |||
| 222 | 15 | ||
| 223 | 15 | /** |
|
| 224 | 15 | * Return next item from current |
|
| 225 | * Also can return item with position from current + $step |
||
| 226 | * |
||
| 227 | * @param int $step |
||
| 228 | * @return Token |
||
| 229 | */ |
||
| 230 | public function getNext(int $step = 1) : Token { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Return previous item |
||
| 238 | * Also can return previous from current position + $step |
||
| 239 | * |
||
| 240 | * @param int $step |
||
| 241 | * @return Token |
||
| 242 | */ |
||
| 243 | public function getPrevious(int $step = 1) : Token { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Return current item in collection |
||
| 251 | * |
||
| 252 | * @return Token |
||
| 253 | */ |
||
| 254 | public function current() { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Return current position |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function key() { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Switch to next position |
||
| 271 | */ |
||
| 272 | public function next() { |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Check if item exist in current position |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function valid() : bool { |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Add item to the end or modify item with given key |
||
| 289 | * |
||
| 290 | * @param int|null $offset |
||
| 291 | * @param Token $item |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function offsetSet($offset, $item) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Check if item with given offset exists |
||
| 315 | * |
||
| 316 | * @param int $offset |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function offsetExists($offset) { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Remove item from collection |
||
| 326 | * |
||
| 327 | * @param int $offset |
||
| 328 | */ |
||
| 329 | public function offsetUnset($offset) { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Get item from collection |
||
| 336 | * |
||
| 337 | * @param int $offset |
||
| 338 | * @return Token|null |
||
| 339 | */ |
||
| 340 | public function offsetGet($offset) { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Return array of items connected to this collection |
||
| 347 | * |
||
| 348 | * Rewrite this method in you class |
||
| 349 | * |
||
| 350 | * <code> |
||
| 351 | * foreach($collection->getTokens() as $item){ |
||
| 352 | * echo get_class($item)."\n; |
||
| 353 | * } |
||
| 354 | * </code> |
||
| 355 | * @return Token[] |
||
| 356 | */ |
||
| 357 | public function getTokens() : array { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Iterate over objects in collection |
||
| 364 | * |
||
| 365 | * <code> |
||
| 366 | * $collection->each(function($item, $index, $collection){ |
||
| 367 | * if ( $index > 0 ) { |
||
| 368 | * $item->remove(); |
||
| 369 | * } |
||
| 370 | * }) |
||
| 371 | * </code> |
||
| 372 | * |
||
| 373 | * @param callable $callback |
||
| 374 | * @return $this |
||
| 375 | * @throws \InvalidArgumentException |
||
| 376 | */ |
||
| 377 | public function each(callable $callback) : self { |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Remove all tokens in collection |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function remove() : self { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * @param Query $query |
||
| 408 | * @return Collection |
||
| 409 | */ |
||
| 410 | public function find(Query $query) { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * Remove all invalid tokens in collection |
||
| 418 | * Refresh index. |
||
| 419 | * |
||
| 420 | * @return Collection |
||
| 421 | */ |
||
| 422 | public function refresh() : self { |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * @param Token $tokenStart |
||
| 436 | * @param Token $tokenEnd |
||
| 437 | * @return Collection |
||
| 438 | */ |
||
| 439 | public function extractByTokens(Token $tokenStart, Token $tokenEnd) : Collection { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * @return $this |
||
| 458 | */ |
||
| 459 | public function storeContentHash() : self { |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public function isChanged() : bool { |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | private function getContentHash() : string { |
||
| 479 | |||
| 480 | |||
| 481 | /** |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function assemble() : string { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Remove invalid tokens from collection |
||
| 500 | * |
||
| 501 | * @return $this |
||
| 502 | */ |
||
| 503 | protected function cleanCollection() : self { |
||
| 513 | |||
| 514 | } |