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 |
||
26 | class absences_PeriodFrame |
||
27 | { |
||
28 | public $isfixed = false; |
||
29 | |||
30 | public $altbg = true; |
||
31 | |||
32 | public $datebegin; |
||
33 | public $datebegintxt; |
||
34 | |||
35 | public $dateend; |
||
36 | public $dateendtxt; |
||
37 | |||
38 | public $vactype; |
||
39 | public $addvac; |
||
40 | public $save_previsional; |
||
41 | public $remark; |
||
42 | public $nbdaystxt; |
||
43 | public $invaliddate; |
||
44 | public $invaliddate2; |
||
45 | public $invalidentry; |
||
46 | public $invalidentry1; |
||
47 | public $invalidentry3; |
||
48 | public $totaltxt; |
||
49 | public $balancetxt; |
||
50 | public $calendar; |
||
51 | public $t_total; |
||
52 | public $t_available; |
||
53 | public $t_waiting; |
||
54 | public $t_period_nbdays; |
||
55 | public $t_view_rights; |
||
56 | public $t_recurring; |
||
57 | |||
58 | public $totalval; |
||
59 | public $maxallowed; |
||
60 | public $id; |
||
61 | public $id_user; |
||
62 | public $ide; |
||
63 | |||
64 | public $year; |
||
65 | public $month; |
||
66 | public $rfrom; |
||
67 | |||
68 | |||
69 | |||
70 | public function __construct($id_user, $id) |
||
71 | { |
||
72 | require_once dirname(__FILE__).'/entry.class.php'; |
||
73 | global $babBody, $babDB; |
||
74 | $this->datebegin = $GLOBALS['babUrlScript']."?tg=month&callback=dateBegin&ymin=0&ymax=2"; |
||
75 | $this->datebegintxt = absences_translate("Begin date"); |
||
76 | $this->dateend = $GLOBALS['babUrlScript']."?tg=month&callback=dateEnd&ymin=0&ymax=2"; |
||
77 | $this->dateendtxt = absences_translate("End date"); |
||
78 | $this->vactype = absences_translate("Vacation type"); |
||
79 | View Code Duplication | if ($id) |
|
|
|||
80 | { |
||
81 | $this->addvac = absences_translate("Edit vacation request"); |
||
82 | $this->save_previsional = absences_translate("Save previsional request"); |
||
83 | } else { |
||
84 | $this->addvac = absences_translate("Request vacation"); |
||
85 | $this->save_previsional = absences_translate("Create previsional request"); |
||
86 | } |
||
87 | $this->remark = absences_translate("Remarks"); |
||
88 | $this->nbdaystxt = absences_translate("Quantity"); |
||
89 | $this->invaliddate = absences_translate("ERROR: End date must be older"); |
||
90 | $this->invaliddate = str_replace("'", "\'", $this->invaliddate); |
||
91 | $this->invaliddate = str_replace('"', "'+String.fromCharCode(34)+'",$this->invaliddate); |
||
92 | $this->invaliddate2 = absences_translate("Total days does'nt fit between dates"); |
||
93 | $this->invaliddate2 = str_replace("'", "\'", $this->invaliddate2); |
||
94 | $this->invaliddate2 = str_replace('"', "'+String.fromCharCode(34)+'",$this->invaliddate2); |
||
95 | $this->invalidentry = absences_translate("Invalid entry! Only numbers are accepted or . !"); |
||
96 | $this->invalidentry = str_replace("'", "\'", $this->invalidentry); |
||
97 | $this->invalidentry = str_replace('"', "'+String.fromCharCode(34)+'",$this->invalidentry); |
||
98 | $this->invalidentry1 = absences_translate("Invalid entry"); |
||
99 | $this->invalidentry3 = absences_translate("The number of days exceed the total allowed"); |
||
100 | $this->totaltxt = absences_translate("Total"); |
||
101 | $this->balancetxt = absences_translate("Balance"); |
||
102 | $this->calendar = absences_translate("Planning"); |
||
103 | $this->t_total = absences_translate("Total"); |
||
104 | $this->t_available = absences_translate("Available"); |
||
105 | $this->t_waiting = absences_translate("Waiting"); |
||
106 | $this->t_period_nbdays = absences_translate("Period days"); |
||
107 | $this->t_view_rights = absences_translate("View rights details"); |
||
108 | $this->t_recurring = absences_translate("Do a recuring request"); |
||
109 | $this->t_previsional = absences_translate("Previsional"); |
||
110 | $this->t_previsional_available = absences_translate("Prev. bal."); |
||
111 | |||
112 | $this->totalval = 0; |
||
113 | $this->maxallowed = 0; |
||
114 | $this->id = $id; |
||
115 | $this->id_user = $id_user; |
||
116 | $this->ide = bab_rp('ide'); |
||
117 | |||
118 | $this->year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y'); |
||
119 | $this->month = isset($_REQUEST['month']) ? $_REQUEST['month'] : date('n'); |
||
120 | $this->rfrom = isset($_REQUEST['rfrom']) ? $_REQUEST['rfrom'] : 0; |
||
121 | |||
122 | if (!empty($this->id) && !isset($_POST['daybegin'])) |
||
123 | { |
||
124 | include_once $GLOBALS['babInstallPath']."utilit/dateTime.php"; |
||
125 | |||
126 | $entry = absences_Entry::getById($this->id); |
||
127 | $arr = $entry->getRow(); |
||
128 | |||
129 | $date_begin = BAB_DateTime::fromIsoDateTime($arr['date_begin']); |
||
130 | $date_end = BAB_DateTime::fromIsoDateTime($arr['date_end']); |
||
131 | |||
132 | $this->yearbegin = $date_begin->getYear(); |
||
133 | $this->monthbegin = $date_begin->getMonth(); |
||
134 | $this->daybegin = $date_begin->getDayOfMonth(); |
||
135 | |||
136 | $this->yearend = $date_end->getYear(); |
||
137 | $this->monthend = $date_end->getMonth(); |
||
138 | $this->dayend = $date_end->getDayOfMonth(); |
||
139 | |||
140 | $this->hourbegin = date('H:i:s', $date_begin->getTimeStamp()); |
||
141 | $this->halfdaybegin = 'am' === date('a', $date_begin->getTimeStamp()) ? 0 : 1; |
||
142 | $this->hourend = date('H:i:s', $date_end->getTimeStamp()); |
||
143 | $this->halfdayend = 'am' === date('a', $date_end->getTimeStamp()) ? 0 : 1; |
||
144 | } |
||
145 | elseif (isset($_POST['daybegin'])) |
||
146 | { |
||
147 | $this->daybegin = $_POST['daybegin']; |
||
148 | $this->dayend = $_POST['dayend']; |
||
149 | $this->monthbegin = $_POST['monthbegin']; |
||
150 | $this->monthend = $_POST['monthend']; |
||
151 | $this->yearbegin = $this->year + $_POST['yearbegin'] - 1; |
||
152 | $this->yearend = $this->year + $_POST['yearend'] - 1; |
||
153 | $this->halfdaybegin = 0; |
||
154 | $this->halfdayend = 1; |
||
155 | $this->hourbegin = $_POST['hourbegin']; |
||
156 | $this->hourend = $_POST['hourend']; |
||
157 | } |
||
158 | else |
||
159 | { |
||
160 | $this->daybegin = date("j"); |
||
161 | $this->dayend = date("j"); |
||
162 | $this->monthbegin = date("n"); |
||
163 | $this->monthend = date("n"); |
||
164 | $this->yearbegin = $this->year; |
||
165 | $this->yearend = $this->year; |
||
166 | $this->halfdaybegin = 0; |
||
167 | $this->halfdayend = 1; |
||
168 | $this->hourbegin = '00:00:00'; |
||
169 | $this->hourend = '23:59:59'; |
||
170 | } |
||
171 | |||
172 | $this->halfdaysel = $this->halfdaybegin; |
||
173 | |||
174 | $this->calurl = absences_addon()->getUrl()."planning&idx=cal&idu=".$id_user; |
||
175 | |||
176 | $this->rights = absences_getRightsByGroupOnPeriod($id_user, $this->rfrom); |
||
177 | $this->total_available = array('D' => 0, 'H' => 0); |
||
178 | $this->total_waiting = array('D' => 0, 'H' => 0); |
||
179 | $this->total_previsional = array('D' => 0, 'H' => 0); |
||
180 | $this->total_prevavail = array('D' => 0, 'H' => 0); |
||
181 | |||
182 | $this->dayType = array( |
||
183 | absences_translate("Morning"), |
||
184 | absences_translate("Afternoon") |
||
185 | ); |
||
186 | |||
187 | $this->hours = absences_hoursList($GLOBALS['BAB_SESS_USERID']); |
||
188 | |||
189 | |||
190 | require_once $GLOBALS['babInstallPath'].'utilit/workinghoursincl.php'; |
||
191 | $jSON = array(); |
||
192 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 0), $jSON); |
||
193 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 1), $jSON); |
||
194 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 2), $jSON); |
||
195 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 3), $jSON); |
||
196 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 4), $jSON); |
||
197 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 5), $jSON); |
||
198 | $this->addWorkingPeriod(bab_getWHours($GLOBALS['BAB_SESS_USERID'], 6), $jSON); |
||
199 | |||
200 | |||
201 | $this->json_working_hours = '['.implode(',', $jSON).']'; |
||
202 | |||
203 | } |
||
204 | |||
205 | /** |
||
206 | * reduce working periods for planning |
||
207 | */ |
||
208 | private function addWorkingPeriod($whours, &$jSON) |
||
239 | |||
240 | |||
241 | function getnextday() |
||
258 | |||
259 | function getnextmonth() |
||
305 | |||
306 | function getnextright() |
||
307 | { |
||
308 | if ($this->right = & current($this->rights)) |
||
309 | { |
||
310 | next($this->rights); |
||
311 | $this->altbg = !$this->altbg; |
||
312 | $this->right['quantity_available'] = $this->right['quantity_available'] - $this->right['waiting']; |
||
313 | $this->right['previsional_available'] = $this->right['quantity_available'] - $this->right['previsional']; |
||
314 | |||
315 | $this->total_available[$this->right['quantity_unit']] += $this->right['quantity_available']; |
||
316 | $this->total_waiting[$this->right['quantity_unit']] += $this->right['waiting']; |
||
317 | $this->total_previsional[$this->right['quantity_unit']] += $this->right['previsional']; |
||
318 | $this->total_prevavail[$this->right['quantity_unit']] += $this->right['previsional_available']; |
||
319 | |||
320 | $this->right['quantity_available'] = bab_toHtml(absences_quantity($this->right['quantity_available'], $this->right['quantity_unit'])); |
||
321 | $this->right['previsional_available'] = bab_toHtml(absences_quantity($this->right['previsional_available'], $this->right['quantity_unit'])); |
||
322 | $this->right['waiting'] = bab_toHtml(absences_quantity($this->right['waiting'], $this->right['quantity_unit'])); |
||
323 | $this->right['previsional'] = bab_toHtml(absences_quantity($this->right['previsional'], $this->right['quantity_unit'])); |
||
324 | |||
325 | if (isset($this->right['type'])) |
||
326 | { |
||
327 | $this->color = bab_toHtml($this->right['type']->color); |
||
328 | $this->type = bab_toHtml($this->right['type']->name); |
||
329 | } else { |
||
330 | $this->color = null; |
||
331 | $this->type = null; |
||
332 | } |
||
333 | |||
334 | return true; |
||
335 | } |
||
336 | else |
||
337 | return false; |
||
338 | |||
339 | } |
||
340 | |||
341 | private function getDisplay($arr) |
||
352 | |||
353 | public function gettotal() |
||
354 | { |
||
370 | |||
371 | } |
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.