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 |
||
| 9 | abstract class Model implements ModelInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The table associated with the model. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $table; |
||
| 17 | |||
| 18 | protected $properties = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Store instance of database connection used. |
||
| 22 | * |
||
| 23 | * @var Pyjac\ORM\DatabaseConnection |
||
| 24 | */ |
||
| 25 | protected $databaseConnection; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The id of the model. |
||
| 29 | * |
||
| 30 | * @property string $id |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a model instance. |
||
| 35 | */ |
||
| 36 | public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
||
| 37 | { |
||
| 38 | if ($databaseConnection == null) { |
||
| 39 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
||
| 40 | } else { |
||
| 41 | $this->databaseConnection = $databaseConnection; |
||
|
|
|||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Sets into $properties the $key => $value pairs. |
||
| 47 | * |
||
| 48 | * @param string $key |
||
| 49 | * @param string $val |
||
| 50 | */ |
||
| 51 | public function __set($key, $val) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $key |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function __get($key) |
||
| 62 | { |
||
| 63 | if (isset($this->properties[$key])) { |
||
| 64 | return $this->properties[$key]; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get all the model properties. |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getProperties() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set model properties. |
||
| 80 | */ |
||
| 81 | public function setProperties(array $properties) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Pluralize the name of the child class. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getTableName() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Find the particular model with the passed id. |
||
| 103 | * |
||
| 104 | * @param int $id |
||
| 105 | * |
||
| 106 | * @return object |
||
| 107 | */ |
||
| 108 | public static function find($id) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the particular model with the passed id. |
||
| 117 | * |
||
| 118 | * @param int $id |
||
| 119 | * |
||
| 120 | * @return object |
||
| 121 | */ |
||
| 122 | public function get($id) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get all the models from the database. |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public static function getAll() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns all the models from the database. |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function all() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Update the model in the database. |
||
| 163 | * |
||
| 164 | * @return int |
||
| 165 | */ |
||
| 166 | public function update() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Insert the model values into the database. |
||
| 189 | * |
||
| 190 | * @return int |
||
| 191 | */ |
||
| 192 | public function create() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Save the model data to the database. |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function save() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Delete a model from the database. |
||
| 227 | * |
||
| 228 | * @param int $id |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public static function destroy($id) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Delete model from the database. |
||
| 241 | * |
||
| 242 | * @param int $id |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function delete($id) |
||
| 254 | } |
||
| 255 |
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..