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 |
||
| 23 | class ArrayCollection implements Collection |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * An array containing the entries of this collection. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $elements; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Initializes a new ArrayCollection. |
||
| 34 | * |
||
| 35 | * @param array $elements |
||
| 36 | * |
||
| 37 | * @codeCoverageIgnore |
||
| 38 | */ |
||
| 39 | public function __construct(array $elements = array()) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | */ |
||
| 47 | public function toArray() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritDoc} |
||
| 54 | */ |
||
| 55 | public function first() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | public function last() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | */ |
||
| 71 | public function key() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritDoc} |
||
| 78 | */ |
||
| 79 | public function next() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | public function current() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | public function remove($key) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritDoc} |
||
| 109 | */ |
||
| 110 | public function removeElement($element) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Required by interface ArrayAccess. |
||
| 125 | * |
||
| 126 | * {@inheritDoc} |
||
| 127 | */ |
||
| 128 | public function offsetExists($offset) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Required by interface ArrayAccess. |
||
| 135 | * |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | public function offsetGet($offset) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Required by interface ArrayAccess. |
||
| 145 | * |
||
| 146 | * {@inheritDoc} |
||
| 147 | */ |
||
| 148 | public function offsetSet($offset, $value) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Required by interface ArrayAccess. |
||
| 161 | * |
||
| 162 | * {@inheritDoc} |
||
| 163 | */ |
||
| 164 | public function offsetUnset($offset) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritDoc} |
||
| 171 | */ |
||
| 172 | public function containsKey($key) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritDoc} |
||
| 179 | */ |
||
| 180 | public function contains($element) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Tests for the existence of an element that satisfies the given predicate. |
||
| 187 | * |
||
| 188 | * @param Closure $p The predicate. |
||
| 189 | * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise. |
||
| 190 | */ |
||
| 191 | public function exists(Closure $p) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Applies the given predicate p to all elements of this collection, |
||
| 204 | * returning true, if the predicate yields true for all elements. |
||
| 205 | * |
||
| 206 | * @param Closure $p The predicate. |
||
| 207 | * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. |
||
| 208 | */ |
||
| 209 | public function forAll(Closure $p) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritDoc} |
||
| 216 | */ |
||
| 217 | public function indexOf($element) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritDoc} |
||
| 224 | */ |
||
| 225 | public function get($key) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | public function getKeys() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritDoc} |
||
| 240 | */ |
||
| 241 | public function getValues() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritDoc} |
||
| 248 | */ |
||
| 249 | public function count() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritDoc} |
||
| 256 | */ |
||
| 257 | public function set($key, $value) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritDoc} |
||
| 264 | */ |
||
| 265 | public function add($value) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritDoc} |
||
| 274 | */ |
||
| 275 | public function isEmpty() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Required by interface IteratorAggregate. |
||
| 282 | * |
||
| 283 | * {@inheritDoc} |
||
| 284 | */ |
||
| 285 | public function getIterator() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritDoc} |
||
| 292 | */ |
||
| 293 | public function map(Closure $func) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritDoc} |
||
| 300 | */ |
||
| 301 | public function filter(Closure $p) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritDoc} |
||
| 308 | */ |
||
| 309 | public function partition(Closure $p) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns a string representation of this object. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function __toString() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritDoc} |
||
| 336 | */ |
||
| 337 | public function clear() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritDoc} |
||
| 344 | */ |
||
| 345 | public function slice($offset, $length = null) |
||
| 349 | } |