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:
1 | <?php |
||
13 | class CollectionsVisitorTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | private $visitor; |
||
16 | |||
17 | public function setUp() |
||
21 | |||
22 | /** |
||
23 | * It should visit all comparators. |
||
24 | * |
||
25 | * @dataProvider provideComparator |
||
26 | */ |
||
27 | public function testComparator(string $type, string $expectedOperator) |
||
35 | |||
36 | View Code Duplication | public function provideComparator() |
|
69 | |||
70 | /** |
||
71 | * It should throw an exception on unsupported comparators. |
||
72 | * |
||
73 | * @dataProvider provideUnsupportedComparators |
||
74 | * |
||
75 | * @expectedException Psi\Component\ObjectAgent\Exception\BadMethodCallException |
||
76 | */ |
||
77 | public function testUnsupportedComparators($comparator) |
||
81 | |||
82 | public function provideUnsupportedComparators() |
||
93 | |||
94 | /** |
||
95 | * It should visit complex comparators. |
||
96 | * |
||
97 | * @dataProvider provideComplexComparator |
||
98 | */ |
||
99 | public function testComplexComparator(string $type, $value, \Closure $assertion) |
||
104 | |||
105 | public function provideComplexComparator() |
||
134 | |||
135 | /** |
||
136 | * It should visit and composites. |
||
137 | */ |
||
138 | View Code Duplication | public function testVisitCompositeAnd() |
|
148 | |||
149 | /** |
||
150 | * It should visit or composites. |
||
151 | */ |
||
152 | View Code Duplication | public function testVisitCompositeOr() |
|
162 | } |
||
163 |
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.