Complex classes like ArrayCollection 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 ArrayCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ArrayCollection implements CollectionInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * An array containing the entries of this collection. |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $elements; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Initializes a new ArrayCollection. |
||
| 28 | * |
||
| 29 | * @param array $elements |
||
| 30 | */ |
||
| 31 | 197 | public function __construct(array $elements = array()) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Gets the PHP array representation of this collection. |
||
| 38 | * @return array The PHP array representation of this collection. |
||
| 39 | */ |
||
| 40 | 10 | public function toArray() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | 11 | public function first() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | 10 | public function last() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 1 | public function key() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 1 | public function next() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | 16 | public function current() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | 10 | public function remove($key) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 2 | public function removeElement($element) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | 1 | public function offsetExists($offset) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 1 | public function offsetGet($offset) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 14 | public function offsetSet($offset, $value) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | 1 | public function offsetUnset($offset) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | 8 | public function containsKey($key) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | 20 | public function contains($element) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | * @param string $findElement |
||
| 177 | */ |
||
| 178 | 7 | public function exists($findKey = null, $findElement = null) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 1 | public function indexOf($element) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 57 | public function get($key) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 78 | public function getKeys() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 1 | public function getValues() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 60 | public function count() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 40 | public function set($key, $value) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | 44 | public function add($value) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | 15 | public function isEmpty() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | 6 | public function getIterator() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | 1 | public function clear() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | 1 | public function slice($offset, $length = null) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Sorting by values using a user-defined comparison function. |
||
| 285 | * |
||
| 286 | * @param \Closure $callable |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | 3 | public function sort(\Closure $callable) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Sort the list by elements. |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 1 | public function sortAsc() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Sort the list by elements. |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | public function sortDesc() |
||
| 324 | } |
||
| 325 |