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 |
||
| 5 | View Code Duplication | abstract class Model implements ModelInterface |
|
|
|
|||
| 6 | { |
||
| 7 | |||
| 8 | |||
| 9 | protected $properties = []; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Store instance of database connection used. |
||
| 13 | * @var [type] |
||
| 14 | */ |
||
| 15 | protected $databaseConnection; |
||
| 16 | |||
| 17 | public function __construct() |
||
| 22 | /** |
||
| 23 | * @param string $key rep column name |
||
| 24 | * @param string $val rep column value |
||
| 25 | * sets into $propertie the $key => $value pairs |
||
| 26 | */ |
||
| 27 | public function __set($key, $val) |
||
| 31 | /** |
||
| 32 | * @param string $key reps the column name |
||
| 33 | * @return $key and $value |
||
| 34 | */ |
||
| 35 | public function __get($key) |
||
| 39 | /** |
||
| 40 | * Get all the model properties |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | public function getProperties() |
||
| 48 | /** |
||
| 49 | * Gets the name of the child class only |
||
| 50 | * without the namespace |
||
| 51 | * @var $className |
||
| 52 | * @var $table |
||
| 53 | * @return $table |
||
| 54 | */ |
||
| 55 | public function getTableName() |
||
| 61 | /** |
||
| 62 | * returns a particular record |
||
| 63 | * @param $id reps the record id |
||
| 64 | * @param $connection initialised to null |
||
| 65 | * @return object |
||
| 66 | */ |
||
| 67 | public static function find($id) |
||
| 72 | /** |
||
| 73 | * returns a particular record |
||
| 74 | * @param $id reps the record id |
||
| 75 | * @param $connection initialised to null |
||
| 76 | * @return object |
||
| 77 | */ |
||
| 78 | public function get($id) |
||
| 89 | |||
| 90 | public static function getAll() |
||
| 95 | |||
| 96 | public function all() |
||
| 105 | /** update table with instance properties |
||
| 106 | * |
||
| 107 | */ |
||
| 108 | private function update() |
||
| 132 | /** |
||
| 133 | * insert instance data into the table |
||
| 134 | */ |
||
| 135 | private function create() |
||
| 165 | /** |
||
| 166 | * get db connection |
||
| 167 | */ |
||
| 168 | public function getConnection($connection = null) |
||
| 175 | /** |
||
| 176 | * checks if the id exists |
||
| 177 | * update if exist |
||
| 178 | * create if not exist |
||
| 179 | */ |
||
| 180 | public function save() |
||
| 188 | /** |
||
| 189 | * @param row reps record id |
||
| 190 | * @param $connection initialised to null |
||
| 191 | * @return boolean |
||
| 192 | */ |
||
| 193 | public static function destroy($id) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Handle dynamic static method calls into the method. |
||
| 208 | * |
||
| 209 | * @param string $method |
||
| 210 | * @param array $parameters |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public static function __callStatic($method, $parameters) |
||
| 220 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.