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 | * @see ArrayObject::append |
||
| 180 | * |
||
| 181 | * @param mixed $value |
||
| 182 | */ |
||
| 183 | public function append( $value ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @see ArrayObject::offsetSet() |
||
| 189 | * |
||
| 190 | * @param mixed $index |
||
| 191 | * @param mixed $value |
||
| 192 | */ |
||
| 193 | public function offsetSet( $index, $value ) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Method that actually sets the element and holds |
||
| 199 | * all common code needed for set operations, including |
||
| 200 | * type checking and offset resolving. |
||
| 201 | * |
||
| 202 | * If you want to do additional indexing or have code that |
||
| 203 | * otherwise needs to be executed whenever an element is added, |
||
| 204 | * you can overload @see preSetElement. |
||
| 205 | * |
||
| 206 | * @param mixed $index |
||
| 207 | * @param mixed $value |
||
| 208 | * |
||
| 209 | * @throws InvalidArgumentException |
||
| 210 | */ |
||
| 211 | private function setElement( $index, $value ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see Serializable::serialize |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function serialize() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @see Serializable::unserialize |
||
| 241 | * |
||
| 242 | * @param string $serialized |
||
| 243 | */ |
||
| 244 | public function unserialize( $serialized ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns if the ArrayObject has no elements. |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function isEmpty() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @since 0.1 |
||
| 267 | * |
||
| 268 | * @param string $snakHash |
||
| 269 | * |
||
| 270 | * @return boolean |
||
| 271 | */ |
||
| 272 | public function hasSnakHash( $snakHash ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @since 0.1 |
||
| 278 | * |
||
| 279 | * @param string $snakHash |
||
| 280 | */ |
||
| 281 | public function removeSnakHash( $snakHash ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @since 0.1 |
||
| 295 | * |
||
| 296 | * @param Snak $snak |
||
| 297 | * |
||
| 298 | * @return boolean Indicates if the snak was added or not. |
||
| 299 | */ |
||
| 300 | public function addSnak( Snak $snak ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @since 0.1 |
||
| 311 | * |
||
| 312 | * @param Snak $snak |
||
| 313 | * |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | public function hasSnak( Snak $snak ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @since 0.1 |
||
| 322 | * |
||
| 323 | * @param Snak $snak |
||
| 324 | */ |
||
| 325 | public function removeSnak( Snak $snak ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @since 0.1 |
||
| 331 | * |
||
| 332 | * @param string $snakHash |
||
| 333 | * |
||
| 334 | * @return Snak|bool |
||
| 335 | */ |
||
| 336 | public function getSnak( $snakHash ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Orders the snaks in the list grouping them by property. |
||
| 352 | * |
||
| 353 | * @param string[] $order List of serliazed property ids to order by. |
||
| 354 | * |
||
| 355 | * @since 0.5 |
||
| 356 | */ |
||
| 357 | public function orderByProperty( array $order = [] ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param Snak[] $snaks to remove and re add |
||
| 371 | */ |
||
| 372 | private function moveSnaksToBottom( array $snaks ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Gets the snaks in the current object in an array |
||
| 381 | * grouped by property id |
||
| 382 | * |
||
| 383 | * @return array[] |
||
| 384 | */ |
||
| 385 | private function getSnaksByProperty() { |
||
| 399 | |||
| 400 | } |
||
| 401 |
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.