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 | /** @var array days of weekend */ |
||
| 21 | private $weekendDays = array(); |
||
| 22 | |||
| 23 | /** @var array of form names */ |
||
| 24 | public $form_names = array(); |
||
| 25 | |||
| 26 | /** @var array of database programs table columns */ |
||
| 27 | public $dbColumns = array(); |
||
| 28 | |||
| 29 | /** @var datetime at what registration opens */ |
||
| 30 | public $regOpening = NULL; |
||
| 31 | |||
| 32 | /** @var datetime at what registration ends*/ |
||
| 33 | public $regClosing = NULL; |
||
| 34 | |||
| 35 | /** @var string registration heading text */ |
||
| 36 | public $regHeading = ''; |
||
| 37 | |||
| 38 | private $program; |
||
| 39 | private $httpEncoding; |
||
| 40 | private $dbTable; |
||
| 41 | |||
| 42 | protected $table = 'kk_meetings'; |
||
| 43 | |||
| 44 | /** Constructor */ |
||
| 45 | public function __construct(Context $database, ProgramModel $program) |
||
| 46 | { |
||
| 47 | $this->weekendDays = array("pátek", "sobota", "neděle"); |
||
| 48 | $this->form_names = array( |
||
| 49 | "place", |
||
| 50 | "start_date", |
||
| 51 | "end_date", |
||
| 52 | "open_reg", |
||
| 53 | "close_reg", |
||
| 54 | "contact", |
||
| 55 | "email", |
||
| 56 | "gsm", |
||
| 57 | "cost", |
||
| 58 | "advance", |
||
| 59 | "numbering" |
||
| 60 | ); |
||
| 61 | $this->dbColumns = array( |
||
| 62 | "place", |
||
| 63 | "start_date", |
||
| 64 | "end_date", |
||
| 65 | "open_reg", |
||
| 66 | "close_reg", |
||
| 67 | "contact", |
||
| 68 | "email", |
||
| 69 | "gsm", |
||
| 70 | "cost", |
||
| 71 | "advance", |
||
| 72 | "numbering" |
||
| 73 | ); |
||
| 74 | $this->dbTable = "kk_meetings"; |
||
| 75 | $this->setDatabase($database); |
||
| 76 | $this->program = $program; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function setHttpEncoding($encoding) |
||
| 80 | { |
||
| 81 | $this->httpEncoding = $encoding; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Create new or return existing instance of class |
||
| 86 | * |
||
| 87 | * @return mixed instance of class |
||
| 88 | */ |
||
| 89 | public static function getInstance() |
||
| 90 | { |
||
| 91 | if(self::$instance === false) { |
||
| 92 | self::$instance = new self(); |
||
| 93 | } |
||
| 94 | return self::$instance; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Create a new record |
||
| 99 | * |
||
| 100 | * @param mixed array of data |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | public function create(array $data) |
||
| 104 | { |
||
| 105 | $data['guid'] = md5(uniqid()); |
||
| 106 | $result = $this->getDatabase()->query('INSERT INTO ' . $this->getTable(), $data); |
||
| 107 | |||
| 108 | return $result; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Modify record |
||
| 113 | * |
||
| 114 | * @param int $id ID of record |
||
| 115 | * @param array $db_data array of data |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function update($id, array $data) |
||
| 119 | { |
||
| 120 | $result = $this->getDatabase()->table($this->getTable())->where('id', $id)->update($data); |
||
| 121 | |||
| 122 | return $result; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Delete one or multiple record/s |
||
| 127 | * |
||
| 128 | * @param int ID/s of record |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function delete($ids) |
|
| 132 | { |
||
| 133 | $data = array('deleted' => '1'); |
||
| 134 | $result = $this->getDatabase()->table($this->getTable())->where('id', $ids)->update($data); |
||
| 135 | |||
| 136 | return $result; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return meeting data |
||
| 141 | * |
||
| 142 | * @return string html table |
||
| 143 | */ |
||
| 144 | public function getData($meetingId = null) |
||
| 145 | { |
||
| 146 | if(isset($meetingId)) { |
||
| 147 | $data = $this->getDatabase() |
||
| 148 | ->table($this->getTable()) |
||
| 149 | ->where('deleted ? AND id ?', '0', $meetingId) |
||
| 150 | ->fetch(); |
||
| 151 | } else { |
||
| 152 | $data = $this->getDatabase() |
||
| 153 | ->table($this->getTable()) |
||
| 154 | ->where('deleted', '0') |
||
| 155 | ->fetchAll(); |
||
| 156 | } |
||
| 157 | |||
| 158 | if(!$data) { |
||
| 159 | return 0; |
||
| 160 | } else { |
||
| 161 | return $data; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $priceType cost|advance |
||
| 167 | * @return integer |
||
| 168 | */ |
||
| 169 | public function getPrice($priceType) |
||
| 170 | { |
||
| 171 | return $this->getDatabase() |
||
| 172 | ->table($this->getTable()) |
||
| 173 | ->select($priceType) |
||
| 174 | ->where('id', $this->getMeetingId()) |
||
| 175 | ->limit(1) |
||
| 176 | ->fetchField(); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Render HTML Provinces <select> |
||
| 181 | * |
||
| 182 | * @param int ID of selected province |
||
| 183 | * @return string html <select> |
||
| 184 | */ |
||
| 185 | public function renderHtmlProvinceSelect($selectedProvince) |
||
| 186 | { |
||
| 187 | $html_select = "<select style='width: 195px; font-size: 10px' name='province'>\n"; |
||
| 188 | |||
| 189 | $result = $this->getDatabase() |
||
| 190 | ->table('kk_provinces') |
||
| 191 | ->fetchAll(); |
||
| 192 | |||
| 193 | foreach($result as $data) { |
||
| 194 | if($data['id'] == $selectedProvince) { |
||
| 195 | $sel = "selected"; |
||
| 196 | } |
||
| 197 | else $sel = ""; |
||
| 198 | $html_select .= "<option value='" . $data['id'] . "' " . $sel . ">" . $data['province_name'] . "</option>"; |
||
| 199 | } |
||
| 200 | |||
| 201 | $html_select .= "</select>\n"; |
||
| 202 | |||
| 203 | return $html_select; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** Public program same as getPrograms*/ |
||
| 207 | public function getPublicPrograms($blockId) |
||
| 208 | { |
||
| 209 | $result = $this->getDatabase() |
||
| 210 | ->query('SELECT progs.id AS id, |
||
| 211 | progs.name AS name, |
||
| 212 | style |
||
| 213 | FROM kk_programs AS progs |
||
| 214 | LEFT JOIN kk_categories AS cat ON cat.id = progs.category |
||
| 215 | WHERE block = ? AND progs.deleted = ? |
||
| 216 | LIMIT 10', |
||
| 217 | $blockId, '0') |
||
| 218 | ->fetchAll(); |
||
| 219 | |||
| 220 | if(!$result) { |
||
| 221 | $html = ''; |
||
| 222 | } else { |
||
| 223 | $html = "<table>\n"; |
||
| 224 | $html .= " <tr>\n"; |
||
| 225 | foreach($result as $data) { |
||
| 226 | $html .= "<td class='category cat-".$data['style']."' style='text-align:center;'>\n"; |
||
| 227 | $html .= "<a class='programLink' rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'program', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n"; |
||
| 228 | $html .= "</td>\n"; |
||
| 229 | } |
||
| 230 | $html .= " </tr>\n"; |
||
| 231 | $html .= "</table>\n"; |
||
| 232 | } |
||
| 233 | return $html; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function renderPublicProgramOverview($meetingId) |
||
| 237 | { |
||
| 238 | $days = array("pátek", "sobota", "neděle"); |
||
| 239 | $html = ""; |
||
| 240 | |||
| 241 | foreach($days as $dayKey => $dayVal) { |
||
| 242 | $html .= "<table>\n"; |
||
| 243 | $html .= " <tr>\n"; |
||
| 244 | $html .= " <td class='day' colspan='2' >" . $dayVal . "</td>\n"; |
||
| 245 | $html .= " </tr>\n"; |
||
| 246 | |||
| 247 | $result = $this->getDatabase() |
||
| 248 | ->query('SELECT blocks.id AS id, |
||
| 249 | day, |
||
| 250 | DATE_FORMAT(`from`, "%H:%i") AS `from`, |
||
| 251 | DATE_FORMAT(`to`, "%H:%i") AS `to`, |
||
| 252 | blocks.name AS name, |
||
| 253 | program, |
||
| 254 | display_progs, |
||
| 255 | style |
||
| 256 | FROM kk_blocks AS blocks |
||
| 257 | LEFT JOIN kk_categories AS cat ON cat.id = blocks.category |
||
| 258 | WHERE blocks.deleted = ? AND day = ? AND meeting = ? |
||
| 259 | ORDER BY `from` ASC', |
||
| 260 | '0', $dayVal, $meetingId) |
||
| 261 | ->fetchAll(); |
||
| 262 | |||
| 263 | if(!$result) { |
||
| 264 | $html .= "<td class='emptyTable' style='width:400px;'>Nejsou žádná aktuální data.</td>\n"; |
||
| 265 | } else { |
||
| 266 | foreach($result as $data) { |
||
| 267 | $html .= "<tr>\n"; |
||
| 268 | $html .= "<td class='time'>" . $data['from'] . " - " . $data['to'] . "</td>\n"; |
||
| 269 | if(($data['program'] == 1) && ($data['display_progs'] == 1)) { |
||
| 270 | $html .= "<td class='category cat-" . $data['style'] . "' class='daytime'>\n"; |
||
| 271 | $html .= "<div>\n"; |
||
| 272 | $html .= "<a class='programLink rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'block', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n"; |
||
| 273 | $html .= "</div>\n"; |
||
| 274 | $html .= $this->getPublicPrograms($data['id']); |
||
| 275 | $html .= "</td>\n"; |
||
| 276 | } else { |
||
| 277 | $html .= "<td class='category cat-" . $data['style'] . "'>"; |
||
| 278 | $html .= "<a class='programLink rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'block', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n"; |
||
| 279 | $html .= "</td>\n"; |
||
| 280 | } |
||
| 281 | $html .= "</tr>\n"; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | $html .= "</table>\n"; |
||
| 285 | } |
||
| 286 | |||
| 287 | return $html; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @deprecated |
||
| 292 | * |
||
| 293 | * Render data in a table |
||
| 294 | * |
||
| 295 | * @return string html of a table |
||
| 296 | */ |
||
| 297 | public function renderData() |
||
| 298 | { |
||
| 299 | $result = $this->database |
||
| 300 | ->table($this->dbTable) |
||
| 301 | ->select('id, |
||
| 302 | place, |
||
| 303 | DATE_FORMAT(start_date, "%d. %m. %Y") AS start_date, |
||
| 304 | DATE_FORMAT(end_date, "%d. %m. %Y") AS end_date, |
||
| 305 | DATE_FORMAT(open_reg, "%d. %m. %Y %H:%i:%s") AS open_reg, |
||
| 306 | DATE_FORMAT(close_reg, "%d. %m. %Y %H:%i:%s") AS close_reg, |
||
| 307 | contact, |
||
| 308 | email, |
||
| 309 | gsm') |
||
| 310 | ->where('deleted', '0') |
||
| 311 | ->limit(30) |
||
| 312 | ->fetchAll(); |
||
| 313 | |||
| 314 | $html_row = ""; |
||
| 315 | |||
| 316 | if(!$result) { |
||
| 317 | $html_row .= "<tr class='radek1'>"; |
||
| 318 | $html_row .= "<td><img class='edit' src='" . IMG_DIR . "icons/edit2.gif' /></td>\n"; |
||
| 319 | $html_row .= "<td><img class='edit' src='" . IMG_DIR . "icons/delete2.gif' /></td>\n"; |
||
| 320 | $html_row .= "<td colspan='11' class='emptyTable'>Nejsou k dispozici žádné položky.</td>"; |
||
| 321 | $html_row .= "</tr>"; |
||
| 322 | } else { |
||
| 323 | foreach($result as $data) { |
||
| 324 | $html_row .= "<tr class='radek1'>"; |
||
| 325 | $html_row .= "\t\t\t<td><a href='process.php?id=".$data['id']."&cms=edit&page=meetings' title='Upravit'><img class='edit' src='".IMG_DIR."icons/edit.gif' /></a></td>\n"; |
||
| 326 | $html_row .= "\t\t\t<td><a href=\"javascript:confirmation('?id=".$data['id']."&cms=del', 'sraz: ".$data['place']." ".$data['start_date']." -> Opravdu SMAZAT tento sraz? Jste si jisti?')\" title='Odstranit'><img class='edit' src='".IMG_DIR."icons/delete.gif' /></a></td>\n"; |
||
| 327 | $html_row .= "\t\t\t<td class='text'>".$data['id']."</td>\n"; |
||
| 328 | $html_row .= "\t\t\t<td class='text'>".$data['place']."</td>\n"; |
||
| 329 | $html_row .= "\t\t\t<td class='text'>".$data['start_date']."</td>\n"; |
||
| 330 | $html_row .= "\t\t\t<td class='text'>".$data['end_date']."</td>\n"; |
||
| 331 | $html_row .= "\t\t\t<td class='text'>".$data['open_reg']."</td>\n"; |
||
| 332 | $html_row .= "\t\t\t<td class='text'>".$data['close_reg']."</td>\n"; |
||
| 333 | $html_row .= "\t\t\t<td class='text'>".$data['contact']."</td>\n"; |
||
| 334 | $html_row .= "\t\t\t<td class='text'>".$data['email']."</td>\n"; |
||
| 335 | $html_row .= "\t\t\t<td class='text'>".$data['gsm']."</td>\n"; |
||
| 336 | $html_row .= "</tr>"; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | // table head |
||
| 341 | $html_thead = "\t<tr>\n"; |
||
| 342 | $html_thead .= "\t\t<th></th>\n"; |
||
| 343 | $html_thead .= "\t\t<th></th>\n"; |
||
| 344 | $html_thead .= "\t\t<th class='tab1'>id</th>\n"; |
||
| 345 | $html_thead .= "\t\t<th class='tab1'>místo</th>\n"; |
||
| 346 | $html_thead .= "\t\t<th class='tab1'>začátek</th>\n"; |
||
| 347 | $html_thead .= "\t\t<th class='tab1'>konec</th>\n"; |
||
| 348 | $html_thead .= "\t\t<th class='tab1'>otevření registrace</th>\n"; |
||
| 349 | $html_thead .= "\t\t<th class='tab1'>uzavření registrace</th>\n"; |
||
| 350 | $html_thead .= "\t\t<th class='tab1'>kontakt</th>\n"; |
||
| 351 | $html_thead .= "\t\t<th class='tab1'>e-mail</th>\n"; |
||
| 352 | $html_thead .= "\t\t<th class='tab1'>telefon</th>\n"; |
||
| 353 | $html_thead .= "\t</tr>\n"; |
||
| 354 | |||
| 355 | // table foot |
||
| 356 | $html_tfoot = $html_thead; |
||
| 357 | |||
| 358 | // table |
||
| 359 | $html_table = "<table id='MeetingsTable' class='list tablesorter'>\n"; |
||
| 360 | $html_table .= "\t<thead>\n"; |
||
| 361 | $html_table .= $html_thead; |
||
| 362 | $html_table .= "\t</thead>\n"; |
||
| 363 | $html_table .= "\t<tfoot>\n"; |
||
| 364 | $html_table .= $html_tfoot; |
||
| 365 | $html_table .= "\t</tfoot>\n"; |
||
| 366 | $html_table .= "\t<tbody>\n"; |
||
| 367 | $html_table .= $html_row; |
||
| 368 | $html_table .= "\t</tbody>\n"; |
||
| 369 | $html_table .= "</table>\n"; |
||
| 370 | |||
| 371 | return $html_table; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param integer $meetingId |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setRegistrationHandlers($meetingId = 1) |
||
| 379 | { |
||
| 380 | $meeting = $this->getDatabase() |
||
| 381 | ->table($this->getTable()) |
||
| 382 | ->select('id') |
||
| 383 | ->select('place') |
||
| 384 | ->select('DATE_FORMAT(start_date, "%Y") AS year') |
||
| 385 | ->select('UNIX_TIMESTAMP(open_reg) AS open_reg') |
||
| 386 | ->select('UNIX_TIMESTAMP(close_reg) AS close_reg') |
||
| 387 | ->where('id', $meetingId) |
||
| 388 | ->order('id DESC') |
||
| 389 | ->limit(1) |
||
| 390 | ->fetch(); |
||
| 391 | |||
| 392 | $this->setRegHeading($meeting->place . ' ' . $meeting->year); |
||
| 393 | $this->setRegClosing($meeting->close_reg); |
||
| 394 | $this->setRegOpening($meeting->open_reg); |
||
| 395 | |||
| 396 | return $this; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getRegOpening() |
||
| 403 | { |
||
| 404 | return $this->regOpening; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $value |
||
| 409 | * @return $this |
||
| 410 | */ |
||
| 411 | public function setRegOpening($value = '') |
||
| 412 | { |
||
| 413 | $this->regOpening = $value; |
||
| 414 | |||
| 415 | return $this; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getRegClosing() |
||
| 422 | { |
||
| 423 | return $this->regClosing; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $value |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function setRegClosing($value = '') |
||
| 431 | { |
||
| 432 | $this->regClosing = $value; |
||
| 433 | |||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getRegHeading() |
||
| 441 | { |
||
| 442 | return $this->regHeading; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $value |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setRegHeading($value = '') |
||
| 450 | { |
||
| 451 | $this->regHeading = $value; |
||
| 452 | |||
| 453 | return $this; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Is registration open? |
||
| 458 | * |
||
| 459 | * @return boolean |
||
| 460 | */ |
||
| 461 | public function isRegOpen($debug = false) |
||
| 462 | { |
||
| 463 | return (($this->getRegOpening() < time()) && (time() < $this->getRegClosing()) || $debug); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param integer $id |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getProvinceNameById($id) |
||
| 471 | { |
||
| 472 | return $this->getDatabase() |
||
| 473 | ->table('kk_provinces') |
||
| 474 | ->select('province_name') |
||
| 475 | ->where('id', $id) |
||
| 476 | ->limit(1) |
||
| 477 | ->fetchField('province_name'); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param integer|string $meetingId |
||
| 482 | * @return ActiveRow |
||
| 483 | */ |
||
| 484 | public function getPlaceAndYear($meetingId) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @return ActiveRow |
||
| 497 | */ |
||
| 498 | public function getMenuItems() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return integer |
||
| 512 | */ |
||
| 513 | public function getLastMeetingId() |
||
| 522 | |||
| 523 | } |
||
| 524 |
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.