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 |
||
22 | class TimezoneRule extends Component |
||
23 | { |
||
24 | const TYPE_DAYLIGHT = 'DAYLIGHT'; |
||
25 | const TYPE_STANDARD = 'STANDARD'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $type; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $tzOffsetFrom; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $tzOffsetTo; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $tzName; |
||
46 | |||
47 | /** |
||
48 | * @var \DateTimeInterface |
||
49 | */ |
||
50 | protected $dtStart; |
||
51 | |||
52 | /** |
||
53 | * @var RecurrenceRule |
||
54 | */ |
||
55 | protected $recurrenceRule; |
||
56 | |||
57 | /** |
||
58 | * create new Timezone Rule object by giving a rule type identifier. |
||
59 | * |
||
60 | * @param string $ruleType one of DAYLIGHT or STANDARD |
||
61 | * |
||
62 | * @throws \InvalidArgumentException |
||
63 | */ |
||
64 | View Code Duplication | public function __construct($ruleType) |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function buildPropertyBag() |
||
103 | |||
104 | /** |
||
105 | * @param $offset |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function setTzOffsetFrom($offset) |
||
115 | |||
116 | /** |
||
117 | * @param $offset |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setTzOffsetTo($offset) |
||
127 | |||
128 | /** |
||
129 | * @param $name |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setTzName($name) |
||
139 | |||
140 | /** |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setDtStart(\DateTimeInterface $dtStart) |
||
149 | |||
150 | /** |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function getType() |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getTzOffsetFrom() |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getTzOffsetTo() |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getTzName() |
||
191 | |||
192 | /** |
||
193 | * @return RecurrenceRule |
||
194 | */ |
||
195 | public function getRecurrenceRule() |
||
199 | |||
200 | /** |
||
201 | * @return mixed return string representation of start date or NULL if no date was given |
||
202 | */ |
||
203 | public function getDtStart() |
||
211 | } |
||
212 |
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.