Complex classes like HashArray 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 HashArray, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class HashArray extends ArrayObject implements Hashable, Comparable { |
||
36 | |||
37 | /** |
||
38 | * Maps element hashes to their offsets. |
||
39 | * |
||
40 | * @since 0.1 |
||
41 | * |
||
42 | * @var array [ element hash (string) => array [ element offset (string|int) ] | element offset (string|int) ] |
||
43 | */ |
||
44 | protected $offsetHashes = []; |
||
45 | |||
46 | /** |
||
47 | * If duplicate values (based on hash) should be accepted or not. |
||
48 | * |
||
49 | * @since 0.3 |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $acceptDuplicates = false; |
||
54 | |||
55 | /** |
||
56 | * @var integer |
||
57 | */ |
||
58 | protected $indexOffset = 0; |
||
59 | |||
60 | /** |
||
61 | * Returns the name of an interface/class that the element should implement/extend. |
||
62 | * |
||
63 | * @since 0.4 |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | abstract public function getObjectType(); |
||
68 | |||
69 | /** |
||
70 | * @see ArrayObject::__construct |
||
71 | * |
||
72 | * @param array|Traversable|null $input |
||
73 | * @param int $flags |
||
74 | * @param string $iteratorClass |
||
75 | * |
||
76 | * @throws InvalidArgumentException |
||
77 | */ |
||
78 | public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) { |
||
91 | |||
92 | /** |
||
93 | * Finds a new offset for when appending an element. |
||
94 | * The base class does this, so it would be better to integrate, |
||
95 | * but there does not appear to be any way to do this... |
||
96 | * |
||
97 | * @return integer |
||
98 | */ |
||
99 | protected function getNewOffset() { |
||
106 | |||
107 | /** |
||
108 | * Gets called before a new element is added to the ArrayObject. |
||
109 | * |
||
110 | * At this point the index is always set (ie not null) and the |
||
111 | * value is always of the type returned by @see getObjectType. |
||
112 | * |
||
113 | * Should return a boolean. When false is returned the element |
||
114 | * does not get added to the ArrayObject. |
||
115 | * |
||
116 | * @since 0.1 |
||
117 | * |
||
118 | * @param int|string $index |
||
119 | * @param Hashable $hashable |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | protected function preSetElement( $index, $hashable ) { |
||
146 | |||
147 | /** |
||
148 | * Returns if there is an element with the provided hash. |
||
149 | * |
||
150 | * @since 0.1 |
||
151 | * |
||
152 | * @param string $elementHash |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | public function hasElementHash( $elementHash ) { |
||
159 | |||
160 | /** |
||
161 | * Returns if there is an element with the same hash as the provided element in the list. |
||
162 | * |
||
163 | * @since 0.1 |
||
164 | * |
||
165 | * @param Hashable $element |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function hasElement( Hashable $element ) { |
||
172 | |||
173 | /** |
||
174 | * Removes the element with the hash of the provided element, if there is such an element in the list. |
||
175 | * |
||
176 | * @since 0.1 |
||
177 | * |
||
178 | * @param Hashable $element |
||
179 | */ |
||
180 | public function removeElement( Hashable $element ) { |
||
183 | |||
184 | /** |
||
185 | * Removes the element with the provided hash, if there is such an element in the list. |
||
186 | * |
||
187 | * @since 0.1 |
||
188 | * |
||
189 | * @param string $elementHash |
||
190 | */ |
||
191 | public function removeByElementHash( $elementHash ) { |
||
202 | |||
203 | /** |
||
204 | * Adds the provided element to the list if there is no element with the same hash yet. |
||
205 | * |
||
206 | * @since 0.1 |
||
207 | * |
||
208 | * @param Hashable $element |
||
209 | * |
||
210 | * @return bool Indicates if the element was added or not. |
||
211 | */ |
||
212 | public function addElement( Hashable $element ) { |
||
221 | |||
222 | /** |
||
223 | * Returns the element with the provided hash or false if there is no such element. |
||
224 | * |
||
225 | * @since 0.1 |
||
226 | * |
||
227 | * @param string $elementHash |
||
228 | * |
||
229 | * @return mixed|bool |
||
230 | */ |
||
231 | public function getByElementHash( $elementHash ) { |
||
245 | |||
246 | /** |
||
247 | * @see ArrayObject::offsetUnset |
||
248 | * |
||
249 | * @since 0.1 |
||
250 | * |
||
251 | * @param mixed $index |
||
252 | */ |
||
253 | public function offsetUnset( $index ) { |
||
279 | |||
280 | /** |
||
281 | * @see Hashable::getHash |
||
282 | * |
||
283 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
284 | * |
||
285 | * @since 0.1 |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function getHash() { |
||
293 | |||
294 | /** |
||
295 | * @see Comparable::equals |
||
296 | * |
||
297 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
298 | * |
||
299 | * @since 0.3 |
||
300 | * |
||
301 | * @param mixed $target |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function equals( $target ) { |
||
313 | |||
314 | /** |
||
315 | * Returns if the hash indices are up to date. |
||
316 | * For an HashArray with immutable objects this should always be the case. |
||
317 | * For one with mutable objects it's the responsibility of the mutating code |
||
318 | * to keep the indices up to date (see class documentation) and thus possible |
||
319 | * this has not been done since the last update, thus causing a state where |
||
320 | * one or more indices are out of date. |
||
321 | * |
||
322 | * @since 0.4 |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | public function indicesAreUpToDate() { |
||
340 | |||
341 | /** |
||
342 | * Removes and adds all elements, ensuring the indices are up to date. |
||
343 | * |
||
344 | * @since 0.4 |
||
345 | */ |
||
346 | public function rebuildIndices() { |
||
356 | |||
357 | /** |
||
358 | * @see ArrayObject::append |
||
359 | * |
||
360 | * @param mixed $value |
||
361 | */ |
||
362 | public function append( $value ) { |
||
365 | |||
366 | /** |
||
367 | * @see ArrayObject::offsetSet() |
||
368 | * |
||
369 | * @param mixed $index |
||
370 | * @param mixed $value |
||
371 | */ |
||
372 | public function offsetSet( $index, $value ) { |
||
375 | |||
376 | /** |
||
377 | * Returns if the provided value has the same type as the elements |
||
378 | * that can be added to this ArrayObject. |
||
379 | * |
||
380 | * @param mixed $value |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | protected function hasValidType( $value ) { |
||
388 | |||
389 | /** |
||
390 | * Method that actually sets the element and holds |
||
391 | * all common code needed for set operations, including |
||
392 | * type checking and offset resolving. |
||
393 | * |
||
394 | * If you want to do additional indexing or have code that |
||
395 | * otherwise needs to be executed whenever an element is added, |
||
396 | * you can overload @see preSetElement. |
||
397 | * |
||
398 | * @param mixed $index |
||
399 | * @param mixed $value |
||
400 | * |
||
401 | * @throws InvalidArgumentException |
||
402 | */ |
||
403 | protected function setElement( $index, $value ) { |
||
418 | |||
419 | /** |
||
420 | * @see Serializable::serialize |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function serialize() { |
||
430 | |||
431 | /** |
||
432 | * @see Serializable::unserialize |
||
433 | * |
||
434 | * @param string $serialized |
||
435 | */ |
||
436 | public function unserialize( $serialized ) { |
||
447 | |||
448 | /** |
||
449 | * Returns if the ArrayObject has no elements. |
||
450 | * |
||
451 | * @return bool |
||
452 | */ |
||
453 | public function isEmpty() { |
||
456 | |||
457 | } |
||
458 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.