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 |
||
| 8 | abstract class Model implements ModelInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The table associated with the model. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $table; |
||
| 16 | |||
| 17 | protected $properties = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Store instance of database connection used. |
||
| 21 | * |
||
| 22 | * @var Pyjac\ORM\DatabaseConnection |
||
| 23 | */ |
||
| 24 | protected $databaseConnection; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The id of the model. |
||
| 28 | * |
||
| 29 | * @property string $id |
||
| 30 | */ |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a model instance. |
||
| 34 | */ |
||
| 35 | public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sets into $properties the $key => $value pairs. |
||
| 46 | * |
||
| 47 | * @param string $key |
||
| 48 | * @param string $val |
||
| 49 | */ |
||
| 50 | public function __set($key, $val) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $key |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function __get($key) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get all the model properties. |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function getProperties() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set model properties. |
||
| 79 | */ |
||
| 80 | public function setProperties(array $properties) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Pluralize the name of the child class. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getTableName() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Find the particular model with the passed id. |
||
| 102 | * |
||
| 103 | * @param int $id |
||
| 104 | * |
||
| 105 | * @return object |
||
| 106 | */ |
||
| 107 | public static function find($id) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the particular model with the passed id. |
||
| 116 | * |
||
| 117 | * @param int $id |
||
| 118 | * |
||
| 119 | * @return object|null |
||
| 120 | */ |
||
| 121 | public function get($id) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get all the models from the database. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public static function getAll() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns all the models from the database. |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function all() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Update the model in the database. |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function update() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Insert the model values into the database. |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function create() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Save the model data to the database. |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function save() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Delete a model from the database. |
||
| 226 | * |
||
| 227 | * @param int $id |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public static function destroy($id) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Delete model from the database. |
||
| 240 | * |
||
| 241 | * @param int $id |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function delete($id) |
||
| 253 | } |
||
| 254 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..