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 = array(); |
||
| 34 | 29 | ||
| 35 | 29 | /** |
|
| 36 | 8 | * @param Reference[]|Traversable $references |
|
| 37 | * |
||
| 38 | * @throws InvalidArgumentException |
||
| 39 | 21 | */ |
|
| 40 | 12 | public function __construct( $references = array() ) { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Adds the provided reference to the list. |
||
| 56 | * Empty references are ignored. |
||
| 57 | * |
||
| 58 | 14 | * @since 0.1 |
|
| 59 | 14 | * |
|
| 60 | * @param Reference $reference |
||
| 61 | * @param int|null $index |
||
| 62 | * |
||
| 63 | 14 | * @throws InvalidArgumentException |
|
| 64 | */ |
||
| 65 | 14 | public function addReference( Reference $reference, $index = null ) { |
|
| 91 | 6 | ||
| 92 | 6 | /** |
|
| 93 | 5 | * @since 1.1 |
|
| 94 | 5 | * |
|
| 95 | * @param Snak[]|Snak $snaks |
||
| 96 | 6 | * @param Snak [$snak2,...] |
|
| 97 | 5 | * |
|
| 98 | * @throws InvalidArgumentException |
||
| 99 | */ |
||
| 100 | public function addNewReference( $snaks = array() /*...*/ ) { |
||
| 107 | |||
| 108 | 2 | /** |
|
| 109 | 2 | * @param Reference $reference |
|
| 110 | 2 | * @param int $index |
|
| 111 | 2 | */ |
|
| 112 | 2 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
|
| 119 | 2 | ||
| 120 | /** |
||
| 121 | 2 | * Returns if the list contains a reference with the same hash as the provided reference. |
|
| 122 | 2 | * |
|
| 123 | 2 | * @since 0.1 |
|
| 124 | 2 | * |
|
| 125 | * @param Reference $reference |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public function hasReference( Reference $reference ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the index of the Reference object or false if the Reference could not be found. |
||
| 135 | 6 | * |
|
| 136 | 6 | * @since 0.5 |
|
| 137 | 6 | * |
|
| 138 | * @param Reference $reference |
||
| 139 | * |
||
| 140 | * @return int|bool |
||
| 141 | */ |
||
| 142 | public function indexOf( Reference $reference ) { |
||
| 155 | |||
| 156 | 1 | /** |
|
| 157 | 2 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
|
| 158 | * |
||
| 159 | 2 | * @since 0.1 |
|
| 160 | * |
||
| 161 | * @param Reference $reference |
||
| 162 | */ |
||
| 163 | public function removeReference( Reference $reference ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns if the list contains a reference with the provided hash. |
||
| 169 | 3 | * |
|
| 170 | 3 | * @since 0.3 |
|
| 171 | 3 | * |
|
| 172 | * @param string $referenceHash |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function hasReferenceHash( $referenceHash ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Looks for the first Reference object in this list with the provided hash. |
||
| 182 | 8 | * Removes all occurences of that object. |
|
| 183 | 8 | * |
|
| 184 | * @since 0.3 |
||
| 185 | * |
||
| 186 | * @param string $referenceHash ` |
||
| 187 | */ |
||
| 188 | public function removeReferenceHash( $referenceHash ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the first Reference object with the provided hash, or |
||
| 204 | * null if there is no such reference in the list. |
||
| 205 | * |
||
| 206 | * @since 0.3 |
||
| 207 | * |
||
| 208 | * @param string $referenceHash |
||
| 209 | * |
||
| 210 | 14 | * @return Reference|null |
|
| 211 | */ |
||
| 212 | public function getReference( $referenceHash ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @see Serializable::serialize |
||
| 224 | * |
||
| 225 | * @since 2.1 |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function serialize() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @see Serializable::unserialize |
||
| 235 | * |
||
| 236 | * @since 2.1 |
||
| 237 | * |
||
| 238 | * @param string $serialized |
||
| 239 | */ |
||
| 240 | public function unserialize( $serialized ) { |
||
| 243 | 3 | ||
| 244 | /** |
||
| 245 | * @since 4.4 |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function isEmpty() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 255 | * |
||
| 256 | * @since 0.3 |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getValueHash() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @see Comparable::equals |
||
| 267 | * |
||
| 268 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 269 | * |
||
| 270 | * @since 0.3 |
||
| 271 | * |
||
| 272 | * @param mixed $target |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function equals( $target ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @see Countable::count |
||
| 287 | * |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | public function count() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @see IteratorAggregate::getIterator |
||
| 296 | * |
||
| 297 | * @since 5.0 |
||
| 298 | * |
||
| 299 | * @return Traversable |
||
| 300 | */ |
||
| 301 | public function getIterator() { |
||
| 304 | |||
| 305 | } |
||
| 306 |
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..