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 |
||
12 | final class DateTimeZoneTest extends TestCase |
||
13 | { |
||
14 | /** |
||
15 | * Verify basic usage of filter(). |
||
16 | * |
||
17 | * @test |
||
18 | * @covers ::filter |
||
19 | * |
||
20 | * @return void |
||
21 | */ |
||
22 | View Code Duplication | public function filter() |
|
29 | |||
30 | /** |
||
31 | * Verify behavior of filter() when $allowNull is true and $value is null. |
||
32 | * |
||
33 | * @test |
||
34 | * @covers ::filter |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | public function filterNullAllowed() |
||
42 | |||
43 | /** |
||
44 | * Verify behavior of filter() when $allowNull is false and $value is null. |
||
45 | * |
||
46 | * @test |
||
47 | * @covers ::filter |
||
48 | * @expectedException \TraderInteractive\Filter\Exception |
||
49 | * @expectedExceptionMessage $value not a non-empty string |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function filterNullNotAllowed() |
||
57 | |||
58 | /** |
||
59 | * Verify behavior of filter() when $value is a \DateTimeZone object. |
||
60 | * |
||
61 | * @test |
||
62 | * @covers ::filter |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function filterTimeZonePass() |
||
71 | |||
72 | /** |
||
73 | * Verify behavior of filter() when $value is not a valid timezone. |
||
74 | * |
||
75 | * @test |
||
76 | * @covers ::filter |
||
77 | * @expectedException \TraderInteractive\Filter\Exception |
||
78 | * @expectedExceptionMessage Unknown or bad timezone (INVALID) |
||
79 | */ |
||
80 | public function filterInvalidName() |
||
84 | |||
85 | /** |
||
86 | * Verify behavior of filter() $value is a string with only whitespace. |
||
87 | * |
||
88 | * @test |
||
89 | * @covers ::filter |
||
90 | * @expectedException \TraderInteractive\Filter\Exception |
||
91 | * @expectedExceptionMessage $value not a non-empty string |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function filterEmptyValue() |
||
99 | |||
100 | /** |
||
101 | * Verify behavior of filter() when $value is not a string. |
||
102 | * |
||
103 | * @test |
||
104 | * @covers ::filter |
||
105 | * @expectedException \TraderInteractive\Filter\Exception |
||
106 | * @expectedExceptionMessage $value not a non-empty string |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function filterNonStringArgument() |
||
114 | } |
||
115 |
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.