Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ComicTest 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 ComicTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | final class ComicTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | /** |
||
16 | * Verify basic behavior of getId. |
||
17 | * |
||
18 | * @test |
||
19 | * |
||
20 | * @return void |
||
21 | */ |
||
22 | public function getId() |
||
27 | |||
28 | /** |
||
29 | * Verify basic behavior of getDigitalId. |
||
30 | * |
||
31 | * @test |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function getDigitalId() |
||
40 | |||
41 | /** |
||
42 | * Verify basic behavior of getTitle. |
||
43 | * |
||
44 | * @test |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function getTitle() |
||
53 | |||
54 | /** |
||
55 | * Verify basic behavior of getIssueNumber. |
||
56 | * |
||
57 | * @test |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function getIssueNumber() |
||
66 | |||
67 | /** |
||
68 | * Verify basic behavior of getVariantDescription. |
||
69 | * |
||
70 | * @test |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | public function getVariantDescription() |
||
79 | |||
80 | /** |
||
81 | * Verify basic behavior of getDescription. |
||
82 | * |
||
83 | * @test |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | public function getDescription() |
||
92 | |||
93 | /** |
||
94 | * Verify basic behavior of getModified. |
||
95 | * |
||
96 | * @test |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | public function getModified() |
||
105 | |||
106 | /** |
||
107 | * Verify basic behavior of getIsbn. |
||
108 | * |
||
109 | * @test |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | public function getIsbn() |
||
118 | |||
119 | /** |
||
120 | * Verify basic behavior of getUpc. |
||
121 | * |
||
122 | * @test |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function getUpc() |
||
131 | |||
132 | /** |
||
133 | * Verify basic behavior of getDiamondCode. |
||
134 | * |
||
135 | * @test |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function getDiamondCode() |
||
144 | |||
145 | /** |
||
146 | * Verify basic behavior of getEan. |
||
147 | * |
||
148 | * @test |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | public function getEan() |
||
157 | |||
158 | /** |
||
159 | * Verify basic behavior of getIssn. |
||
160 | * |
||
161 | * @test |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function getIssn() |
||
170 | |||
171 | /** |
||
172 | * Verify basic behavior of getFormat. |
||
173 | * |
||
174 | * @test |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function getFormat() |
||
183 | |||
184 | /** |
||
185 | * Verify basic behavior of getPageCount. |
||
186 | * |
||
187 | * @test |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | public function getPageCount() |
||
196 | |||
197 | /** |
||
198 | * Verify basic behavior of getTextObjects. |
||
199 | * |
||
200 | * @test |
||
201 | * |
||
202 | * @return void |
||
203 | */ |
||
204 | public function getTextObjects() |
||
215 | |||
216 | /** |
||
217 | * Verify basic behavior of getResourceURI. |
||
218 | * |
||
219 | * @test |
||
220 | * |
||
221 | * @return void |
||
222 | */ |
||
223 | public function getResourceURI() |
||
228 | |||
229 | /** |
||
230 | * Verify basic behavior of getUrls. |
||
231 | * |
||
232 | * @test |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | View Code Duplication | public function getUrls() |
|
246 | |||
247 | /** |
||
248 | * Verify basic behavior of getSeries. |
||
249 | * |
||
250 | * @test |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | public function getSeries() |
||
263 | |||
264 | /** |
||
265 | * Verify basic behavior of getEvents. |
||
266 | * |
||
267 | * @test |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | View Code Duplication | public function getEvents() |
|
286 | |||
287 | /** |
||
288 | * Verify basic behavior of getStories. |
||
289 | * |
||
290 | * @test |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | View Code Duplication | public function getStories() |
|
309 | |||
310 | /** |
||
311 | * Verify basic behavior of getCreators. |
||
312 | * |
||
313 | * @test |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | View Code Duplication | public function getCreators() |
|
332 | |||
333 | /** |
||
334 | * Verify basic behavior of getCharacters. |
||
335 | * |
||
336 | * @test |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | View Code Duplication | public function getCharacters() |
|
355 | |||
356 | /** |
||
357 | * Verify basic behavior of getVariants. |
||
358 | * |
||
359 | * @test |
||
360 | * |
||
361 | * @return void |
||
362 | */ |
||
363 | View Code Duplication | public function getVariants() |
|
375 | |||
376 | /** |
||
377 | * Verify basic behavior of getCollections. |
||
378 | * |
||
379 | * @test |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | View Code Duplication | public function getCollections() |
|
395 | |||
396 | /** |
||
397 | * Verify basic behavior of getCollectedIssues. |
||
398 | * |
||
399 | * @test |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | View Code Duplication | public function getCollectedIssues() |
|
415 | |||
416 | /** |
||
417 | * Verify basic behavior of getDates. |
||
418 | * |
||
419 | * @test |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | View Code Duplication | public function getDates() |
|
433 | |||
434 | /** |
||
435 | * Verify basic behavior of getPrices. |
||
436 | * |
||
437 | * @test |
||
438 | * |
||
439 | * @return void |
||
440 | */ |
||
441 | View Code Duplication | public function getPrices() |
|
451 | |||
452 | /** |
||
453 | * Verify basic behavior of getThumbnail. |
||
454 | * |
||
455 | * @test |
||
456 | * |
||
457 | * @return void |
||
458 | */ |
||
459 | public function getThumbnail() |
||
466 | |||
467 | /** |
||
468 | * Verify basic behavior of getImages. |
||
469 | * |
||
470 | * @test |
||
471 | * |
||
472 | * @return void |
||
473 | */ |
||
474 | View Code Duplication | public function getImages() |
|
484 | |||
485 | /** |
||
486 | * Verify basic behavior of findAll(). |
||
487 | * |
||
488 | * @test |
||
489 | * @covers ::findAll |
||
490 | * |
||
491 | * @return void |
||
492 | */ |
||
493 | public function findAll() |
||
503 | |||
504 | /** |
||
505 | * Verify query parameters are set properly with findAll(). |
||
506 | * |
||
507 | * @test |
||
508 | * @covers ::findAll |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | public function findAllParametersSetProperly() |
||
551 | |||
552 | /** |
||
553 | * Helper method to return test comic input data |
||
554 | * |
||
555 | * @return array |
||
556 | */ |
||
557 | private static function getTestData() |
||
694 | } |
||
695 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: