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 extends SplObjectStorage implements Comparable { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param Reference[]|Traversable $references |
||
| 34 | 29 | * |
|
| 35 | 29 | * @throws InvalidArgumentException |
|
| 36 | 8 | */ |
|
| 37 | public function __construct( $references = array() ) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Adds the provided reference to the list. |
||
| 53 | * |
||
| 54 | * @since 0.1 |
||
| 55 | * |
||
| 56 | * @param Reference $reference |
||
| 57 | * @param int|null $index |
||
| 58 | 14 | * |
|
| 59 | 14 | * @throws InvalidArgumentException |
|
| 60 | */ |
||
| 61 | public function addReference( Reference $reference, $index = null ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @see SplObjectStorage::attach |
||
| 76 | * |
||
| 77 | 14 | * @param Reference $reference |
|
| 78 | 14 | * @param mixed $data Unused in the ReferenceList class. |
|
| 79 | 14 | */ |
|
| 80 | 14 | public function attach( $reference, $data = null ) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @since 1.1 |
||
| 88 | * |
||
| 89 | * @param Snak[]|Snak $snaks |
||
| 90 | * @param Snak [$snak2,...] |
||
| 91 | 6 | * |
|
| 92 | 6 | * @throws InvalidArgumentException |
|
| 93 | 5 | */ |
|
| 94 | 5 | public function addNewReference( $snaks = array() /*...*/ ) { |
|
| 101 | |||
| 102 | /** |
||
| 103 | 2 | * @param Reference $reference |
|
| 104 | 2 | * @param int $index |
|
| 105 | 2 | */ |
|
| 106 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns if the list contains a reference with the same hash as the provided reference. |
||
| 131 | * |
||
| 132 | * @since 0.1 |
||
| 133 | * |
||
| 134 | * @param Reference $reference |
||
| 135 | 6 | * |
|
| 136 | 6 | * @return boolean |
|
| 137 | 6 | */ |
|
| 138 | public function hasReference( Reference $reference ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the index of a reference or false if the reference could not be found. |
||
| 145 | * |
||
| 146 | * @since 0.5 |
||
| 147 | * |
||
| 148 | * @param Reference $reference |
||
| 149 | 2 | * |
|
| 150 | 2 | * @return int|boolean |
|
| 151 | */ |
||
| 152 | 2 | public function indexOf( Reference $reference ) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
||
| 167 | * |
||
| 168 | * @since 0.1 |
||
| 169 | 3 | * |
|
| 170 | 3 | * @param Reference $reference |
|
| 171 | 3 | */ |
|
| 172 | public function removeReference( Reference $reference ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns if the list contains a reference with the provided hash. |
||
| 178 | * |
||
| 179 | * @since 0.3 |
||
| 180 | * |
||
| 181 | * @param string $referenceHash |
||
| 182 | 8 | * |
|
| 183 | 8 | * @return boolean |
|
| 184 | */ |
||
| 185 | public function hasReferenceHash( $referenceHash ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Removes the reference with the provided hash if it exists in the list. |
||
| 191 | * |
||
| 192 | * @since 0.3 |
||
| 193 | 5 | * |
|
| 194 | 5 | * @param string $referenceHash ` |
|
| 195 | */ |
||
| 196 | 5 | public function removeReferenceHash( $referenceHash ) { |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the reference with the provided hash, or null if there is no such reference in the list. |
||
| 206 | * |
||
| 207 | * @since 0.3 |
||
| 208 | * |
||
| 209 | * @param string $referenceHash |
||
| 210 | 14 | * |
|
| 211 | * @return Reference|null |
||
| 212 | */ |
||
| 213 | 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 Serializable::unserialize |
||
| 239 | * |
||
| 240 | * @since 2.1 |
||
| 241 | 3 | * |
|
| 242 | 3 | * @param string $data |
|
| 243 | 3 | */ |
|
| 244 | public function unserialize( $data ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @since 4.4 |
||
| 250 | 3 | * |
|
| 251 | 3 | * @return bool |
|
| 252 | */ |
||
| 253 | public function isEmpty() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Removes duplicates bases on hash value. |
||
| 259 | * |
||
| 260 | * @since 0.2 |
||
| 261 | */ |
||
| 262 | public function removeDuplicates() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
| 282 | * |
||
| 283 | * @since 0.3 |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getValueHash() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @see Comparable::equals |
||
| 294 | * |
||
| 295 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
| 296 | * |
||
| 297 | * @since 0.3 |
||
| 298 | * |
||
| 299 | * @param mixed $target |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function equals( $target ) { |
||
| 311 | |||
| 312 | } |
||
| 313 |