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 |
||
| 28 | class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Reference[] Ordered list or references, indexed by SPL object hash. |
||
| 32 | */ |
||
| 33 | private $references = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Reference[]|Traversable $references |
||
| 37 | * |
||
| 38 | * @throws InvalidArgumentException |
||
| 39 | */ |
||
| 40 | public function __construct( $references = [] ) { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Adds the provided reference to the list. |
||
| 56 | * Empty references are ignored. |
||
| 57 | * |
||
| 58 | * @since 0.1 |
||
| 59 | * |
||
| 60 | * @param Reference $reference |
||
| 61 | * @param int|null $index New position of the added reference, or null to append. |
||
| 62 | * |
||
| 63 | * @throws InvalidArgumentException |
||
| 64 | */ |
||
| 65 | public function addReference( Reference $reference, $index = null ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @since 1.1 |
||
| 90 | * |
||
| 91 | * @param Snak[]|Snak $snaks |
||
| 92 | * @param Snak [$snak2,...] |
||
| 93 | * |
||
| 94 | * @throws InvalidArgumentException |
||
| 95 | */ |
||
| 96 | public function addNewReference( $snaks = [] /*...*/ ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Reference $reference |
||
| 106 | * @param int $index |
||
| 107 | */ |
||
| 108 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns if the list contains a reference with the same hash as the provided reference. |
||
| 124 | * |
||
| 125 | * @since 0.1 |
||
| 126 | * |
||
| 127 | * @param Reference $reference |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function hasReference( Reference $reference ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the index of the Reference object or false if the Reference could not be found. |
||
| 137 | * |
||
| 138 | * @since 0.5 |
||
| 139 | * |
||
| 140 | * @param Reference $reference |
||
| 141 | * |
||
| 142 | * @return int|bool |
||
| 143 | */ |
||
| 144 | public function indexOf( Reference $reference ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * 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 | /** |
||
| 170 | * Returns if the list contains a reference with the provided hash. |
||
| 171 | * |
||
| 172 | * @since 0.3 |
||
| 173 | * |
||
| 174 | * @param string $referenceHash |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function hasReferenceHash( $referenceHash ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * 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 | * @param string $referenceHash |
||
| 211 | * |
||
| 212 | * @return Reference|null |
||
| 213 | */ |
||
| 214 | public function getReference( $referenceHash ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @see Serializable::serialize |
||
| 226 | * |
||
| 227 | * @since 2.1 |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 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 | */ |
||
| 251 | 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 | * @see Comparable::equals |
||
| 289 | * |
||
| 290 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 291 | * |
||
| 292 | * @since 0.3 |
||
| 293 | * |
||
| 294 | * @param mixed $target |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function equals( $target ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @see Countable::count |
||
| 309 | * |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | public function count() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @see IteratorAggregate::getIterator |
||
| 318 | * |
||
| 319 | * @since 5.0 |
||
| 320 | * |
||
| 321 | * @return Iterator|Reference[] |
||
| 322 | */ |
||
| 323 | public function getIterator() { |
||
| 326 | |||
| 327 | } |
||
| 328 |
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..