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 | final class DateTimeZoneTest extends TestCase |
||
12 | { |
||
13 | /** |
||
14 | * Verify basic behavior of fromString() |
||
15 | * |
||
16 | * @test |
||
17 | * @covers ::fromString |
||
18 | * |
||
19 | * @return void |
||
20 | */ |
||
21 | public function fromString() |
||
27 | |||
28 | /** |
||
29 | * Verify behavior of fromString() with default timezone. |
||
30 | * |
||
31 | * @test |
||
32 | * @covers ::fromString |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function fromStringDefaultTimeZone() |
||
41 | |||
42 | /** |
||
43 | * Verify fromString() defaults to UTC on error. |
||
44 | * |
||
45 | * @test |
||
46 | * @covers ::fromString |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | public function fromStringWithInvalidAbbreviation() |
||
54 | |||
55 | /** |
||
56 | * Verify fromString() correctly converts edge case timezones. |
||
57 | * |
||
58 | * @param string $abbreviation The abbreviation to tests. |
||
59 | * @param string $expected The expected result from the fromString() call. |
||
60 | * |
||
61 | * @test |
||
62 | * @covers ::fromString |
||
63 | * @dataProvider getOutliers |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | View Code Duplication | public function fromStringOutliers($abbreviation, $expected) |
|
78 | |||
79 | /** |
||
80 | * Dataprovider for outlier testing |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getOutliers() |
||
96 | |||
97 | /** |
||
98 | * Verify basic behavior of fromOffset() |
||
99 | * |
||
100 | * @test |
||
101 | * @covers ::fromOffset |
||
102 | * @uses \SubjectivePHP\Util\DateTimeZone::fromString |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function fromOffset() |
||
111 | |||
112 | /** |
||
113 | * Verify behavior of fromOffset() when $gmtOffset is not an integer. |
||
114 | * |
||
115 | * @test |
||
116 | * @covers ::fromOffset |
||
117 | * @expectedException \InvalidArgumentException |
||
118 | * @expectedExceptionMessage $gmtOffset must be an integer |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function fromOffsetInvalidOffsetValue() |
||
126 | |||
127 | /** |
||
128 | * Verify behavior of fromOffset() when $isDaylightSavings is not a boolean. |
||
129 | * |
||
130 | * @test |
||
131 | * @covers ::fromOffset |
||
132 | * @expectedException \InvalidArgumentException |
||
133 | * @expectedExceptionMessage $isDaylightSavings must be a boolean |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | public function fromOffsetInvalidDSTValue() |
||
141 | |||
142 | /** |
||
143 | * Verify basic behavior of getLongName(). |
||
144 | * |
||
145 | * @test |
||
146 | * @covers ::getLongName |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | public function getLongName() |
||
155 | |||
156 | /** |
||
157 | * Verify basic behavior of getLongName(). |
||
158 | * |
||
159 | * @test |
||
160 | * @covers ::getLongName |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function getLongNameWithLongName() |
||
169 | |||
170 | /** |
||
171 | * Verify behavior of getLongName() with outlier. |
||
172 | * |
||
173 | * @test |
||
174 | * @covers ::getLongName |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function getLongNameOutlier() |
||
183 | } |
||
184 |
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.