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 |
||
16 | class PotatoModel extends DatabaseConnection |
||
17 | { |
||
18 | /** |
||
19 | * Table name from Child Class |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected static $table; |
||
24 | |||
25 | /** |
||
26 | * Unique ID field from Child Class |
||
27 | * |
||
28 | * @var [type] |
||
29 | */ |
||
30 | protected static $uniqueId; |
||
31 | |||
32 | /** |
||
33 | * Contains the unique ID value |
||
34 | * |
||
35 | * @var null |
||
36 | */ |
||
37 | protected static $uniqueIdValue = null; |
||
38 | |||
39 | /** |
||
40 | * Contains a PDO Connection Object returned by the |
||
41 | * Database Connection class |
||
42 | * |
||
43 | * @var Object |
||
44 | */ |
||
45 | protected static $connection = null; |
||
46 | |||
47 | /** |
||
48 | * Associative array that contains the name of each field and value |
||
49 | * as a key value pair |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected static $data = []; |
||
54 | |||
55 | /** |
||
56 | * If set to true, the save method performs an update on existing row |
||
57 | * |
||
58 | * If set to false, the save method inserts a new row |
||
59 | * |
||
60 | * @var boolean |
||
61 | */ |
||
62 | protected static $update = false; |
||
63 | |||
64 | /** |
||
65 | * Create database connection |
||
66 | * |
||
67 | * Get all things up and running |
||
68 | */ |
||
69 | public function __construct() |
||
75 | |||
76 | /** |
||
77 | * Add the value set in the child class a key value pair to the $data array |
||
78 | * |
||
79 | * @param string $key Contains the name of the field e.g firstName |
||
80 | * @param string/int $value Contains the value of the field e.g John |
||
|
|||
81 | */ |
||
82 | public function __set($key, $value) |
||
86 | |||
87 | /** |
||
88 | * getAll |
||
89 | * |
||
90 | * Returns all rows from a table |
||
91 | * |
||
92 | * @return array returns an array of rows in a table |
||
93 | */ |
||
94 | final public static function getAll() |
||
113 | |||
114 | /** |
||
115 | * save |
||
116 | * |
||
117 | * public function that saves a new instance of the child class data into the database |
||
118 | * |
||
119 | * Create PDO connection, construct SQL Statement, execute the statement |
||
120 | * and return the primary key value of inserted row |
||
121 | * |
||
122 | * @return integer return the primary key value of inserted row |
||
123 | */ |
||
124 | public function save() |
||
148 | |||
149 | |||
150 | public static function find($fieldId) |
||
173 | |||
174 | /** |
||
175 | * destroy |
||
176 | * |
||
177 | * Takes an ID parameter, deletes from the table where the unique ID |
||
178 | * matches the ID parameter |
||
179 | * |
||
180 | * @param integer $id a unique ID from the table |
||
181 | * |
||
182 | * @return boolean should return true or false based on |
||
183 | * whether it was deleted or not |
||
184 | */ |
||
185 | public static function destroy($fieldId) |
||
200 | |||
201 | /** |
||
202 | * getTableName |
||
203 | * |
||
204 | * If a table name is not set in the child class, return a 'snaked' table name. |
||
205 | * |
||
206 | * else return the set table name |
||
207 | * |
||
208 | * @return string table name |
||
209 | */ |
||
210 | public static function getTableName() |
||
220 | |||
221 | /** |
||
222 | * getUniqueId |
||
223 | * |
||
224 | * If the unique ID is set in the child class, return it |
||
225 | * |
||
226 | * else return a default unique ID of 'id' |
||
227 | * |
||
228 | * @return string Unique ID table ID |
||
229 | */ |
||
230 | public static function getUniqueId() |
||
238 | |||
239 | /** |
||
240 | * getDataFieldValues |
||
241 | * |
||
242 | * return an comma separated string of field value pairs |
||
243 | * in assoc array |
||
244 | * |
||
245 | * @param array $fieldValueArray An associative array of all the field-value pairs |
||
246 | * @return string $data A string of comma separated values for SQL statement |
||
247 | */ |
||
248 | View Code Duplication | private function getDataFieldValues($fieldValueArray) |
|
264 | |||
265 | /** |
||
266 | * getUpdateFieldValues |
||
267 | * |
||
268 | * returns comma sperarated string of field values in the SQL update format |
||
269 | * |
||
270 | * @param array $fieldValueArray An associative array of all the field-value pairs |
||
271 | * @return string comma sperarated string of field values in the SQL update format |
||
272 | */ |
||
273 | View Code Duplication | private function getUpdateFieldValues($fieldValueArray) |
|
288 | } |
||
289 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.