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