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 | protected $properties = []; |
||
12 | |||
13 | /** |
||
14 | * Store instance of database connection used. |
||
15 | * |
||
16 | * @var Pyjac\ORM\DatabaseConnection |
||
17 | */ |
||
18 | protected $databaseConnection; |
||
19 | |||
20 | /** |
||
21 | * The id of the model. |
||
22 | * |
||
23 | * @property string $id |
||
24 | */ |
||
25 | |||
26 | /** |
||
27 | * Create a model instance. |
||
28 | */ |
||
29 | public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
||
30 | { |
||
31 | if ($databaseConnection == null) { |
||
32 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
||
33 | } |
||
34 | $this->databaseConnection = $databaseConnection; |
||
|
|||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Sets into $properties the $key => $value pairs. |
||
39 | * |
||
40 | * @param string $key |
||
41 | * @param string $val |
||
42 | */ |
||
43 | public function __set($key, $val) |
||
44 | { |
||
45 | $this->properties[$key] = $val; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $key |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | public function __get($key) |
||
57 | |||
58 | /** |
||
59 | * Get all the model properties. |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getProperties() |
||
64 | { |
||
65 | return $this->properties; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Set model properties. |
||
70 | */ |
||
71 | public function setProperties(array $properties) |
||
75 | |||
76 | /** |
||
77 | * Pluralize the name of the child class. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getTableName() |
||
82 | { |
||
83 | $className = explode('\\', get_called_class()); |
||
84 | |||
85 | return Inflector::pluralize(strtolower(end($className))); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Find the particular model with the passed id. |
||
90 | * |
||
91 | * @param int $id |
||
92 | * |
||
93 | * @return object |
||
94 | */ |
||
95 | public static function find($id) |
||
101 | |||
102 | /** |
||
103 | * Get the particular model with the passed id. |
||
104 | * |
||
105 | * @param int $id |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | public function get($id) |
||
121 | |||
122 | /** |
||
123 | * Get all the models from the database. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | public static function getAll() |
||
133 | |||
134 | /** |
||
135 | * Returns all the models from the database. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function all() |
||
147 | |||
148 | /** |
||
149 | * Update the model in the database. |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | public function update() |
||
173 | |||
174 | /** |
||
175 | * Insert the model values into the database. |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | public function create() |
||
201 | |||
202 | /** |
||
203 | * Save the model data to the database. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function save() |
||
211 | |||
212 | /** |
||
213 | * Delete a model from the database. |
||
214 | * |
||
215 | * @param int $id |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public static function destroy($id) |
||
225 | |||
226 | /** |
||
227 | * Delete model from the database. |
||
228 | * |
||
229 | * @param int $id |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function delete($id) |
||
241 | } |
||
242 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.