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 |
||
| 17 | class VisitorModel extends BaseModel |
||
| 18 | { |
||
| 19 | |||
| 20 | /** @var string search pattern */ |
||
| 21 | public $search; |
||
| 22 | |||
| 23 | /** @var Meeting Meeting class */ |
||
| 24 | public $Meeting; |
||
| 25 | |||
| 26 | /** @var Meal Meals class */ |
||
| 27 | public $Meals; |
||
| 28 | |||
| 29 | /** @var Program Programs class */ |
||
| 30 | public $Programs; |
||
| 31 | |||
| 32 | /** @var Blocks Blocks class */ |
||
| 33 | public $Blocks; |
||
| 34 | |||
| 35 | /** @var int meeting price */ |
||
| 36 | public $meeting_price; |
||
| 37 | |||
| 38 | /** @var int meeting advance */ |
||
| 39 | private $meeting_advance; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Array of database programs table columns |
||
| 43 | * |
||
| 44 | * @var array dbColumns[] |
||
| 45 | */ |
||
| 46 | public $dbColumns = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Array of form names |
||
| 50 | * |
||
| 51 | * @var array formNames[] |
||
| 52 | */ |
||
| 53 | public $formNames = array(); |
||
| 54 | |||
| 55 | protected $table = 'kk_visitors'; |
||
| 56 | |||
| 57 | protected $columns = [ |
||
| 58 | 'name', |
||
| 59 | 'surname', |
||
| 60 | 'nick', |
||
| 61 | 'email', |
||
| 62 | 'birthday', |
||
| 63 | 'street', |
||
| 64 | 'city', |
||
| 65 | 'postal_code', |
||
| 66 | 'group_num', |
||
| 67 | 'group_name', |
||
| 68 | 'troop_name', |
||
| 69 | 'province', |
||
| 70 | 'arrival', |
||
| 71 | 'departure', |
||
| 72 | 'comment', |
||
| 73 | 'question', |
||
| 74 | 'question2', |
||
| 75 | 'bill', |
||
| 76 | 'cost', |
||
| 77 | 'meeting', |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** konstruktor */ |
||
| 81 | public function __construct( |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function getColumns() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Create a new visitor |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function assemble(array $DB_data, $meals_data, $programs_data, $returnGuid = false) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Modify a visitor |
||
| 195 | * |
||
| 196 | * @param int $visitor_id ID of a visitor |
||
| 197 | * @param array $db_data Visitor's database data |
||
| 198 | * @param array $meals_data Data of meals |
||
| 199 | * @param array $programs_data Program's data |
||
| 200 | * @return mixed TRUE or array of errors |
||
| 201 | */ |
||
| 202 | public function modify($ID_visitor, $DB_data, $meals_data, $programs_data) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Modify a visitor |
||
| 241 | * |
||
| 242 | * @param int $visitor_id ID of a visitor |
||
| 243 | * @param array $db_data Visitor's database data |
||
| 244 | * @param array $meals_data Data of meals |
||
| 245 | * @param array $programs_data Program's data |
||
| 246 | * @return mixed TRUE or array of errors |
||
| 247 | */ |
||
| 248 | public function modifyByGuid($guid, $DB_data, $meals_data, $programs_data) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Delete one or multiple record/s |
||
| 289 | * |
||
| 290 | * @param int ID/s of record |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | public function delete($id) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set as checked one or multiple record/s |
||
| 305 | * |
||
| 306 | * @param int ID/s of record |
||
| 307 | * @param int 0 | 1 |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | public function checked($id, $value) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get count of visitors |
||
| 322 | * |
||
| 323 | * @return integer |
||
| 324 | */ |
||
| 325 | public function getCount() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $search |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setSearch($search) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | protected function getSearch() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | protected function buildSearchQuery() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Modify the visitor's bill |
||
| 375 | * |
||
| 376 | * @param int ID/s of visitor |
||
| 377 | * @param string type of payment (pay | advance) |
||
| 378 | * @return string error message or true |
||
| 379 | */ |
||
| 380 | public function payCharge($query_id, $type) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get recipients by ids |
||
| 399 | * |
||
| 400 | * @param mixed ID of visitor |
||
| 401 | * @return mixed result |
||
| 402 | */ |
||
| 403 | public function getRecipients($ids) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get visitor's programs |
||
| 415 | * |
||
| 416 | * @param int ID of visitor |
||
| 417 | * @return mixed result |
||
| 418 | */ |
||
| 419 | public function getVisitorPrograms($visitorId) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Render program switcher for unique visitor |
||
| 430 | * |
||
| 431 | * @param int ID of meeting |
||
| 432 | * @param int ID of visitor |
||
| 433 | * @return string html |
||
| 434 | */ |
||
| 435 | public function renderProgramSwitcher($meetingId, $visitorId) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return Row |
||
| 461 | */ |
||
| 462 | public function all() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Return visitor by id |
||
| 488 | * |
||
| 489 | * @param int $id |
||
| 490 | * @return ActiveRow |
||
| 491 | */ |
||
| 492 | public function findById($id) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return visitor by guid |
||
| 499 | * |
||
| 500 | * @param string $guid |
||
| 501 | * @return ActiveRow |
||
| 502 | */ |
||
| 503 | public function findByGuid($guid) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param array $queryId |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getSerializedMailAddress(array $queryId = []) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param int $id |
||
| 534 | * @return Visitor |
||
| 535 | */ |
||
| 536 | public function getBill($id) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param int ID of visitor |
||
| 547 | * @return mixed result |
||
| 548 | */ |
||
| 549 | public function findVisitorPrograms(int $visitorId) |
||
| 557 | |||
| 558 | } |
||
| 559 |
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.