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_EntryPeriod 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_EntryPeriod, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class absences_EntryPeriod extends absences_Record |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * |
||
| 44 | * @var absences_Entry |
||
| 45 | */ |
||
| 46 | private $entry; |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * @return absences_EntryPeriod |
||
| 52 | */ |
||
| 53 | public static function getById($id_elem) |
||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * (non-PHPdoc) |
||
| 66 | * @see absences_Record::getRow() |
||
| 67 | */ |
||
| 68 | 31 | public function getRow() |
|
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param absences_Entry $entry |
||
| 91 | * @return absences_EntryPeriod |
||
| 92 | */ |
||
| 93 | 36 | public function setEntry(absences_Entry $entry) |
|
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * @return absences_Entry |
||
| 102 | */ |
||
| 103 | 27 | View Code Duplication | public function getEntry() |
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Check validity before saving an element |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function checkValidity() |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Save element (insert or update or delete) |
||
| 135 | */ |
||
| 136 | public function save() |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * get a period on boundaries |
||
| 181 | * |
||
| 182 | * @param string $begin Optional limit to use for duration |
||
| 183 | * @param string $end Optional limit to use for duration |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | 29 | private function getPeriod($begin = null, $end = null) |
|
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Test if the restrictive boundaries exclude the totality of period |
||
| 207 | * |
||
| 208 | * @param string $begin Optional limit to use for duration |
||
| 209 | * @param string $end Optional limit to use for duration |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 31 | private function isOutOfBounds($begin = null, $end = null) |
|
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Get duration in days for the working period |
||
| 233 | * |
||
| 234 | * @param string $begin Optional limit to use for duration |
||
| 235 | * @param string $end Optional limit to use for duration |
||
| 236 | * |
||
| 237 | * @return float |
||
| 238 | */ |
||
| 239 | 28 | public function getDurationDays($begin = null, $end = null) |
|
| 240 | { |
||
| 241 | 28 | if ($this->isOutOfBounds($begin, $end)) { |
|
| 242 | 2 | return 0; |
|
| 243 | } |
||
| 244 | |||
| 245 | 27 | list($begin, $end) = $this->getPeriod($begin, $end); |
|
| 246 | 27 | $dtbegin = BAB_DateTime::fromIsoDateTime($begin); |
|
| 247 | 27 | $dtend = BAB_DateTime::fromIsoDateTime($end); |
|
| 248 | |||
| 249 | 27 | $hb = sprintf('%02d:%02d', $dtbegin->getHour(), $dtbegin->getMinute()); |
|
| 250 | 27 | $he = sprintf('%02d:%02d', $dtend->getHour(), $dtend->getMinute()); |
|
| 251 | |||
| 252 | 27 | if ($hb === $he) { |
|
| 253 | return 0; |
||
| 254 | } |
||
| 255 | |||
| 256 | 27 | if ($he <= '12:00') { |
|
| 257 | 20 | return $this->getMorningDays(); // 0.5 or 0 |
|
| 258 | } |
||
| 259 | |||
| 260 | 26 | if ($hb >= '12:00') { |
|
| 261 | 26 | return $this->getAfternoonDays(); // 0.5 or 0 |
|
| 262 | } |
||
| 263 | |||
| 264 | 7 | if ($hb <= '12:00' && $he >= '12:00') { |
|
| 265 | 7 | $duration = $this->getMorningDays(); |
|
| 266 | 7 | $duration += $this->getAfternoonDays(); |
|
| 267 | 7 | return $duration; |
|
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get or set the cache for half day |
||
| 273 | * @param string $period am|pm |
||
| 274 | * @return string the entry_period dates |
||
| 275 | */ |
||
| 276 | 27 | private function halfDayCache($period) |
|
| 277 | { |
||
| 278 | 27 | $date = substr($this->date_begin, 0, 10); |
|
| 279 | 27 | $entry = $this->getEntry(); |
|
| 280 | |||
| 281 | |||
| 282 | 27 | if (!isset($entry->_getDurationDays_halfDays[$date])) { |
|
| 283 | 27 | $entry->_getDurationDays_halfDays[$date] = array( |
|
| 284 | 27 | 'am' => null, |
|
| 285 | 'pm' => null |
||
| 286 | 27 | ); |
|
| 287 | 27 | } |
|
| 288 | |||
| 289 | 27 | if (isset($entry->_getDurationDays_halfDays[$date][$period])) { |
|
| 290 | 25 | return $entry->_getDurationDays_halfDays[$date][$period]; |
|
| 291 | } |
||
| 292 | |||
| 293 | 27 | $entry->_getDurationDays_halfDays[$date][$period] = $this->date_begin.'/'.$this->date_end; |
|
| 294 | |||
| 295 | 27 | return $entry->_getDurationDays_halfDays[$date][$period]; |
|
| 296 | } |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Get number of days for the morning of the day |
||
| 301 | * if more than one period exists in the morning, the half day duration will be positive only for the first period |
||
| 302 | * @return float |
||
| 303 | */ |
||
| 304 | 27 | View Code Duplication | public function getMorningDays() |
| 305 | { |
||
| 306 | 27 | $period = $this->halfDayCache('am'); |
|
| 307 | |||
| 308 | 27 | if ($this->date_begin.'/'.$this->date_end == $period) { |
|
| 309 | // this period is the first for the morning, it is accounted |
||
| 310 | 27 | return 0.5; |
|
| 311 | } |
||
| 312 | |||
| 313 | return 0; |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Get number of days for the afternoon of the day |
||
| 319 | * if more than one period exists in the morning, the half day duration will be positive only for the first period |
||
| 320 | * @return float |
||
| 321 | */ |
||
| 322 | 26 | View Code Duplication | public function getAfternoonDays() |
| 323 | { |
||
| 324 | 26 | $period = $this->halfDayCache('pm'); |
|
| 325 | |||
| 326 | 26 | if ($this->date_begin.'/'.$this->date_end == $period) { |
|
| 327 | // this period is the first for the afternoon, it is accounted |
||
| 328 | 26 | return 0.5; |
|
| 329 | } |
||
| 330 | |||
| 331 | 7 | return 0; |
|
| 332 | } |
||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Get duration in hours for the working period |
||
| 339 | * |
||
| 340 | * @param string $begin Optional limit to use for duration |
||
| 341 | * @param string $end Optional limit to use for duration |
||
| 342 | * |
||
| 343 | * @return float |
||
| 344 | */ |
||
| 345 | 27 | public function getDurationHours($begin = null, $end = null) |
|
| 357 | } |
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.