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 EngineeringTest 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 EngineeringTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class EngineeringTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | /** |
||
11 | * @var \PhpSpreadsheetTests\Custom\ComplexAssert |
||
12 | */ |
||
13 | protected $complexAssert; |
||
14 | |||
15 | public function setUp() |
||
20 | |||
21 | public function tearDown() |
||
25 | |||
26 | /** |
||
27 | * @dataProvider providerBESSELI |
||
28 | */ |
||
29 | public function testBESSELI() |
||
36 | |||
37 | public function providerBESSELI() |
||
41 | |||
42 | /** |
||
43 | * @dataProvider providerBESSELJ |
||
44 | */ |
||
45 | public function testBESSELJ() |
||
52 | |||
53 | public function providerBESSELJ() |
||
57 | |||
58 | /** |
||
59 | * @dataProvider providerBESSELK |
||
60 | */ |
||
61 | public function testBESSELK() |
||
68 | |||
69 | public function providerBESSELK() |
||
73 | |||
74 | /** |
||
75 | * @dataProvider providerBESSELY |
||
76 | */ |
||
77 | public function testBESSELY() |
||
84 | |||
85 | public function providerBESSELY() |
||
89 | |||
90 | /** |
||
91 | * @dataProvider providerCOMPLEX |
||
92 | */ |
||
93 | public function testCOMPLEX() |
||
100 | |||
101 | public function providerCOMPLEX() |
||
105 | |||
106 | /** |
||
107 | * @dataProvider providerIMAGINARY |
||
108 | */ |
||
109 | public function testIMAGINARY() |
||
116 | |||
117 | public function providerIMAGINARY() |
||
121 | |||
122 | /** |
||
123 | * @dataProvider providerIMREAL |
||
124 | */ |
||
125 | public function testIMREAL() |
||
132 | |||
133 | public function providerIMREAL() |
||
137 | |||
138 | /** |
||
139 | * @dataProvider providerIMABS |
||
140 | */ |
||
141 | public function testIMABS() |
||
148 | |||
149 | public function providerIMABS() |
||
153 | |||
154 | /** |
||
155 | * @dataProvider providerIMARGUMENT |
||
156 | * @group fail19 |
||
157 | */ |
||
158 | public function testIMARGUMENT() |
||
165 | |||
166 | public function providerIMARGUMENT() |
||
170 | |||
171 | /** |
||
172 | * @dataProvider providerIMCONJUGATE |
||
173 | */ |
||
174 | View Code Duplication | public function testIMCONJUGATE() |
|
181 | |||
182 | public function providerIMCONJUGATE() |
||
186 | |||
187 | /** |
||
188 | * @dataProvider providerIMCOS |
||
189 | */ |
||
190 | View Code Duplication | public function testIMCOS() |
|
197 | |||
198 | public function providerIMCOS() |
||
202 | |||
203 | /** |
||
204 | * @dataProvider providerIMDIV |
||
205 | * @group fail19 |
||
206 | */ |
||
207 | View Code Duplication | public function testIMDIV() |
|
216 | |||
217 | public function providerIMDIV() |
||
221 | |||
222 | /** |
||
223 | * @dataProvider providerIMEXP |
||
224 | */ |
||
225 | View Code Duplication | public function testIMEXP() |
|
232 | |||
233 | public function providerIMEXP() |
||
237 | |||
238 | /** |
||
239 | * @dataProvider providerIMLN |
||
240 | */ |
||
241 | View Code Duplication | public function testIMLN() |
|
248 | |||
249 | public function providerIMLN() |
||
253 | |||
254 | /** |
||
255 | * @dataProvider providerIMLOG2 |
||
256 | */ |
||
257 | View Code Duplication | public function testIMLOG2() |
|
264 | |||
265 | public function providerIMLOG2() |
||
269 | |||
270 | /** |
||
271 | * @dataProvider providerIMLOG10 |
||
272 | */ |
||
273 | View Code Duplication | public function testIMLOG10() |
|
280 | |||
281 | public function providerIMLOG10() |
||
285 | |||
286 | /** |
||
287 | * @dataProvider providerIMPOWER |
||
288 | * @group fail19 |
||
289 | */ |
||
290 | View Code Duplication | public function testIMPOWER() |
|
299 | |||
300 | public function providerIMPOWER() |
||
304 | |||
305 | /** |
||
306 | * @dataProvider providerIMPRODUCT |
||
307 | */ |
||
308 | View Code Duplication | public function testIMPRODUCT() |
|
315 | |||
316 | public function providerIMPRODUCT() |
||
320 | |||
321 | /** |
||
322 | * @dataProvider providerIMSIN |
||
323 | */ |
||
324 | View Code Duplication | public function testIMSIN() |
|
331 | |||
332 | public function providerIMSIN() |
||
336 | |||
337 | /** |
||
338 | * @dataProvider providerIMSQRT |
||
339 | */ |
||
340 | View Code Duplication | public function testIMSQRT() |
|
347 | |||
348 | public function providerIMSQRT() |
||
352 | |||
353 | /** |
||
354 | * @dataProvider providerIMSUB |
||
355 | * @group fail19 |
||
356 | */ |
||
357 | View Code Duplication | public function testIMSUB() |
|
366 | |||
367 | public function providerIMSUB() |
||
371 | |||
372 | /** |
||
373 | * @dataProvider providerIMSUM |
||
374 | * @group fail19 |
||
375 | */ |
||
376 | View Code Duplication | public function testIMSUM() |
|
383 | |||
384 | public function providerIMSUM() |
||
388 | |||
389 | /** |
||
390 | * @dataProvider providerERF |
||
391 | */ |
||
392 | public function testERF() |
||
399 | |||
400 | public function providerERF() |
||
404 | |||
405 | /** |
||
406 | * @dataProvider providerERFC |
||
407 | */ |
||
408 | public function testERFC() |
||
415 | |||
416 | public function providerERFC() |
||
420 | |||
421 | /** |
||
422 | * @dataProvider providerBIN2DEC |
||
423 | */ |
||
424 | public function testBIN2DEC() |
||
431 | |||
432 | public function providerBIN2DEC() |
||
436 | |||
437 | /** |
||
438 | * @dataProvider providerBIN2HEX |
||
439 | */ |
||
440 | public function testBIN2HEX() |
||
447 | |||
448 | public function providerBIN2HEX() |
||
452 | |||
453 | /** |
||
454 | * @dataProvider providerBIN2OCT |
||
455 | */ |
||
456 | public function testBIN2OCT() |
||
463 | |||
464 | public function providerBIN2OCT() |
||
468 | |||
469 | /** |
||
470 | * @dataProvider providerDEC2BIN |
||
471 | */ |
||
472 | public function testDEC2BIN() |
||
479 | |||
480 | public function providerDEC2BIN() |
||
484 | |||
485 | /** |
||
486 | * @dataProvider providerDEC2HEX |
||
487 | */ |
||
488 | public function testDEC2HEX() |
||
495 | |||
496 | public function providerDEC2HEX() |
||
500 | |||
501 | /** |
||
502 | * @dataProvider providerDEC2OCT |
||
503 | */ |
||
504 | public function testDEC2OCT() |
||
511 | |||
512 | public function providerDEC2OCT() |
||
516 | |||
517 | /** |
||
518 | * @dataProvider providerHEX2BIN |
||
519 | */ |
||
520 | public function testHEX2BIN() |
||
527 | |||
528 | public function providerHEX2BIN() |
||
532 | |||
533 | /** |
||
534 | * @dataProvider providerHEX2DEC |
||
535 | */ |
||
536 | public function testHEX2DEC() |
||
543 | |||
544 | public function providerHEX2DEC() |
||
548 | |||
549 | /** |
||
550 | * @dataProvider providerHEX2OCT |
||
551 | */ |
||
552 | public function testHEX2OCT() |
||
559 | |||
560 | public function providerHEX2OCT() |
||
564 | |||
565 | /** |
||
566 | * @dataProvider providerOCT2BIN |
||
567 | */ |
||
568 | public function testOCT2BIN() |
||
575 | |||
576 | public function providerOCT2BIN() |
||
580 | |||
581 | /** |
||
582 | * @dataProvider providerOCT2DEC |
||
583 | */ |
||
584 | public function testOCT2DEC() |
||
591 | |||
592 | public function providerOCT2DEC() |
||
596 | |||
597 | /** |
||
598 | * @dataProvider providerOCT2HEX |
||
599 | */ |
||
600 | public function testOCT2HEX() |
||
607 | |||
608 | public function providerOCT2HEX() |
||
612 | |||
613 | /** |
||
614 | * @dataProvider providerDELTA |
||
615 | */ |
||
616 | public function testDELTA() |
||
623 | |||
624 | public function providerDELTA() |
||
628 | |||
629 | /** |
||
630 | * @dataProvider providerGESTEP |
||
631 | */ |
||
632 | public function testGESTEP() |
||
639 | |||
640 | public function providerGESTEP() |
||
644 | |||
645 | public function testGetConversionGroups() |
||
650 | |||
651 | public function testGetConversionGroupUnits() |
||
656 | |||
657 | public function testGetConversionGroupUnitDetails() |
||
662 | |||
663 | public function testGetConversionMultipliers() |
||
668 | |||
669 | /** |
||
670 | * @dataProvider providerCONVERTUOM |
||
671 | */ |
||
672 | public function testCONVERTUOM() |
||
679 | |||
680 | public function providerCONVERTUOM() |
||
684 | } |
||
685 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.