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 |
||
14 | final class DateTimeZoneUtilTest extends TestCase |
||
15 | { |
||
16 | /** |
||
17 | * Verify basic behavior of fromString() |
||
18 | * |
||
19 | * @test |
||
20 | * @covers ::fromString |
||
21 | */ |
||
22 | public function fromString() |
||
28 | |||
29 | /** |
||
30 | * Verify behavior of fromString() with default timezone. |
||
31 | * |
||
32 | * @test |
||
33 | * @covers ::fromString |
||
34 | */ |
||
35 | public function fromStringDefaultTimeZone() |
||
40 | |||
41 | /** |
||
42 | * Verify fromString() defaults to UTC on error. |
||
43 | * |
||
44 | * @test |
||
45 | * @covers ::fromString |
||
46 | */ |
||
47 | public function fromStringWithInvalidAbbreviation() |
||
51 | |||
52 | /** |
||
53 | * Verify fromString() correctly converts edge case timezones. |
||
54 | * |
||
55 | * @param string $abbreviation The abbreviation to tests. |
||
56 | * @param string $expected The expected result from the fromString() call. |
||
57 | * |
||
58 | * @test |
||
59 | * @covers ::fromString |
||
60 | * @dataProvider getOutliers |
||
61 | */ |
||
62 | View Code Duplication | public function fromStringOutliers(string $abbreviation, string $expected) |
|
73 | |||
74 | /** |
||
75 | * Dataprovider for outlier testing |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getOutliers() : array |
||
91 | |||
92 | /** |
||
93 | * Verify basic behavior of fromOffset() |
||
94 | * |
||
95 | * @test |
||
96 | * @covers ::fromOffset |
||
97 | */ |
||
98 | public function fromOffset() |
||
103 | |||
104 | /** |
||
105 | * Verify basic behavior of getLongName(). |
||
106 | * |
||
107 | * @test |
||
108 | * @covers ::getLongName |
||
109 | */ |
||
110 | public function getLongName() |
||
115 | |||
116 | /** |
||
117 | * Verify basic behavior of getLongName(). |
||
118 | * |
||
119 | * @test |
||
120 | * @covers ::getLongName |
||
121 | */ |
||
122 | public function getLongNameWithLongName() |
||
127 | |||
128 | /** |
||
129 | * Verify behavior of getLongName() with outlier. |
||
130 | * |
||
131 | * @test |
||
132 | * @covers ::getLongName |
||
133 | */ |
||
134 | public function getLongNameOutlier() |
||
139 | } |
||
140 |
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.