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 |
||
| 30 | class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Reference[] Ordered list or references, indexed by SPL object hash. |
||
| 34 | 29 | */ |
|
| 35 | 29 | private $references = []; |
|
| 36 | 8 | ||
| 37 | /** |
||
| 38 | * @param Reference[]|Traversable $references |
||
| 39 | 21 | * |
|
| 40 | 12 | * @throws InvalidArgumentException |
|
| 41 | 4 | */ |
|
| 42 | public function __construct( $references = [] ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Adds the provided reference to the list. |
||
| 58 | 14 | * Empty references are ignored. |
|
| 59 | 14 | * |
|
| 60 | * @since 0.1 |
||
| 61 | * |
||
| 62 | * @param Reference $reference |
||
| 63 | 14 | * @param int|null $index New position of the added reference, or null to append. |
|
| 64 | * |
||
| 65 | 14 | * @throws InvalidArgumentException |
|
| 66 | 14 | */ |
|
| 67 | 2 | public function addReference( Reference $reference, $index = null ) { |
|
| 89 | |||
| 90 | /** |
||
| 91 | 6 | * @since 1.1 |
|
| 92 | 6 | * |
|
| 93 | 5 | * @param Snak[]|Snak $snaks |
|
| 94 | 5 | * @param Snak [$snak2,...] |
|
| 95 | * |
||
| 96 | 6 | * @throws InvalidArgumentException |
|
| 97 | 5 | */ |
|
| 98 | public function addNewReference( $snaks = [] /*...*/ ) { |
||
| 105 | 2 | ||
| 106 | /** |
||
| 107 | * @param Reference $reference |
||
| 108 | 2 | * @param int $index |
|
| 109 | 2 | */ |
|
| 110 | 2 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
|
| 123 | 2 | ||
| 124 | 2 | /** |
|
| 125 | * Returns if the list contains a reference with the same hash as the provided reference. |
||
| 126 | * |
||
| 127 | * @since 0.1 |
||
| 128 | * |
||
| 129 | * @param Reference $reference |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function hasReference( Reference $reference ) { |
||
| 136 | 6 | ||
| 137 | 6 | /** |
|
| 138 | * Returns the index of the Reference object or false if the Reference could not be found. |
||
| 139 | * |
||
| 140 | * @since 0.5 |
||
| 141 | * |
||
| 142 | * @param Reference $reference |
||
| 143 | * |
||
| 144 | * @return int|bool |
||
| 145 | */ |
||
| 146 | public function indexOf( Reference $reference ) { |
||
| 159 | 2 | ||
| 160 | /** |
||
| 161 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
||
| 162 | * |
||
| 163 | * @since 0.1 |
||
| 164 | * |
||
| 165 | * @param Reference $reference |
||
| 166 | */ |
||
| 167 | public function removeReference( Reference $reference ) { |
||
| 170 | 3 | ||
| 171 | 3 | /** |
|
| 172 | * Returns if the list contains a reference with the provided hash. |
||
| 173 | * |
||
| 174 | * @since 0.3 |
||
| 175 | * |
||
| 176 | * @param string $referenceHash |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasReferenceHash( $referenceHash ) { |
||
| 183 | 8 | ||
| 184 | /** |
||
| 185 | * Looks for the first Reference object in this list with the provided hash. |
||
| 186 | * Removes all occurences of that object. |
||
| 187 | * |
||
| 188 | * @since 0.3 |
||
| 189 | * |
||
| 190 | * @param string $referenceHash ` |
||
| 191 | */ |
||
| 192 | public function removeReferenceHash( $referenceHash ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the first Reference object with the provided hash, or |
||
| 208 | * null if there is no such reference in the list. |
||
| 209 | * |
||
| 210 | 14 | * @since 0.3 |
|
| 211 | * |
||
| 212 | * @param string $referenceHash |
||
| 213 | * |
||
| 214 | 14 | * @return Reference|null |
|
| 215 | 10 | */ |
|
| 216 | 10 | public function getReference( $referenceHash ) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @see Serializable::serialize |
||
| 228 | * |
||
| 229 | * @since 2.1 |
||
| 230 | 3 | * |
|
| 231 | 3 | * @return string |
|
| 232 | */ |
||
| 233 | public function serialize() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see https://wiki.php.net/rfc/custom_object_serialization |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | 3 | */ |
|
| 242 | 3 | public function __serialize() { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @see https://wiki.php.net/rfc/custom_object_serialization |
||
| 250 | 3 | * |
|
| 251 | 3 | * @param array $data |
|
| 252 | */ |
||
| 253 | public function __unserialize( array $data ) : void { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @see Serializable::unserialize |
||
| 259 | * |
||
| 260 | * @since 2.1 |
||
| 261 | * |
||
| 262 | * @param string $serialized |
||
| 263 | */ |
||
| 264 | public function unserialize( $serialized ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @since 4.4 |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function isEmpty() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 279 | * |
||
| 280 | * @since 0.3 |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getValueHash() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @see Comparable::equals |
||
| 291 | * |
||
| 292 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 293 | * |
||
| 294 | * @since 0.3 |
||
| 295 | * |
||
| 296 | * @param mixed $target |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function equals( $target ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @see Countable::count |
||
| 311 | * |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public function count() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @see IteratorAggregate::getIterator |
||
| 320 | * |
||
| 321 | * @since 5.0 |
||
| 322 | * |
||
| 323 | * @return Iterator|Reference[] |
||
| 324 | */ |
||
| 325 | public function getIterator() { |
||
| 328 | |||
| 329 | } |
||
| 330 |
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..