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 |
||
| 12 | class Model { |
||
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * Table name for the Model. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $table = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Array of validation errors |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $errors = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | * |
||
| 31 | */ |
||
| 32 | public function __construct(){ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * pluralize for table names |
||
| 40 | * |
||
| 41 | * Automatically selects a database table name based on a pluralized lowercase object class name |
||
| 42 | * (i.e. class 'User' => table 'users') |
||
| 43 | * |
||
| 44 | * @param string $word |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | private function pluralize($word){ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * delete record by id |
||
| 61 | * |
||
| 62 | * @param string $id |
||
| 63 | * @return bool |
||
| 64 | * @throws Exception if feed couldn't be deleted |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function deleteById($id){ |
|
| 75 | |||
| 76 | /** |
||
| 77 | * get errors |
||
| 78 | * |
||
| 79 | * @return array errors |
||
| 80 | */ |
||
| 81 | public function errors(){ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * is record exists? |
||
| 87 | * |
||
| 88 | * Almost all methods in model needs you to pass the current user id, |
||
| 89 | * Another approach is to create in Model class a property call id(will be inherited by all extending classes) |
||
| 90 | * Ex: |
||
| 91 | * Inside postsController: |
||
| 92 | * post->id = $postId |
||
| 93 | * post->updatePost(....) -> Inside updatePost() you can get the post id by: $this->id |
||
| 94 | * |
||
| 95 | * @param string $id |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function exists($id){ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Counting the number of a current model's table. |
||
| 108 | * |
||
| 109 | * @return integer |
||
| 110 | */ |
||
| 111 | public function countAll(){ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * adds parts to current query using an array. |
||
| 119 | * |
||
| 120 | * The array will have $key which is the field value, and the $value is the query to be added to the current query |
||
| 121 | * Only fields with existing value(false, and 0 are accepted as valid values) will be considered in our query. |
||
| 122 | * |
||
| 123 | * @param array $options |
||
| 124 | * @param string $implodeBy |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function applyOptions(array $options, $implodeBy){ |
||
| 138 | |||
| 139 | } |
||
| 140 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.