Complex classes like SnakList 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 SnakList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class SnakList extends ArrayObject implements Hashable, Comparable { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Maps snak hashes to their offsets. |
||
| 26 | * |
||
| 27 | * @var array [ snak hash (string) => array [ snak offset (string|int) ] | snak offset (string|int) ] |
||
| 28 | */ |
||
| 29 | private $offsetHashes = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer |
||
| 33 | */ |
||
| 34 | private $indexOffset = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @see ArrayObject::__construct |
||
| 38 | * |
||
| 39 | * @param array|Traversable|null $input |
||
| 40 | * @param int $flags |
||
| 41 | * @param string $iteratorClass |
||
| 42 | * |
||
| 43 | * @throws InvalidArgumentException |
||
| 44 | */ |
||
| 45 | public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Finds a new offset for when appending an element. |
||
| 61 | * The base class does this, so it would be better to integrate, |
||
| 62 | * but there does not appear to be any way to do this... |
||
| 63 | * |
||
| 64 | * @return integer |
||
| 65 | */ |
||
| 66 | private function getNewOffset() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Gets called before a new element is added to the list. |
||
| 76 | * |
||
| 77 | * Should return a boolean. When false is returned the element |
||
| 78 | * does not get added to the ArrayObject. |
||
| 79 | * |
||
| 80 | * @since 0.1 |
||
| 81 | * |
||
| 82 | * @param int|string $index |
||
| 83 | * @param Hashable $hashable |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | private function preSetElement( $index, $hashable ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @see ArrayObject::offsetUnset |
||
| 112 | * |
||
| 113 | * @since 0.1 |
||
| 114 | * |
||
| 115 | * @param mixed $index |
||
| 116 | */ |
||
| 117 | public function offsetUnset( $index ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @see Hashable::getHash |
||
| 146 | * |
||
| 147 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 148 | * |
||
| 149 | * @since 0.1 |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getHash() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @see Comparable::equals |
||
| 160 | * |
||
| 161 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 162 | * |
||
| 163 | * @since 0.3 |
||
| 164 | * |
||
| 165 | * @param mixed $target |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function equals( $target ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Removes duplicates bases on hash value. |
||
| 180 | * |
||
| 181 | * @since 0.3 |
||
| 182 | */ |
||
| 183 | public function removeDuplicates() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @see ArrayObject::append |
||
| 203 | * |
||
| 204 | * @param mixed $value |
||
| 205 | */ |
||
| 206 | public function append( $value ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @see ArrayObject::offsetSet() |
||
| 212 | * |
||
| 213 | * @param mixed $index |
||
| 214 | * @param mixed $value |
||
| 215 | */ |
||
| 216 | public function offsetSet( $index, $value ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Method that actually sets the element and holds |
||
| 222 | * all common code needed for set operations, including |
||
| 223 | * type checking and offset resolving. |
||
| 224 | * |
||
| 225 | * If you want to do additional indexing or have code that |
||
| 226 | * otherwise needs to be executed whenever an element is added, |
||
| 227 | * you can overload @see preSetElement. |
||
| 228 | * |
||
| 229 | * @param mixed $index |
||
| 230 | * @param mixed $value |
||
| 231 | * |
||
| 232 | * @throws InvalidArgumentException |
||
| 233 | */ |
||
| 234 | private function setElement( $index, $value ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @see Serializable::serialize |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function serialize() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @see Serializable::unserialize |
||
| 264 | * |
||
| 265 | * @param string $serialized |
||
| 266 | */ |
||
| 267 | public function unserialize( $serialized ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns if the ArrayObject has no elements. |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function isEmpty() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @since 0.1 |
||
| 290 | * |
||
| 291 | * @param string $snakHash |
||
| 292 | * |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | public function hasSnakHash( $snakHash ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @since 0.1 |
||
| 301 | * |
||
| 302 | * @param string $snakHash |
||
| 303 | */ |
||
| 304 | public function removeSnakHash( $snakHash ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @since 0.1 |
||
| 318 | * |
||
| 319 | * @param Snak $snak |
||
| 320 | * |
||
| 321 | * @return boolean Indicates if the snak was added or not. |
||
| 322 | */ |
||
| 323 | public function addSnak( Snak $snak ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @since 0.1 |
||
| 334 | * |
||
| 335 | * @param Snak $snak |
||
| 336 | * |
||
| 337 | * @return boolean |
||
| 338 | */ |
||
| 339 | public function hasSnak( Snak $snak ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @since 0.1 |
||
| 345 | * |
||
| 346 | * @param Snak $snak |
||
| 347 | */ |
||
| 348 | public function removeSnak( Snak $snak ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @since 0.1 |
||
| 354 | * |
||
| 355 | * @param string $snakHash |
||
| 356 | * |
||
| 357 | * @return Snak|bool |
||
| 358 | */ |
||
| 359 | public function getSnak( $snakHash ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Orders the snaks in the list grouping them by property. |
||
| 375 | * |
||
| 376 | * @param string[] $order List of serliazed property ids to order by. |
||
| 377 | * |
||
| 378 | * @since 0.5 |
||
| 379 | */ |
||
| 380 | public function orderByProperty( array $order = [] ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Snak[] $snaks to remove and re add |
||
| 394 | */ |
||
| 395 | private function moveSnaksToBottom( array $snaks ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets the snaks in the current object in an array |
||
| 404 | * grouped by property id |
||
| 405 | * |
||
| 406 | * @return array[] |
||
| 407 | */ |
||
| 408 | private function getSnaksByProperty() { |
||
| 422 | |||
| 423 | } |
||
| 424 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.