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 absences_WorkperiodRecoverRequest 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 absences_WorkperiodRecoverRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class absences_WorkperiodRecoverRequest extends absences_Request |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @var absences_WorkperiodType |
||
| 53 | */ |
||
| 54 | private $type; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * @param int $id |
||
| 59 | * @return absences_WorkperiodRecoverRequest |
||
| 60 | */ |
||
| 61 | public static function getById($id) |
||
| 62 | { |
||
| 63 | $request = new absences_WorkperiodRecoverRequest(); |
||
| 64 | $request->id = $id; |
||
| 65 | |||
| 66 | return $request; |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function getRow() |
||
| 76 | { |
||
| 77 | if (null === $this->row) |
||
| 78 | { |
||
| 79 | global $babDB; |
||
| 80 | $query = 'SELECT * FROM absences_workperiod_recover_request WHERE id='.$babDB->quote($this->id); |
||
| 81 | |||
| 82 | $res = $babDB->db_query($query); |
||
| 83 | $this->setRow($babDB->db_fetch_assoc($res)); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this->row; |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * |
||
| 92 | * @param absences_WorkperiodType $type |
||
| 93 | * @return absences_WorkperiodRecoverRequest |
||
| 94 | */ |
||
| 95 | public function setType(absences_WorkperiodType $type) |
||
| 96 | { |
||
| 97 | $this->type = $type; |
||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | * @return absences_WorkperiodType |
||
| 105 | */ |
||
| 106 | public function getType() |
||
| 107 | { |
||
| 108 | if (!isset($this->type)) |
||
| 109 | { |
||
| 110 | require_once dirname(__FILE__).'/workperiod_type.class.php'; |
||
| 111 | |||
| 112 | $row = $this->getRow(); |
||
| 113 | $this->type = absences_WorkperiodType::getFromId($row['id_type']); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->type; |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * (non-PHPdoc) |
||
| 124 | * @see absences_Request::getRequestType() |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getRequestType() |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * |
||
| 137 | */ |
||
| 138 | public function save() |
||
| 139 | { |
||
| 140 | global $babDB; |
||
| 141 | require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php'; |
||
| 142 | |||
| 143 | if (!isset($this->validity_end) || '0000-00-00' === $this->validity_end) |
||
| 144 | { |
||
| 145 | $end = BAB_DateTime::fromIsoDateTime($this->date_end); |
||
| 146 | $days = absences_getVacationOption('end_recup'); |
||
| 147 | |||
| 148 | if (!$days) { |
||
| 149 | $days = 365; |
||
| 150 | } |
||
| 151 | |||
| 152 | // les annees ne foncitonnent pas avec cette API ? |
||
| 153 | |||
| 154 | if (($days / 365) >= 1) { |
||
| 155 | $years = (int)($days / 365); |
||
| 156 | $days = $days % 365; |
||
| 157 | |||
| 158 | $end->add($years, BAB_DATETIME_YEAR); |
||
| 159 | } |
||
| 160 | |||
| 161 | $end->add($days, BAB_DATETIME_DAY); |
||
| 162 | |||
| 163 | $this->validity_end = $end->getIsoDate(); |
||
| 164 | } |
||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | if (isset($this->id)) |
||
| 169 | { |
||
| 170 | $query = 'UPDATE absences_workperiod_recover_request SET |
||
| 171 | id_user='.$babDB->quote($this->id_user).', |
||
| 172 | date_begin='.$babDB->quote($this->date_begin).', |
||
| 173 | date_end='.$babDB->quote($this->date_end).', |
||
| 174 | id_type='.$babDB->quote($this->id_type).', |
||
| 175 | idfai='.$babDB->quote($this->idfai).', |
||
| 176 | comment='.$babDB->quote($this->comment).', |
||
| 177 | modifiedOn=NOW(), |
||
| 178 | status='.$babDB->quote($this->status).', |
||
| 179 | comment2='.$babDB->quote($this->comment2).', |
||
| 180 | id_approver='.$babDB->quote($this->id_approver).', |
||
| 181 | id_right='.$babDB->quote($this->id_right).', |
||
| 182 | quantity='.$babDB->quote($this->quantity).', |
||
| 183 | quantity_unit='.$babDB->quote($this->quantity_unit).', |
||
| 184 | validity_end='.$babDB->quote($this->validity_end).' |
||
| 185 | '; |
||
| 186 | |||
| 187 | if (isset($this->todelete)) { |
||
| 188 | $query .= ', todelete='.$babDB->quote($this->todelete); |
||
| 189 | } |
||
| 190 | |||
| 191 | $query .= 'WHERE id='.$babDB->quote($this->id); |
||
| 192 | |||
| 193 | |||
| 194 | $babDB->db_query($query); |
||
| 195 | |||
| 196 | |||
| 197 | } else { |
||
| 198 | |||
| 199 | $babDB->db_query('INSERT INTO absences_workperiod_recover_request ( |
||
| 200 | id_user, |
||
| 201 | date_begin, |
||
| 202 | date_end, |
||
| 203 | id_type, |
||
| 204 | comment, |
||
| 205 | createdOn, |
||
| 206 | modifiedOn, |
||
| 207 | status, |
||
| 208 | quantity, |
||
| 209 | quantity_unit, |
||
| 210 | validity_end |
||
| 211 | ) VALUES ( |
||
| 212 | |||
| 213 | '.$babDB->quote($this->id_user).', |
||
| 214 | '.$babDB->quote($this->date_begin).', |
||
| 215 | '.$babDB->quote($this->date_end).', |
||
| 216 | '.$babDB->quote($this->id_type).', |
||
| 217 | '.$babDB->quote($this->comment).', |
||
| 218 | NOW(), |
||
| 219 | NOW(), |
||
| 220 | '.$babDB->quote($this->status).', |
||
| 221 | '.$babDB->quote($this->quantity).', |
||
| 222 | '.$babDB->quote($this->quantity_unit).', |
||
| 223 | '.$babDB->quote($this->validity_end).' |
||
| 224 | ) |
||
| 225 | '); |
||
| 226 | |||
| 227 | $this->id = $babDB->db_insert_id(); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Trouver la duree en calculee dans la meme unite que le type associe |
||
| 236 | * |
||
| 237 | * duree en jour precis a la demie-journee ou duree en heuress precis a l'heure |
||
| 238 | * |
||
| 239 | * |
||
| 240 | * @return float | int |
||
| 241 | */ |
||
| 242 | public function getComputedDuration() |
||
| 243 | { |
||
| 244 | require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php'; |
||
| 245 | $unit = $this->getType()->quantity_unit; |
||
|
|
|||
| 246 | |||
| 247 | if ('H' === $unit) |
||
| 248 | { |
||
| 249 | return (int) floor((bab_mktime($this->date_end) - bab_mktime($this->date_begin)) / 3600); |
||
| 250 | } |
||
| 251 | |||
| 252 | // trouver les jours complets |
||
| 253 | |||
| 254 | list($day1, $hour1) = explode(' ', $this->date_begin); |
||
| 255 | $full1 = BAB_DateTime::fromIsoDateTime($day1.' 00:00:00'); |
||
| 256 | $full1->add(1, BAB_DATETIME_DAY); |
||
| 257 | |||
| 258 | list($day2, $hour2) = explode(' ', $this->date_end); |
||
| 259 | $full2 = BAB_DateTime::fromIsoDateTime($day2.' 00:00:00'); |
||
| 260 | |||
| 261 | $date_begin = BAB_DateTime::fromIsoDateTime($this->date_begin); |
||
| 262 | $date_end = BAB_DateTime::fromIsoDateTime($this->date_end); |
||
| 263 | |||
| 264 | if ($full1->getIsoDate() > $full2->getIsoDate()) |
||
| 265 | { |
||
| 266 | $full_days = 0; |
||
| 267 | |||
| 268 | $diff_hour = round(($date_end->getTimeStamp() - $date_begin->getTimeStamp()) / 3600); |
||
| 269 | $diff_days = $diff_hour > 4 ? 1 : .5; |
||
| 270 | |||
| 271 | return $diff_days; |
||
| 272 | |||
| 273 | } else { |
||
| 274 | $full_days = $full2->dateDiffIso($full1->getIsoDate(), $full2->getIsoDate()); |
||
| 275 | |||
| 276 | bab_debug('Full days : '.$full_days); |
||
| 277 | |||
| 278 | $diff_hour1 = round(($full1->getTimeStamp() - $date_begin->getTimeStamp()) / 3600); |
||
| 279 | $diff_hour2 = round(($date_end->getTimeStamp() - $full2->getTimeStamp()) / 3600); |
||
| 280 | |||
| 281 | bab_debug('before first complete day : '.$diff_hour1.' h'); |
||
| 282 | bab_debug('after last complete day : '.$diff_hour2.' h'); |
||
| 283 | |||
| 284 | |||
| 285 | $diff_days1 = $diff_hour1 > 12 ? 1 : .5; |
||
| 286 | $diff_days2 = $diff_hour2 > 12 ? 1 : .5; |
||
| 287 | |||
| 288 | return ($diff_days1 + $full_days + $diff_days2); |
||
| 289 | } |
||
| 290 | |||
| 291 | } |
||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * (non-PHPdoc) |
||
| 299 | * @see absences_Request::getApprobationId() |
||
| 300 | * |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | public function getApprobationId() |
||
| 304 | { |
||
| 305 | if ($agent = $this->getAgent()) |
||
| 306 | { |
||
| 307 | return $agent->getRecoverApprobationId(); |
||
| 308 | } |
||
| 309 | |||
| 310 | return null; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Process specific code when the request is rejected |
||
| 315 | * |
||
| 316 | */ |
||
| 317 | protected function onReject() |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * programme de reprise des declaration de jours travaille approuvees par auto-approbation sans droit cree |
||
| 325 | */ |
||
| 326 | public function restoreMissingRight() |
||
| 327 | { |
||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | public function getRight() |
||
| 337 | { |
||
| 338 | if (!$this->id_right) { |
||
| 339 | return null; |
||
| 340 | } |
||
| 341 | |||
| 342 | require_once dirname(__FILE__).'/right.class.php'; |
||
| 343 | |||
| 344 | $right = new absences_Right($this->id_right); |
||
| 345 | |||
| 346 | if (!$right->getRow()) { |
||
| 347 | return null; |
||
| 348 | } |
||
| 349 | |||
| 350 | |||
| 351 | return $right; |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Process specific code when the request is confirmed |
||
| 358 | * |
||
| 359 | */ |
||
| 360 | protected function onConfirm() |
||
| 361 | { |
||
| 362 | if (null !== $this->getRight()) { |
||
| 363 | return; |
||
| 364 | } |
||
| 365 | |||
| 366 | // create right access for recovery only if not allready created |
||
| 367 | |||
| 368 | global $babDB; |
||
| 369 | require_once dirname(__FILE__).'/right.class.php'; |
||
| 370 | |||
| 371 | $agent = $this->getAgent(); |
||
| 372 | |||
| 373 | $begin = mb_substr($this->date_end, 0, 10); |
||
| 374 | $end = $this->validity_end; |
||
| 375 | |||
| 376 | $date = bab_shortDate(bab_mktime($this->date_begin), false); |
||
| 377 | |||
| 378 | $description = $agent->getName().' '.$date; |
||
| 379 | |||
| 380 | // si un droit avec cette description existe deja on ne le cree pas |
||
| 381 | // mais on fait la liaison avec la periode travaillee |
||
| 382 | |||
| 383 | $res = $babDB->db_query('SELECT id FROM absences_rights WHERE description='.$babDB->quote($description)); |
||
| 384 | $existingRight = $babDB->db_fetch_assoc($res); |
||
| 385 | |||
| 386 | if ($existingRight) { |
||
| 387 | $id_right = $existingRight['id']; |
||
| 388 | } else { |
||
| 389 | $id_right = $agent->createRecoveryRight($begin, $end, $description, $this->quantity, $this->quantity_unit); |
||
| 390 | } |
||
| 391 | |||
| 392 | $babDB->db_query('UPDATE absences_workperiod_recover_request |
||
| 393 | SET id_right='.$babDB->quote($id_right).' |
||
| 394 | WHERE id='.$babDB->quote($this->id) |
||
| 395 | ); |
||
| 396 | } |
||
| 397 | |||
| 398 | public function getTitle() |
||
| 399 | { |
||
| 400 | if (isset($this->todelete) && $this->todelete) { |
||
| 401 | return absences_translate('worked period entitling recovery to delete'); |
||
| 402 | } |
||
| 403 | |||
| 404 | return absences_translate('worked period entitling recovery'); |
||
| 405 | } |
||
| 406 | |||
| 407 | |||
| 408 | View Code Duplication | public function getNotifyFields() |
|
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | View Code Duplication | public function getYear() |
|
| 430 | |||
| 431 | |||
| 432 | View Code Duplication | public function getArchiveYear() |
|
| 452 | |||
| 453 | |||
| 454 | View Code Duplication | public function archive() |
|
| 466 | |||
| 467 | |||
| 468 | |||
| 469 | View Code Duplication | public function setNotified() |
|
| 481 | |||
| 482 | |||
| 483 | public function getManagerEditUrl() |
||
| 488 | |||
| 489 | public function getManagerDeleteUrl() |
||
| 494 | |||
| 495 | |||
| 496 | |||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | View Code Duplication | public function getEditUrl($rfrom, $ide = null) |
|
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | public function getManagerFrame() |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Get request card frame to display in requests list |
||
| 530 | * @param bool $display_username Affiche le nom du demandeur dans la card frame ou non, utile pour les listes contenant plusieurs demandeurs possibles |
||
| 531 | * @param int $rfrom si les demandes de la liste sont modifiee par un gestionnaire ou gestionnaire delegue |
||
| 532 | * @param int $ide id entite de l'organigramme >0 si gestionnaire delegue seulement |
||
| 533 | * @return Widget_Frame |
||
| 534 | */ |
||
| 535 | public function getCardFrame($display_username, $rfrom, $ide) |
||
| 585 | |||
| 586 | |||
| 587 | |||
| 588 | /** |
||
| 589 | * Delete request |
||
| 590 | */ |
||
| 591 | public function delete() |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Test form validity |
||
| 615 | * |
||
| 616 | * @throws Exception |
||
| 617 | * |
||
| 618 | * @return bool |
||
| 619 | */ |
||
| 620 | public static function checkForm(Array $workperiod, absences_WorkperiodRecoverRequest $wd = null) |
||
| 725 | } |
||
| 726 | |||
| 727 | |||
| 728 | |||
| 729 | |||
| 902 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.