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 |
||
11 | class MonthTest extends \PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @dataProvider createMonthByAnyDateInRangeProvider |
||
15 | * @param string $date |
||
16 | * @param string $startDate |
||
17 | * @param string $endDate |
||
18 | */ |
||
19 | public function testCreateMonthByAnyDateInRange($date, $startDate, $endDate) |
||
26 | |||
27 | /** |
||
28 | * @return array |
||
29 | */ |
||
30 | public function createMonthByAnyDateInRangeProvider() |
||
38 | |||
39 | /** |
||
40 | * @dataProvider nextMonthProvider |
||
41 | * @param string $date |
||
42 | * @param string $startDate |
||
43 | * @param string $endDate |
||
44 | */ |
||
45 | public function testNextMonth($date, $startDate, $endDate) |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | View Code Duplication | public function nextMonthProvider() |
|
76 | |||
77 | /** |
||
78 | * @dataProvider previousMonthProvider |
||
79 | * @param string $date |
||
80 | * @param string $startDate |
||
81 | * @param string $endDate |
||
82 | */ |
||
83 | View Code Duplication | public function testPreviousMonth($date, $startDate, $endDate) |
|
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | View Code Duplication | public function previousMonthProvider() |
|
114 | |||
115 | public function testGetNumberOfDays() |
||
120 | } |
||
121 |
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.