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 |
||
16 | class CheckInDayConstraint extends Constraint { |
||
17 | |||
18 | /** |
||
19 | * @var |
||
20 | */ |
||
21 | protected $checkin_day; |
||
22 | |||
23 | /** |
||
24 | * @param $units |
||
25 | * @param $checkin_day |
||
26 | */ |
||
27 | public function __construct($units, $checkin_day, $start_date = NULL, $end_date = NULL) { |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function applyConstraint(CalendarResponse &$calendar_response) { |
||
67 | |||
68 | /** |
||
69 | * Generates a text describing an availability_constraint. |
||
70 | * |
||
71 | * @return string |
||
72 | * The formatted message. |
||
73 | */ |
||
74 | public function toString() { |
||
75 | $text = ''; |
||
76 | $args = array(); |
||
77 | |||
78 | $start_date = FALSE; |
||
79 | $end_date = FALSE; |
||
80 | |||
81 | // Day of the week constraint variable. |
||
82 | $day_of_the_week = $this->getWeekDay($this->checkin_day); |
||
83 | |||
84 | // Date range constraint variables. |
||
85 | View Code Duplication | if ($this->start_date !== NULL && $this->start_date != (new \DateTime('1970-01-01'))) { |
|
86 | $start_date = $this->start_date->format('Y-m-d'); |
||
87 | } |
||
88 | View Code Duplication | if ($this->end_date !== NULL && $this->end_date != (new \DateTime('2999-12-31'))) { |
|
89 | $end_date = $this->end_date->format('Y-m-d'); |
||
90 | } |
||
91 | |||
92 | // Finally, build out the constraint text string adding components |
||
93 | // as necessary. |
||
94 | |||
95 | // Specify a date range constraint. |
||
96 | View Code Duplication | if ($start_date && $end_date) { |
|
97 | $text = 'From @start_date to @end_date'; |
||
98 | |||
99 | $args['@start_date'] = $start_date; |
||
100 | $args['@end_date'] = $end_date; |
||
101 | } |
||
102 | |||
103 | // Specify the day of the week constraint. |
||
104 | View Code Duplication | if ($day_of_the_week) { |
|
105 | if ($start_date && $end_date) { |
||
106 | $text = 'From @start_date to @end_date, bookings must start on @day_of_the_week'; |
||
107 | } else { |
||
108 | $text = 'Bookings must start on @day_of_the_week'; |
||
109 | } |
||
110 | |||
111 | $args['@day_of_the_week'] = $day_of_the_week; |
||
112 | } |
||
113 | |||
114 | return array('text' => $text, 'args' => $args); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param $day |
||
119 | * @return string |
||
120 | */ |
||
121 | View Code Duplication | private function getWeekDay($day) { |
|
134 | |||
135 | /** |
||
136 | * @return int |
||
137 | */ |
||
138 | public function getCheckinDay() { |
||
141 | |||
142 | /** |
||
143 | * @param $checkin_day |
||
144 | */ |
||
145 | public function setCheckinDay($checkin_day) { |
||
148 | |||
149 | } |
||
150 |
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.