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:
| 1 | <?php |
||
| 18 | class VisitorModel extends BaseModel |
||
| 19 | { |
||
| 20 | |||
| 21 | /** @var string search pattern */ |
||
| 22 | public $search; |
||
| 23 | |||
| 24 | /** @var Meeting Meeting class */ |
||
| 25 | public $Meeting; |
||
| 26 | |||
| 27 | /** @var Meal Meals class */ |
||
| 28 | public $Meals; |
||
| 29 | |||
| 30 | /** @var Program Programs class */ |
||
| 31 | public $Programs; |
||
| 32 | |||
| 33 | /** @var Blocks Blocks class */ |
||
| 34 | public $Blocks; |
||
| 35 | |||
| 36 | /** @var int meeting price */ |
||
| 37 | public $meeting_price; |
||
| 38 | |||
| 39 | /** @var int meeting advance */ |
||
| 40 | private $meeting_advance; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array of database programs table columns |
||
| 44 | * |
||
| 45 | * @var array dbColumns[] |
||
| 46 | */ |
||
| 47 | public $dbColumns = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of form names |
||
| 51 | * |
||
| 52 | * @var array formNames[] |
||
| 53 | */ |
||
| 54 | public $formNames = array(); |
||
| 55 | |||
| 56 | protected $table = 'kk_visitors'; |
||
| 57 | |||
| 58 | protected $columns = [ |
||
| 59 | 'name', |
||
| 60 | 'surname', |
||
| 61 | 'nick', |
||
| 62 | 'email', |
||
| 63 | 'birthday', |
||
| 64 | 'street', |
||
| 65 | 'city', |
||
| 66 | 'postal_code', |
||
| 67 | 'group_num', |
||
| 68 | 'group_name', |
||
| 69 | 'troop_name', |
||
| 70 | 'province', |
||
| 71 | 'arrival', |
||
| 72 | 'departure', |
||
| 73 | 'comment', |
||
| 74 | 'question', |
||
| 75 | 'question2', |
||
| 76 | 'bill', |
||
| 77 | 'cost', |
||
| 78 | 'meeting', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** konstruktor */ |
||
| 82 | public function __construct( |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getColumns() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create a new visitor |
||
| 134 | * |
||
| 135 | * @return boolean |
||
| 136 | */ |
||
| 137 | public function assemble(array $DB_data, $meals_data, $programs_data, $returnGuid = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Modify a visitor |
||
| 197 | * |
||
| 198 | * @param int $visitor_id ID of a visitor |
||
| 199 | * @param array $db_data Visitor's database data |
||
| 200 | * @param array $meals_data Data of meals |
||
| 201 | * @param array $programs_data Program's data |
||
| 202 | * @return mixed TRUE or array of errors |
||
| 203 | */ |
||
| 204 | public function modify($ID_visitor, $DB_data, $meals_data, $programs_data) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Modify a visitor |
||
| 248 | * |
||
| 249 | * @param int $visitor_id ID of a visitor |
||
| 250 | * @param array $db_data Visitor's database data |
||
| 251 | * @param array $meals_data Data of meals |
||
| 252 | * @param array $programs_data Program's data |
||
| 253 | * @return mixed TRUE or array of errors |
||
| 254 | */ |
||
| 255 | public function modifyByGuid($guid, $DB_data, $meals_data, $programs_data) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Delete one or multiple record/s |
||
| 301 | * |
||
| 302 | * @param int ID/s of record |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function delete($id) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set as checked one or multiple record/s |
||
| 317 | * |
||
| 318 | * @param int ID/s of record |
||
| 319 | * @param int 0 | 1 |
||
| 320 | * @return boolean |
||
| 321 | */ |
||
| 322 | public function checked($id, $value) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get count of visitors |
||
| 334 | * |
||
| 335 | * @return integer |
||
| 336 | */ |
||
| 337 | public function getCount() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $search |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function setSearch($search) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | protected function getSearch() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | protected function buildSearchQuery() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Modify the visitor's bill |
||
| 387 | * |
||
| 388 | * @param int ID/s of visitor |
||
| 389 | * @param string type of payment (pay | advance) |
||
| 390 | * @return string error message or true |
||
| 391 | */ |
||
| 392 | public function payCharge($query_id, $type) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get recipients by ids |
||
| 411 | * |
||
| 412 | * @param mixed ID of visitor |
||
| 413 | * @return mixed result |
||
| 414 | */ |
||
| 415 | public function getRecipients($ids) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get visitor's programs |
||
| 427 | * |
||
| 428 | * @param int ID of visitor |
||
| 429 | * @return mixed result |
||
| 430 | */ |
||
| 431 | public function getVisitorPrograms($visitorId) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Render program switcher for unique visitor |
||
| 442 | * |
||
| 443 | * @param int ID of meeting |
||
| 444 | * @param int ID of visitor |
||
| 445 | * @return string html |
||
| 446 | */ |
||
| 447 | public function renderProgramSwitcher($meetingId, $visitorId) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return Row |
||
| 473 | */ |
||
| 474 | public function all() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Return visitor by id |
||
| 500 | * |
||
| 501 | * @param int $id |
||
| 502 | * @return ActiveRow |
||
| 503 | */ |
||
| 504 | public function findById($id) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Return visitor by guid |
||
| 511 | * |
||
| 512 | * @param string $guid |
||
| 513 | * @return ActiveRow |
||
| 514 | */ |
||
| 515 | public function findByGuid($guid) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param array $queryId |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public function getSerializedMailAddress(array $queryId = []) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param int $id |
||
| 545 | * @return Visitor |
||
| 546 | */ |
||
| 547 | public function getBill($id) |
||
| 555 | } |
||
| 556 |
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.