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 |
||
| 24 | class Model extends DatabaseQuery implements ModelInterface |
||
| 25 | { |
||
| 26 | //Inject the inflector trait |
||
| 27 | use Inflector; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * stripclassName() |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | public static function stripclassName() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the table name if defined in the model |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function tableName() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the fields to be fillables defined in the model |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function fields() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * getClassName() |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getClassName() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * getTableName() |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getTableName($connection) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * getALL() |
||
| 97 | * Get all record from the database |
||
| 98 | * |
||
| 99 | * @return object |
||
| 100 | */ |
||
| 101 | public function getALL($dbConnection = NULL) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * where($data, $condition) |
||
| 117 | * Get data from database where $data = $condition |
||
| 118 | * |
||
| 119 | * @return object |
||
| 120 | */ |
||
| 121 | public function where($data, $condition = NULL, $dbConnection = NULL) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * find($value) |
||
| 138 | * Find data from database where id = $value |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public static function find($value, $dbConnection = NULL) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * save() |
||
| 161 | * Insert data into database |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function save($dbConnection = NULL) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * update() |
||
| 181 | * Update details in database after ::find(2) |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function update($dbConnection = NULL) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * destroy($value) |
||
| 200 | * Delete data from database |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function destroy($value, $dbConnection = NULL) |
||
| 217 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: