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 |
||
4 | final class Calender |
||
5 | { |
||
6 | |||
7 | private $year; |
||
8 | private $month; |
||
9 | private $day; |
||
10 | private $timeZone; |
||
11 | |||
12 | /** |
||
13 | * Calender constructor. |
||
14 | * |
||
15 | * @param string|null $year |
||
16 | * @param string|null $month |
||
17 | * @param string|null $day |
||
18 | * @param string|null $timezone |
||
19 | */ |
||
20 | private function __construct($year = null, $month = null, $day = null, $timezone = null) |
||
29 | |||
30 | /** |
||
31 | * @param string|null $year |
||
32 | * @param string|null $month |
||
33 | * @param string|null $day |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | private function validateInput($year = null, $month = null, $day = null) |
||
46 | |||
47 | /** |
||
48 | * @return void |
||
49 | */ |
||
50 | private function validateState() |
||
56 | |||
57 | /** |
||
58 | * @return void |
||
59 | */ |
||
60 | private function validateDay() |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | private function getMaxDays() |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | private function getYearOrHolder() |
||
88 | |||
89 | /** |
||
90 | * @return void |
||
91 | */ |
||
92 | private function validateMonth() |
||
98 | |||
99 | /** |
||
100 | * @param string $day should match regex pattern ----\d\d |
||
101 | * |
||
102 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
103 | */ |
||
104 | View Code Duplication | public static function fromDay($day) |
|
113 | |||
114 | /** |
||
115 | * @param string $monthDay |
||
116 | * |
||
117 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
118 | */ |
||
119 | View Code Duplication | public static function fromMonthDay($monthDay) |
|
128 | |||
129 | /** |
||
130 | * @param string $month |
||
131 | * |
||
132 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
133 | */ |
||
134 | View Code Duplication | public static function fromMonth($month) |
|
143 | |||
144 | /** |
||
145 | * @param string $yearMonth |
||
146 | * |
||
147 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
148 | */ |
||
149 | View Code Duplication | public static function fromYearMonth($yearMonth) |
|
158 | |||
159 | /** |
||
160 | * @param string $year |
||
161 | * |
||
162 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
163 | */ |
||
164 | View Code Duplication | public static function fromYear($year) |
|
173 | } |
||
174 |
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.