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 |
||
18 | abstract class Db { |
||
19 | |||
20 | protected $data; |
||
21 | |||
22 | protected $tableName; |
||
23 | |||
24 | protected $insertStatement; |
||
25 | |||
26 | protected $loadStatement; |
||
27 | |||
28 | public function __construct($data = array()){ |
||
31 | |||
32 | /** |
||
33 | * Insert current object data into database |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function insert(){ |
||
40 | |||
41 | /** |
||
42 | * Get id of the recently inserted record |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function getLastInsertId(){ |
||
48 | |||
49 | /** |
||
50 | * Get single record by primary key |
||
51 | * @param int $value primary key value |
||
52 | * @return \OCA\Documents\Db |
||
53 | */ |
||
54 | public function load($value){ |
||
67 | |||
68 | /** |
||
69 | * Get single record matching condition |
||
70 | * @param string $field for WHERE condition |
||
71 | * @param mixed $value matching value(s) |
||
72 | * @return \OCA\Documents\Db |
||
73 | * @throws Exception |
||
74 | */ |
||
75 | public function loadBy($field, $value){ |
||
91 | |||
92 | /** |
||
93 | * Delete records matching the condition |
||
94 | * @param string $field for WHERE condition |
||
95 | * @param mixed $value matching value(s) |
||
96 | */ |
||
97 | public function deleteBy($field, $value){ |
||
111 | |||
112 | /** |
||
113 | * Get all records from the table |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getCollection(){ |
||
124 | |||
125 | /** |
||
126 | * Get array of matching records |
||
127 | * @param string $field for WHERE condition |
||
128 | * @param mixed $value matching value(s) |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getCollectionBy($field, $value){ |
||
151 | |||
152 | /** |
||
153 | * Get object data |
||
154 | * @return Array |
||
155 | */ |
||
156 | public function getData(){ |
||
159 | |||
160 | /** |
||
161 | * Set object data |
||
162 | * @param array $data |
||
163 | */ |
||
164 | public function setData($data){ |
||
167 | |||
168 | /** |
||
169 | * Check if there are any data in current object |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function hasData(){ |
||
175 | |||
176 | /** |
||
177 | * Build placeholders for the query with variable input data |
||
178 | * @param string $field field name |
||
179 | * @param Array $array data |
||
180 | * @return String `field` IN (?, ?...) placeholders matching the number of elements in array |
||
181 | */ |
||
182 | protected function buildInQuery($field, $array){ |
||
188 | |||
189 | /** |
||
190 | * Execute a query on database |
||
191 | * @param string $statement query to be executed |
||
192 | * @param mixed $args value(s) for the query. |
||
193 | * If omited the query will be run on the current object $data |
||
194 | * @return mixed (array/false) |
||
195 | */ |
||
196 | protected function execute($statement, $args = null){ |
||
197 | $query = \OC::$server->getDatabaseConnection()->prepare($statement); |
||
198 | |||
199 | if (!is_null($args)){ |
||
200 | $result = $query->execute($args); |
||
201 | } elseif (count($this->data)){ |
||
202 | $result = $query->execute($this->data); |
||
203 | } else { |
||
204 | $result = $query->execute(); |
||
205 | } |
||
206 | |||
207 | return $result ? $query : false; |
||
208 | } |
||
209 | |||
210 | public function __call($name, $arguments){ |
||
220 | } |
||
221 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.