Complex classes like ReferenceList 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 ReferenceList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ReferenceList implements Countable, IteratorAggregate, Serializable { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Reference[] Ordered list or references, indexed by SPL object hash. |
||
| 31 | */ |
||
| 32 | private $references = []; |
||
| 33 | |||
| 34 | 29 | /** |
|
| 35 | 29 | * @param Reference[]|Traversable $references |
|
| 36 | 8 | * |
|
| 37 | * @throws InvalidArgumentException |
||
| 38 | */ |
||
| 39 | 21 | public function __construct( $references = [] ) { |
|
| 40 | 12 | if ( !is_array( $references ) && !( $references instanceof Traversable ) ) { |
|
| 41 | 4 | throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' ); |
|
| 42 | } |
||
| 43 | |||
| 44 | 8 | foreach ( $references as $reference ) { |
|
| 45 | 17 | if ( !( $reference instanceof Reference ) ) { |
|
| 46 | 17 | throw new InvalidArgumentException( 'Every element in $references must be an instance of Reference' ); |
|
| 47 | } |
||
| 48 | |||
| 49 | $this->addReference( $reference ); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Adds the provided reference to the list. |
||
| 55 | * Empty references are ignored. |
||
| 56 | * |
||
| 57 | * @since 0.1 |
||
| 58 | 14 | * |
|
| 59 | 14 | * @param Reference $reference |
|
| 60 | * @param int|null $index New position of the added reference, or null to append. |
||
| 61 | * |
||
| 62 | * @throws InvalidArgumentException |
||
| 63 | 14 | */ |
|
| 64 | public function addReference( Reference $reference, $index = null ) { |
||
| 65 | 14 | if ( $index !== null && ( !is_int( $index ) || $index < 0 ) ) { |
|
| 66 | 14 | throw new InvalidArgumentException( '$index must be a non-negative integer or null' ); |
|
| 67 | 2 | } |
|
| 68 | |||
| 69 | 14 | if ( $reference->isEmpty() ) { |
|
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | $splHash = spl_object_hash( $reference ); |
||
| 74 | |||
| 75 | if ( array_key_exists( $splHash, $this->references ) ) { |
||
| 76 | return; |
||
| 77 | 14 | } |
|
| 78 | 14 | ||
| 79 | 14 | if ( $index === null || $index >= count( $this->references ) ) { |
|
| 80 | 14 | // Append object to the end of the reference list. |
|
| 81 | 14 | $this->references[$splHash] = $reference; |
|
| 82 | } else { |
||
| 83 | $this->insertReferenceAtIndex( $reference, $index ); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @since 1.1 |
||
| 89 | * |
||
| 90 | * @param Snak ...$snaks |
||
| 91 | 6 | * (passing a single Snak[] is still supported but deprecated) |
|
| 92 | 6 | * |
|
| 93 | 5 | * @throws InvalidArgumentException |
|
| 94 | 5 | */ |
|
| 95 | public function addNewReference( ...$snaks ) { |
||
| 96 | 6 | if ( count( $snaks ) === 1 && is_array( $snaks[0] ) ) { |
|
| 97 | 5 | // TODO stop supporting this |
|
| 98 | $snaks = $snaks[0]; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->addReference( new Reference( $snaks ) ); |
||
| 102 | } |
||
| 103 | 2 | ||
| 104 | 2 | /** |
|
| 105 | 2 | * @param Reference $reference |
|
| 106 | * @param int $index |
||
| 107 | */ |
||
| 108 | 2 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
|
| 121 | 2 | ||
| 122 | 2 | /** |
|
| 123 | 2 | * Returns if the list contains a reference with the same hash as the provided reference. |
|
| 124 | 2 | * |
|
| 125 | * @since 0.1 |
||
| 126 | * |
||
| 127 | * @param Reference $reference |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function hasReference( Reference $reference ) { |
||
| 134 | |||
| 135 | 6 | /** |
|
| 136 | 6 | * Returns the index of the Reference object or false if the Reference could not be found. |
|
| 137 | 6 | * |
|
| 138 | * @since 0.5 |
||
| 139 | * |
||
| 140 | * @param Reference $reference |
||
| 141 | * |
||
| 142 | * @return int|bool |
||
| 143 | */ |
||
| 144 | public function indexOf( Reference $reference ) { |
||
| 157 | 2 | ||
| 158 | /** |
||
| 159 | 2 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
|
| 160 | * |
||
| 161 | * @since 0.1 |
||
| 162 | * |
||
| 163 | * @param Reference $reference |
||
| 164 | */ |
||
| 165 | public function removeReference( Reference $reference ) { |
||
| 168 | |||
| 169 | 3 | /** |
|
| 170 | 3 | * Returns if the list contains a reference with the provided hash. |
|
| 171 | 3 | * |
|
| 172 | * @since 0.3 |
||
| 173 | * |
||
| 174 | * @param string $referenceHash |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function hasReferenceHash( $referenceHash ) { |
||
| 181 | |||
| 182 | 8 | /** |
|
| 183 | 8 | * Looks for the first Reference object in this list with the provided hash. |
|
| 184 | * Removes all occurences of that object. |
||
| 185 | * |
||
| 186 | * @since 0.3 |
||
| 187 | * |
||
| 188 | * @param string $referenceHash ` |
||
| 189 | */ |
||
| 190 | public function removeReferenceHash( $referenceHash ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the first Reference object with the provided hash, or |
||
| 206 | * null if there is no such reference in the list. |
||
| 207 | * |
||
| 208 | * @since 0.3 |
||
| 209 | * |
||
| 210 | 14 | * @param string $referenceHash |
|
| 211 | * |
||
| 212 | * @return Reference|null |
||
| 213 | */ |
||
| 214 | 14 | public function getReference( $referenceHash ) { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @see Serializable::serialize |
||
| 226 | * |
||
| 227 | * @since 2.1 |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | 3 | */ |
|
| 231 | 3 | public function serialize() { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @see https://wiki.php.net/rfc/custom_object_serialization |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function __serialize() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @see https://wiki.php.net/rfc/custom_object_serialization |
||
| 248 | * |
||
| 249 | * @param array $data |
||
| 250 | 3 | */ |
|
| 251 | 3 | public function __unserialize( array $data ) : void { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @see Serializable::unserialize |
||
| 257 | * |
||
| 258 | * @since 2.1 |
||
| 259 | * |
||
| 260 | * @param string $serialized |
||
| 261 | */ |
||
| 262 | public function unserialize( $serialized ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @since 4.4 |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isEmpty() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 277 | * |
||
| 278 | * @since 0.3 |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getValueHash() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 289 | * |
||
| 290 | * @since 0.3 |
||
| 291 | * |
||
| 292 | * @param mixed $target |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function equals( $target ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @see Countable::count |
||
| 307 | * |
||
| 308 | * @return int |
||
| 309 | */ |
||
| 310 | public function count() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @see IteratorAggregate::getIterator |
||
| 316 | * |
||
| 317 | * @since 5.0 |
||
| 318 | * |
||
| 319 | * @return Iterator|Reference[] |
||
| 320 | */ |
||
| 321 | public function getIterator() { |
||
| 324 | |||
| 325 | } |
||
| 326 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..