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) |
||
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) |
||
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() |
||
101 | |||
102 | /** |
||
103 | * Find the particular model with the passed id. |
||
104 | * |
||
105 | * @param int $id |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | public static function find($id) |
||
115 | |||
116 | /** |
||
117 | * Get the particular model with the passed id. |
||
118 | * |
||
119 | * @param int $id |
||
120 | * |
||
121 | * @return object |
||
122 | */ |
||
123 | public function get($id) |
||
135 | |||
136 | /** |
||
137 | * Get all the models from the database. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public static function getAll() |
||
147 | |||
148 | /** |
||
149 | * Returns all the models from the database. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function all() |
||
161 | |||
162 | /** |
||
163 | * Update the model in the database. |
||
164 | * |
||
165 | * @return int |
||
166 | */ |
||
167 | public function update() |
||
187 | |||
188 | /** |
||
189 | * Insert the model values into the database. |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | public function create() |
||
215 | |||
216 | /** |
||
217 | * Save the model data to the database. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function save() |
||
225 | |||
226 | /** |
||
227 | * Delete a model from the database. |
||
228 | * |
||
229 | * @param int $id |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public static function destroy($id) |
||
239 | |||
240 | /** |
||
241 | * Delete model from the database. |
||
242 | * |
||
243 | * @param int $id |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function delete($id) |
||
255 | } |
||
256 |
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..