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 Object 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 Object, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Object extends Entity implements IObject { |
||
|
|
|||
| 38 | |||
| 39 | /** |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | public $id; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ICalendar |
||
| 47 | */ |
||
| 48 | public $calendar; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $uri; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $etag; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * @var integer |
||
| 65 | */ |
||
| 66 | public $ruds; |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * @var VCalendar |
||
| 71 | */ |
||
| 72 | public $vObject; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $objectName; |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * take data from vobject and put into this Object object |
||
| 83 | * @param VCalendar $vcalendar |
||
| 84 | * @throws CorruptDataException |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public static function fromVObject(VCalendar $vcalendar) { |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * @param ICalendar $calendar |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function setCalendar(ICalendar $calendar) { |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * @return ICalendar |
||
| 105 | */ |
||
| 106 | public function getCalendar() { |
||
| 109 | |||
| 110 | public function getCalendarid() { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $uri |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function setUri($uri) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getUri() { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $etag |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setEtag($etag) { |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * @param bool $force generate etag if none stored |
||
| 143 | * @return mixed (string|null) |
||
| 144 | */ |
||
| 145 | public function getEtag($force=false) { |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @param integer $ruds |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setRuds($ruds) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * @param boolean $force return value all the time |
||
| 169 | * @return mixed (integer|null) |
||
| 170 | */ |
||
| 171 | public function getRuds($force=false) { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @param VCalendar $vobject |
||
| 189 | * @param boolean $autoAssignUri |
||
| 190 | * @throws CorruptDataException |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function setVObject(VCalendar $vobject, $autoAssignUri=false) { |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * @return VCalendar |
||
| 222 | */ |
||
| 223 | public function getVObject() { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @return array array of updated fields for update query |
||
| 256 | */ |
||
| 257 | public function getUpdatedFields() { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * set lastModified to now and update ETag |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function touch() { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * does an object allow |
||
| 293 | * @param integer $cruds |
||
| 294 | * @return integer |
||
| 295 | */ |
||
| 296 | public function doesAllow($cruds) { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * get text/calendar representation of stored object |
||
| 303 | * @return integer |
||
| 304 | */ |
||
| 305 | public function getCalendarData() { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * set the calendarData |
||
| 312 | * @param string $data CalendarData |
||
| 313 | * @throws CorruptDataException |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setCalendarData($data) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * update Etag |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function generateEtag() { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * get type of stored object |
||
| 344 | * @return integer |
||
| 345 | */ |
||
| 346 | public function getType() { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * get startDate |
||
| 355 | * @return DateTime |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function getStartDate() { |
|
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * get endDate |
||
| 371 | * @return DateTime |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function getEndDate() { |
|
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * get whether or not object is repeating |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | public function getRepeating() { |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * get summary of object |
||
| 400 | * @return mixed (string|null) |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function getSummary() { |
|
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * get last modified of object |
||
| 415 | * @return mixed (DateTime|null) |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function getLastModified() { |
|
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param DateTime $start |
||
| 431 | * @param DateTime $end |
||
| 432 | * @return boolean |
||
| 433 | */ |
||
| 434 | public function isInTimeRange(DateTime $start, DateTime $end) { |
||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * get name of property inside $this->vobject |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | private function getObjectName() { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * register field types |
||
| 451 | */ |
||
| 452 | protected function registerTypes() { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * register mandatory fields |
||
| 466 | */ |
||
| 467 | protected function registerMandatory() { |
||
| 472 | |||
| 473 | |||
| 474 | /** |
||
| 475 | * check if object is valid |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public function isValid() { |
||
| 493 | } |