Complex classes like BaseCollection 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 BaseCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class BaseCollection implements \Iterator, \ArrayAccess, \Countable { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | protected $position = 0; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Array of objects |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $items = []; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $initialContentHash; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @param array $items |
||
| 34 | */ |
||
| 35 | public function __construct(array $items = []) { |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | public function __clone() { |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Return number of items in this collection |
||
| 59 | * |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function count() { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Add one item to begin of collection |
||
| 69 | * This item is accessible via `$collection->getFirst();` |
||
| 70 | * |
||
| 71 | * @param $item |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function prepend(Token $item) { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Add one item to the end of collection |
||
| 82 | * This item is accessible via `$collection->getLast();` |
||
| 83 | * |
||
| 84 | * @param $item |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function append(Token $item) { |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * @param int $index |
||
| 95 | * @param array $items |
||
| 96 | * @return $this |
||
| 97 | * @throws \InvalidArgumentException |
||
| 98 | */ |
||
| 99 | public function addAfter($index, $items) { |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Truncate current list of items and add new |
||
| 124 | * |
||
| 125 | * @param array $items |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function setItems($items) { |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Remove part of items from collection |
||
| 147 | * Works as array_slice |
||
| 148 | * |
||
| 149 | * |
||
| 150 | * @param $offset |
||
| 151 | * @param null $length |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function slice($offset, $length = null) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Take part of items and return new collection |
||
| 162 | * Works as array_slice |
||
| 163 | * At this point items in 2 collection is same |
||
| 164 | * |
||
| 165 | * @param int $offset |
||
| 166 | * @param null $length |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | public function extractItems($offset, $length = null) { |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Rewind current collection |
||
| 181 | */ |
||
| 182 | public function rewind() { |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Return last item from collection |
||
| 190 | * |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | public function getLast() { |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Return first item from collection |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function getFirst() { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Return next item from current |
||
| 209 | * Also can return item with position from current + $step |
||
| 210 | * |
||
| 211 | * @param int $step |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | public function getNext($step = 1) { |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Return previous item |
||
| 222 | * Also can return previous from current position + $step |
||
| 223 | * |
||
| 224 | * @param int $step |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | public function getPrevious($step = 1) { |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Return current item in collection |
||
| 235 | * |
||
| 236 | * @return object |
||
| 237 | */ |
||
| 238 | public function current() { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Return current position |
||
| 245 | * |
||
| 246 | * @return int |
||
| 247 | */ |
||
| 248 | public function key() { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Switch to next position |
||
| 255 | */ |
||
| 256 | public function next() { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if item exist in current position |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function valid() { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Add item to the end or modify item with given key |
||
| 273 | * |
||
| 274 | * @param int|null $offset |
||
| 275 | * @param object $item |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function offsetSet($offset, $item) { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Check if item with given offset exists |
||
| 299 | * |
||
| 300 | * @param mixed $offset |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function offsetExists($offset) { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Remove item from collection |
||
| 310 | * |
||
| 311 | * @param int $offset |
||
| 312 | */ |
||
| 313 | public function offsetUnset($offset) { |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Get item from collection |
||
| 320 | * |
||
| 321 | * @param int $offset |
||
| 322 | * @return object |
||
| 323 | */ |
||
| 324 | public function offsetGet($offset) { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Return array of items connected to this collection |
||
| 331 | * |
||
| 332 | * Rewrite this method in you class |
||
| 333 | * |
||
| 334 | * <code> |
||
| 335 | * foreach($collection->getTokens() as $item){ |
||
| 336 | * echo get_class($item)."\n; |
||
| 337 | * } |
||
| 338 | * </code> |
||
| 339 | * @return Token[] |
||
| 340 | */ |
||
| 341 | public function getTokens() : array { |
||
| 344 | |||
| 345 | |||
| 346 | public function getItems() { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Iterate over objects in collection |
||
| 354 | * |
||
| 355 | * <code> |
||
| 356 | * $collection->each(function($item, $index, $collection){ |
||
| 357 | * if ( $index > 0 ) { |
||
| 358 | * $item->remove(); |
||
| 359 | * } |
||
| 360 | * }) |
||
| 361 | * </code> |
||
| 362 | * |
||
| 363 | * @param callable $callback |
||
| 364 | * @return $this |
||
| 365 | * @throws \InvalidArgumentException |
||
| 366 | */ |
||
| 367 | public function each(callable $callback) : self { |
||
| 381 | |||
| 382 | |||
| 383 | public function map(callable $callback) { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Remove all tokens in collection |
||
| 391 | * |
||
| 392 | * @return Collection |
||
| 393 | */ |
||
| 394 | public function remove() { |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int $index |
||
| 404 | */ |
||
| 405 | protected function validateIndex($index) { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | public function storeContentHash() { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public function isChanged() { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private function getContentHash() { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function assemble() { |
||
| 450 | |||
| 451 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.