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 | 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 | /** |
||
18 | * Create a model instance. |
||
19 | * |
||
20 | */ |
||
21 | public function __construct() |
||
26 | /** |
||
27 | * Sets into $properties the $key => $value pairs |
||
28 | * |
||
29 | * @param string $key |
||
30 | * @param string $val |
||
31 | * |
||
32 | */ |
||
33 | public function __set($key, $val) |
||
37 | /** |
||
38 | * @param string $key |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function __get($key) |
||
46 | /** |
||
47 | * Get all the model properties. |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | public function getProperties() |
||
55 | /** |
||
56 | * Gets the name of the child class with a 's'. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getTableName() |
||
66 | /** |
||
67 | * Find the particular model with the passed id. |
||
68 | * |
||
69 | * @param int $id |
||
70 | * |
||
71 | * @return object |
||
72 | */ |
||
73 | public static function find($id) |
||
78 | |||
79 | /** |
||
80 | * Get the particular model with the passed id. |
||
81 | * |
||
82 | * @param int $id |
||
83 | * |
||
84 | * @return object |
||
85 | */ |
||
86 | public function get($id) |
||
97 | |||
98 | /** |
||
99 | * Get all the models from the database. |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public static function getAll() |
||
108 | |||
109 | /** |
||
110 | * Returns all the models from the database. |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | public function all() |
||
123 | /** |
||
124 | * Update the model in the database. |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | private function update() |
||
148 | |||
149 | /** |
||
150 | * Insert the model values into the database. |
||
151 | * |
||
152 | * @return int |
||
153 | */ |
||
154 | private function create() |
||
177 | |||
178 | /** |
||
179 | * Save the model data to the database. |
||
180 | * |
||
181 | * @return boolean |
||
182 | */ |
||
183 | public function save() |
||
187 | |||
188 | /** |
||
189 | * Delete a model from the database. |
||
190 | * @param int $id |
||
191 | * @return boolean |
||
192 | */ |
||
193 | public static function destroy($id) |
||
198 | |||
199 | /** |
||
200 | * Delete model from the database. |
||
201 | * |
||
202 | * @param int $id |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function delete($id) |
||
212 | |||
213 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.