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 | * @param array $items |
||
| 28 | */ |
||
| 29 | public function __construct(array $items = []) { |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | */ |
||
| 41 | public function __clone() { |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Return number of items in this collection |
||
| 52 | * |
||
| 53 | * @return int |
||
| 54 | */ |
||
| 55 | public function count() { |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Add one item to begin of collection |
||
| 62 | * This item is accessible via `$collection->getFirst();` |
||
| 63 | * |
||
| 64 | * @param $item |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function prepend(Token $item) { |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Add one item to the end of collection |
||
| 75 | * This item is accessible via `$collection->getLast();` |
||
| 76 | * |
||
| 77 | * @param $item |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function append(Token $item) { |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @param int $index |
||
| 88 | * @param array $items |
||
| 89 | * @return $this |
||
| 90 | * @throws \InvalidArgumentException |
||
| 91 | */ |
||
| 92 | public function addAfter($index, $items) { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Truncate current list of items and add new |
||
| 117 | * |
||
| 118 | * @param array $items |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setItems($items) { |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Remove part of items from collection |
||
| 140 | * Works as array_slice |
||
| 141 | * |
||
| 142 | * |
||
| 143 | * @param $offset |
||
| 144 | * @param null $length |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function slice($offset, $length = null) { |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Take part of items and return new collection |
||
| 155 | * Works as array_slice |
||
| 156 | * At this point items in 2 collection is same |
||
| 157 | * |
||
| 158 | * @param int $offset |
||
| 159 | * @param null $length |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | public function extractItems($offset, $length = null) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Rewind current collection |
||
| 174 | */ |
||
| 175 | public function rewind() { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Return last item from collection |
||
| 183 | * |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | public function getLast() { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Return first item from collection |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function getFirst() { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Return next item from current |
||
| 202 | * Also can return item with position from current + $step |
||
| 203 | * |
||
| 204 | * @param int $step |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function getNext($step = 1) { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Return previous item |
||
| 215 | * Also can return previous from current position + $step |
||
| 216 | * |
||
| 217 | * @param int $step |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | public function getPrevious($step = 1) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Return current item in collection |
||
| 228 | * |
||
| 229 | * @return object |
||
| 230 | */ |
||
| 231 | public function current() { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Return current position |
||
| 238 | * |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | public function key() { |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Switch to next position |
||
| 248 | */ |
||
| 249 | public function next() { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Check if item exist in current position |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function valid() { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Add item to the end or modify item with given key |
||
| 266 | * |
||
| 267 | * @param int|null $offset |
||
| 268 | * @param object $item |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function offsetSet($offset, $item) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Check if item with given offset exists |
||
| 292 | * |
||
| 293 | * @param mixed $offset |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function offsetExists($offset) { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Remove item from collection |
||
| 303 | * |
||
| 304 | * @param int $offset |
||
| 305 | */ |
||
| 306 | public function offsetUnset($offset) { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Get item from collection |
||
| 313 | * |
||
| 314 | * @param int $offset |
||
| 315 | * @return object |
||
| 316 | */ |
||
| 317 | public function offsetGet($offset) { |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Return array of items connected to this collection |
||
| 324 | * |
||
| 325 | * Rewrite this method in you class |
||
| 326 | * |
||
| 327 | * <code> |
||
| 328 | * foreach($collection->getTokens() as $item){ |
||
| 329 | * echo get_class($item)."\n; |
||
| 330 | * } |
||
| 331 | * </code> |
||
| 332 | * @return Token[] |
||
| 333 | */ |
||
| 334 | public function getTokens() : array { |
||
| 337 | |||
| 338 | |||
| 339 | public function getItems() { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Iterate over objects in collection |
||
| 347 | * |
||
| 348 | * <code> |
||
| 349 | * $collection->each(function($item, $index, $collection){ |
||
| 350 | * if ( $index > 0 ) { |
||
| 351 | * $item->remove(); |
||
| 352 | * } |
||
| 353 | * }) |
||
| 354 | * </code> |
||
| 355 | * |
||
| 356 | * @param callable $callback |
||
| 357 | * @return $this |
||
| 358 | * @throws \InvalidArgumentException |
||
| 359 | */ |
||
| 360 | public function each(callable $callback) : self { |
||
| 374 | |||
| 375 | |||
| 376 | public function map(callable $callback) { |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * @param int $index |
||
| 384 | */ |
||
| 385 | protected function validateIndex($index) { |
||
| 388 | |||
| 389 | |||
| 390 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.