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) => snak offset (string|int) ] |
||
| 28 | */ |
||
| 29 | private $offsetHashes = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 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 int |
||
| 65 | */ |
||
| 66 | private function getNewOffset() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @see ArrayObject::offsetUnset |
||
| 76 | * |
||
| 77 | * @since 0.1 |
||
| 78 | * |
||
| 79 | * @param int|string $index |
||
| 80 | */ |
||
| 81 | public function offsetUnset( $index ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @see Hashable::getHash |
||
| 96 | * |
||
| 97 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 98 | * |
||
| 99 | * @since 0.1 |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getHash() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @see Comparable::equals |
||
| 110 | * |
||
| 111 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 112 | * |
||
| 113 | * @since 0.3 |
||
| 114 | * |
||
| 115 | * @param mixed $target |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function equals( $target ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @see ArrayObject::append |
||
| 130 | * |
||
| 131 | * @param Snak $value |
||
| 132 | */ |
||
| 133 | public function append( $value ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @see ArrayObject::offsetSet() |
||
| 139 | * |
||
| 140 | * @param int|string $index |
||
| 141 | * @param Snak $value |
||
| 142 | */ |
||
| 143 | public function offsetSet( $index, $value ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Method that actually sets the element and holds |
||
| 149 | * all common code needed for set operations, including |
||
| 150 | * type checking and offset resolving. |
||
| 151 | * |
||
| 152 | * @param int|string $index |
||
| 153 | * @param Snak $value |
||
| 154 | * |
||
| 155 | * @throws InvalidArgumentException |
||
| 156 | */ |
||
| 157 | private function setElement( $index, $value ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @see Serializable::serialize |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function serialize() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @see Serializable::unserialize |
||
| 189 | * |
||
| 190 | * @param string $serialized |
||
| 191 | */ |
||
| 192 | public function unserialize( $serialized ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns if the ArrayObject has no elements. |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function isEmpty() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @since 0.1 |
||
| 215 | * |
||
| 216 | * @param string $snakHash |
||
| 217 | * |
||
| 218 | * @return boolean |
||
| 219 | */ |
||
| 220 | public function hasSnakHash( $snakHash ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @since 0.1 |
||
| 226 | * |
||
| 227 | * @param string $snakHash |
||
| 228 | */ |
||
| 229 | public function removeSnakHash( $snakHash ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @since 0.1 |
||
| 238 | * |
||
| 239 | * @param Snak $snak |
||
| 240 | * |
||
| 241 | * @return boolean Indicates if the snak was added or not. |
||
| 242 | */ |
||
| 243 | public function addSnak( Snak $snak ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @since 0.1 |
||
| 254 | * |
||
| 255 | * @param Snak $snak |
||
| 256 | * |
||
| 257 | * @return boolean |
||
| 258 | */ |
||
| 259 | public function hasSnak( Snak $snak ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @since 0.1 |
||
| 265 | * |
||
| 266 | * @param Snak $snak |
||
| 267 | */ |
||
| 268 | public function removeSnak( Snak $snak ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @since 0.1 |
||
| 274 | * |
||
| 275 | * @param string $snakHash |
||
| 276 | * |
||
| 277 | * @return Snak|bool |
||
| 278 | */ |
||
| 279 | public function getSnak( $snakHash ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Orders the snaks in the list grouping them by property. |
||
| 290 | * |
||
| 291 | * @param string[] $order List of serliazed property ids to order by. |
||
| 292 | * |
||
| 293 | * @since 0.5 |
||
| 294 | */ |
||
| 295 | public function orderByProperty( array $order = [] ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param Snak[] $snaks to remove and re add |
||
| 309 | */ |
||
| 310 | private function moveSnaksToBottom( array $snaks ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the snaks in the current object in an array |
||
| 319 | * grouped by property id |
||
| 320 | * |
||
| 321 | * @return array[] |
||
| 322 | */ |
||
| 323 | private function getSnaksByProperty() { |
||
| 337 | |||
| 338 | } |
||
| 339 |
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.