Complex classes like Diff 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 Diff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Diff extends ArrayObject implements DiffOp { |
||
26 | |||
27 | /** |
||
28 | * @var bool|null |
||
29 | */ |
||
30 | private $isAssociative = null; |
||
31 | |||
32 | /** |
||
33 | * Pointers to the operations of certain types for quick lookup. |
||
34 | * |
||
35 | * @var array[] |
||
36 | */ |
||
37 | private $typePointers = [ |
||
38 | 'add' => [], |
||
39 | 'remove' => [], |
||
40 | 'change' => [], |
||
41 | 'list' => [], |
||
42 | 'map' => [], |
||
43 | 'diff' => [], |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $indexOffset = 0; |
||
50 | |||
51 | /** |
||
52 | * @since 0.1 |
||
53 | * |
||
54 | * @param DiffOp[] $operations |
||
55 | * @param bool|null $isAssociative |
||
56 | * |
||
57 | * @throws InvalidArgumentException |
||
58 | */ |
||
59 | 107 | public function __construct( array $operations = [], $isAssociative = null ) { |
|
76 | |||
77 | /** |
||
78 | * @since 0.1 |
||
79 | * |
||
80 | * @return DiffOp[] |
||
81 | */ |
||
82 | 122 | public function getOperations(): array { |
|
85 | |||
86 | /** |
||
87 | * @since 0.1 |
||
88 | * |
||
89 | * @param string $type |
||
90 | * |
||
91 | * @return DiffOp[] |
||
92 | */ |
||
93 | 23 | public function getTypeOperations( string $type ): array { |
|
99 | |||
100 | /** |
||
101 | * @since 0.1 |
||
102 | * |
||
103 | * @param DiffOp[] $operations |
||
104 | */ |
||
105 | 7 | public function addOperations( array $operations ) { |
|
110 | |||
111 | /** |
||
112 | * Gets called before a new element is added to the ArrayObject. |
||
113 | * |
||
114 | * At this point the index is always set (ie not null) and the |
||
115 | * value is always of the type returned by @see getObjectType. |
||
116 | * |
||
117 | * Should return a boolean. When false is returned the element |
||
118 | * does not get added to the ArrayObject. |
||
119 | * |
||
120 | * @param int|string $index |
||
121 | * @param DiffOp $value |
||
122 | * |
||
123 | * @return bool |
||
124 | * @throws InvalidArgumentException |
||
125 | */ |
||
126 | 86 | private function preSetElement( $index, DiffOp $value ): bool { |
|
140 | |||
141 | /** |
||
142 | * @see Serializable::unserialize |
||
143 | * |
||
144 | * @since 0.1 |
||
145 | * |
||
146 | * @param string $serialization |
||
147 | */ |
||
148 | 43 | public function unserialize( $serialization ) { |
|
165 | |||
166 | /** |
||
167 | * @since 0.1 |
||
168 | * |
||
169 | * @return DiffOpAdd[] |
||
170 | */ |
||
171 | 7 | public function getAdditions(): array { |
|
174 | |||
175 | /** |
||
176 | * @since 0.1 |
||
177 | * |
||
178 | * @return DiffOpRemove[] |
||
179 | */ |
||
180 | 7 | public function getRemovals(): array { |
|
183 | |||
184 | /** |
||
185 | * @since 0.1 |
||
186 | * |
||
187 | * @return DiffOpChange[] |
||
188 | */ |
||
189 | 2 | public function getChanges(): array { |
|
190 | 2 | return $this->getTypeOperations( 'change' ); |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @since 0.1 |
||
195 | * |
||
196 | * @return array of mixed |
||
197 | */ |
||
198 | 1 | public function getAddedValues(): array { |
|
206 | |||
207 | /** |
||
208 | * @since 0.1 |
||
209 | * |
||
210 | * @return array of mixed |
||
211 | */ |
||
212 | 1 | public function getRemovedValues(): array { |
|
220 | |||
221 | /** |
||
222 | * @see DiffOp::isAtomic |
||
223 | * |
||
224 | * @since 0.1 |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | 72 | public function isAtomic(): bool { |
|
231 | |||
232 | /** |
||
233 | * @see DiffOp::getType |
||
234 | * |
||
235 | * @since 0.1 |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | 152 | public function getType(): string { |
|
242 | |||
243 | /** |
||
244 | * Counts the number of atomic operations in the diff. |
||
245 | * This means the size of a diff with as elements only empty diffs will be 0. |
||
246 | * Or that the size of a diff with one atomic operation and one diff that itself |
||
247 | * holds two atomic operations will be 3. |
||
248 | * |
||
249 | * @see Countable::count |
||
250 | * |
||
251 | * @since 0.1 |
||
252 | * |
||
253 | * @return int |
||
254 | */ |
||
255 | 74 | public function count(): int { |
|
267 | |||
268 | /** |
||
269 | * @since 0.3 |
||
270 | */ |
||
271 | 1 | public function removeEmptyOperations() { |
|
278 | |||
279 | /** |
||
280 | * Returns the value of the isAssociative flag. |
||
281 | * |
||
282 | * @since 0.4 |
||
283 | * |
||
284 | * @return bool|null |
||
285 | */ |
||
286 | 9 | public function isAssociative() { |
|
289 | |||
290 | /** |
||
291 | * Returns if the diff looks associative or not. |
||
292 | * This first checks the isAssociative flag and in case its null checks |
||
293 | * if there are any non-add-non-remove operations. |
||
294 | * |
||
295 | * @since 0.4 |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | 9 | public function looksAssociative(): bool { |
|
302 | |||
303 | /** |
||
304 | * Returns if the diff can be non-associative. |
||
305 | * This means it does not contain any non-add-non-remove operations. |
||
306 | * |
||
307 | * @since 0.4 |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | 16 | public function hasAssociativeOperations(): bool { |
|
317 | |||
318 | /** |
||
319 | * Returns the Diff in array form where nested DiffOps are also turned into their array form. |
||
320 | * |
||
321 | * @see DiffOp::toArray |
||
322 | * |
||
323 | * @since 0.5 |
||
324 | * |
||
325 | * @param callable|null $valueConverter optional callback used to convert any |
||
326 | * complex values to arrays. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | 108 | public function toArray( callable $valueConverter = null ): array { |
|
343 | |||
344 | /** |
||
345 | * @since 2.0 |
||
346 | * |
||
347 | * @param mixed $target |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | 12 | public function equals( $target ) { |
|
363 | |||
364 | /** |
||
365 | * Finds a new offset for when appending an element. |
||
366 | * The base class does this, so it would be better to integrate, |
||
367 | * but there does not appear to be any way to do this... |
||
368 | * |
||
369 | * @return int |
||
370 | */ |
||
371 | 23 | private function getNewOffset(): int { |
|
378 | |||
379 | /** |
||
380 | * @see ArrayObject::append |
||
381 | * |
||
382 | * @since 0.1 |
||
383 | * |
||
384 | * @param mixed $value |
||
385 | */ |
||
386 | 19 | public function append( $value ) { |
|
389 | |||
390 | /** |
||
391 | * @see ArrayObject::offsetSet() |
||
392 | * |
||
393 | * @since 0.1 |
||
394 | * |
||
395 | * @param int|string $index |
||
396 | * @param mixed $value |
||
397 | */ |
||
398 | 74 | public function offsetSet( $index, $value ) { |
|
401 | |||
402 | /** |
||
403 | * Method that actually sets the element and holds |
||
404 | * all common code needed for set operations, including |
||
405 | * type checking and offset resolving. |
||
406 | * |
||
407 | * If you want to do additional indexing or have code that |
||
408 | * otherwise needs to be executed whenever an element is added, |
||
409 | * you can overload @see preSetElement. |
||
410 | * |
||
411 | * @param int|string|null $index |
||
412 | * @param mixed $value |
||
413 | * |
||
414 | * @throws InvalidArgumentException |
||
415 | */ |
||
416 | 88 | private function setElement( $index, $value ) { |
|
431 | |||
432 | /** |
||
433 | * @see Serializable::serialize |
||
434 | * |
||
435 | * @since 0.1 |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | 43 | public function serialize() { |
|
451 | |||
452 | /** |
||
453 | * @since 0.1 |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | 15 | public function isEmpty(): bool { |
|
467 | |||
468 | } |
||
469 |
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.