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 MeetingModel 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 MeetingModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MeetingModel extends BaseModel |
||
|
|
|||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $weekendDays = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $form_names = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $dbColumns = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var DateTime |
||
| 37 | */ |
||
| 38 | public $regOpening = NULL; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var DateTime |
||
| 42 | */ |
||
| 43 | public $regClosing = NULL; |
||
| 44 | |||
| 45 | /** @var string registration heading text */ |
||
| 46 | public $regHeading = ''; |
||
| 47 | |||
| 48 | public $eventId; |
||
| 49 | public $courseId; |
||
| 50 | |||
| 51 | private $configuration; |
||
| 52 | |||
| 53 | private $program; |
||
| 54 | private $httpEncoding; |
||
| 55 | private $dbTable; |
||
| 56 | |||
| 57 | protected $table = 'kk_meetings'; |
||
| 58 | |||
| 59 | /** Constructor */ |
||
| 60 | public function __construct(Context $database, ProgramModel $program) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $encoding |
||
| 100 | */ |
||
| 101 | public function setHttpEncoding($encoding) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Create new or return existing instance of class |
||
| 108 | * |
||
| 109 | * @return mixed instance of class |
||
| 110 | */ |
||
| 111 | public static function getInstance() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return Nette\Database\Table\IRow |
||
| 121 | */ |
||
| 122 | public function all() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $id |
||
| 132 | * @return Nette\Database\Table\IRow |
||
| 133 | */ |
||
| 134 | public function find($id) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Create a new record |
||
| 144 | * |
||
| 145 | * @param mixed array of data |
||
| 146 | * @return boolean |
||
| 147 | */ |
||
| 148 | public function create(array $data) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Modify record |
||
| 158 | * |
||
| 159 | * @param int $id ID of record |
||
| 160 | * @param array $db_data array of data |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function update($id, array $data) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Delete one or multiple record/s |
||
| 172 | * |
||
| 173 | * @param int ID/s of record |
||
| 174 | * @return boolean |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function delete($ids) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Return meeting data |
||
| 186 | * |
||
| 187 | * @return Nette\Database\Table\IRow |
||
| 188 | */ |
||
| 189 | public function getData($meetingId = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $priceType cost|advance |
||
| 206 | * @return integer |
||
| 207 | */ |
||
| 208 | public function getPrice($priceType) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Render HTML Provinces <select> |
||
| 220 | * |
||
| 221 | * @param int ID of selected province |
||
| 222 | * @return string html <select> |
||
| 223 | */ |
||
| 224 | public function renderHtmlProvinceSelect($selectedProvince) |
||
| 244 | |||
| 245 | /** Public program same as getPrograms*/ |
||
| 246 | public function getPublicPrograms($blockId) |
||
| 274 | |||
| 275 | public function renderPublicProgramOverview($meetingId) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param integer $meetingId |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setRegistrationHandlers($meetingId = 1) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function getRegOpening() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $value |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function setRegOpening($value = '') |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getRegClosing() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $value |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function setRegClosing($value = '') |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function getRegHeading() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $value |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | public function setRegHeading($value = '') |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Is registration open? |
||
| 413 | * |
||
| 414 | * @return boolean |
||
| 415 | */ |
||
| 416 | public function isRegOpen($debug = false) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param integer $id |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function getProvinceNameById($id) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return Row |
||
| 437 | */ |
||
| 438 | public function findEventId() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return Row |
||
| 449 | */ |
||
| 450 | public function findCourseId() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param integer|string $meetingId |
||
| 461 | * @return ActiveRow |
||
| 462 | */ |
||
| 463 | public function getPlaceAndYear($meetingId) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return ActiveRow |
||
| 476 | */ |
||
| 477 | public function getMenuItems() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return integer |
||
| 491 | */ |
||
| 492 | public function getLastMeetingId() |
||
| 501 | |||
| 502 | } |
||
| 503 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.