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 |
||
16 | View Code Duplication | final class DateTimeTest extends TestCase |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * Verify basic behavior of isWeekDay(). |
||
20 | * |
||
21 | * @test |
||
22 | * @covers ::isWeekDay |
||
23 | * |
||
24 | * @return void |
||
25 | */ |
||
26 | public function isWeekDay() |
||
31 | |||
32 | /** |
||
33 | * Verify basic behavior of isWeekendDay(). |
||
34 | * |
||
35 | * @test |
||
36 | * @covers ::isWeekendDay |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | public function isWeekendDay() |
||
45 | |||
46 | /** |
||
47 | * Verify basic behavior of isSameDay(). |
||
48 | * |
||
49 | * @test |
||
50 | * @covers ::isSameDay |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function isSameDay() |
||
62 | |||
63 | /** |
||
64 | * Verify basic behavior of isDaylightSavings(). |
||
65 | * |
||
66 | * @test |
||
67 | * @covers ::isDaylightSavings |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | public function isDaylightSavings() |
||
76 | |||
77 | /** |
||
78 | * Verify basic behavior of isInRange(). |
||
79 | * |
||
80 | * @test |
||
81 | * @covers ::isInRange |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function isInRange() |
||
92 | |||
93 | /** |
||
94 | * Verify error behavior of isInRange(). |
||
95 | * |
||
96 | * @test |
||
97 | * @covers ::isInRange |
||
98 | * @expectedException \DomainException |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function isInRangeWithInvalidRange() |
||
109 | |||
110 | /** |
||
111 | * Verify basic behavior of asAgoString(). |
||
112 | * |
||
113 | * @test |
||
114 | * @covers ::asAgoString |
||
115 | * @dataProvider provideAgoStringData |
||
116 | * |
||
117 | * @param string $dateTimeString The date/time string. |
||
118 | * @param string $expectedAgoString The expected ago string. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function asAgoString(string $dateTimeString, string $expectedAgoString) |
||
126 | |||
127 | /** |
||
128 | * Returns data for the asAgoString test. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function provideAgoStringData() |
||
150 | |||
151 | /** |
||
152 | * Verify error behavior of asAgoString(). |
||
153 | * |
||
154 | * @test |
||
155 | * @covers ::asAgoString |
||
156 | * @expectedException \DomainException |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function asAgoStringWithFutureDate() |
||
164 | } |
||
165 |
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.