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:
Complex classes like MinMaxDaysConstraint often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MinMaxDaysConstraint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class MinMaxDaysConstraint extends Constraint { |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $min_days = 0; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $max_days = 0; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $checkin_day = NULL; |
||
34 | |||
35 | /** |
||
36 | * @param $min_days |
||
37 | * @param $max_days |
||
38 | * @param $start_date |
||
39 | * @param $end_date |
||
40 | * @param $checkin_day |
||
41 | */ |
||
42 | public function __construct($units, $min_days = 0, $max_days = 0, $start_date = NULL, $end_date = NULL, $checkin_day = NULL) { |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function applyConstraint(CalendarResponse &$calendar_response) { |
||
96 | |||
97 | /** |
||
98 | * Generates a text describing an availability_constraint. |
||
99 | * |
||
100 | * @return string |
||
101 | * The formatted message. |
||
102 | */ |
||
103 | public function toString() { |
||
173 | |||
174 | /** |
||
175 | * @param $day |
||
176 | * @return string |
||
177 | */ |
||
178 | View Code Duplication | private function getWeekDay($day) { |
|
191 | |||
192 | /** |
||
193 | * @return int |
||
194 | */ |
||
195 | public function getMinDays() { |
||
198 | |||
199 | /** |
||
200 | * @param $min_days |
||
201 | */ |
||
202 | public function setMinDays($min_days) { |
||
205 | |||
206 | /** |
||
207 | * @return int |
||
208 | */ |
||
209 | public function getMaxDays() { |
||
212 | |||
213 | /** |
||
214 | * @param $max_days |
||
215 | */ |
||
216 | public function setMaxDays($max_days) { |
||
219 | |||
220 | /** |
||
221 | * @return int |
||
222 | */ |
||
223 | public function getCheckinDay() { |
||
226 | |||
227 | /** |
||
228 | * @param $checkin_day |
||
229 | */ |
||
230 | public function setCheckinDay($checkin_day) { |
||
233 | |||
234 | } |
||
235 |
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.