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 |
||
9 | class DateRangeTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | private $start; |
||
12 | private $end; |
||
13 | public function setUp() |
||
18 | |||
19 | /** @test */ |
||
20 | View Code Duplication | public function acceptStartEndParams() |
|
27 | |||
28 | /** @test */ |
||
29 | View Code Duplication | public function acceptStartEndArray() |
|
36 | |||
37 | /** @test */ |
||
38 | public function convertStringToDateTimeObject() |
||
44 | |||
45 | /** |
||
46 | * @test |
||
47 | * @expectedException InvalidArgumentException |
||
48 | */ |
||
49 | public function throwErrorWhenInvalidDatetimeString() |
||
53 | |||
54 | /** @test */ |
||
55 | public function defaultInterval() |
||
60 | |||
61 | /** @test */ |
||
62 | public function changeInterval() |
||
68 | |||
69 | /** @test */ |
||
70 | public function getDatePeriod() |
||
78 | |||
79 | /** @test */ |
||
80 | public function getIterator() |
||
95 | |||
96 | |||
97 | /** @test */ |
||
98 | public function testContains() |
||
106 | |||
107 | /** |
||
108 | * @test |
||
109 | * @expectedException InvalidArgumentException |
||
110 | */ |
||
111 | public function constructInvalidArrayArgument() |
||
116 | |||
117 | /** |
||
118 | * @test |
||
119 | * @expectedException InvalidArgumentException |
||
120 | */ |
||
121 | public function shouldHaveEndDateIsAfterThanStartDate() |
||
125 | } |
||
126 |
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.