Total Complexity | 136 |
Total Lines | 512 |
Duplicated Lines | 16.41 % |
Changes | 0 |
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:
Complex classes like QuickInsertRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuickInsertRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class QuickInsertRepository |
||
6 | { |
||
7 | private static $mock = []; |
||
8 | private static $metadata = []; |
||
9 | private static $tableName; |
||
10 | |||
11 | public static $em; |
||
|
|||
12 | public static $connection; |
||
13 | public static $container; |
||
14 | |||
15 | public static function init($noFkCheck = false, $manager = null) |
||
31 | } |
||
32 | } |
||
33 | |||
34 | public static function close($noFkCheck = false) |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public static function isEmpty($variable) |
||
55 | } |
||
56 | |||
57 | private static function extract($object) |
||
64 | } |
||
65 | |||
66 | public static function extractExt($object, $em) |
||
90 | } |
||
91 | |||
92 | private static function buildExtra($extra) |
||
93 | { |
||
94 | $methods = [ |
||
95 | 'GROUP BY', |
||
96 | 'HAVING', |
||
97 | 'ORDER BY', |
||
98 | ]; |
||
99 | $sql = ''; |
||
100 | |||
101 | foreach ($methods as $method) { |
||
102 | if (isset($extra[$method])) { |
||
103 | $sql .= ' '.$method.' '; |
||
104 | if (is_array($extra[$method])) { |
||
105 | foreach ($extra[$method] as $group) { |
||
106 | $sql .= $group.' '; |
||
107 | } |
||
108 | } else { |
||
109 | $sql .= $extra[$method].' '; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | if (isset($extra['LIMIT'])) { |
||
115 | if (is_array($extra['LIMIT'])) { |
||
116 | if (isset($extra['LIMIT'][1])) { |
||
117 | $offset = $extra['LIMIT'][0]; |
||
118 | $limit = $extra['LIMIT'][1]; |
||
119 | } else { |
||
120 | $offset = 0; |
||
121 | $limit = $extra['LIMIT'][0]; |
||
122 | } |
||
123 | $sql .= 'LIMIT '.$offset.', '.$limit; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | $sql = str_replace(' ', ' ', $sql); |
||
128 | |||
129 | return $sql; |
||
130 | } |
||
131 | |||
132 | private static function buildWhere($tableName, $where) |
||
186 | } |
||
187 | |||
188 | public static function findNextId($tableName, &$out = null) |
||
212 | } |
||
213 | |||
214 | public static function findNextIdExt($object, $em, &$out = null) |
||
215 | { |
||
216 | self::init(true, $em); |
||
217 | $data = self::extractExt($object, $em); |
||
218 | |||
219 | return self::findNextId($data['table'], $out); |
||
220 | } |
||
221 | |||
222 | public static function get($object, $one = false, $where = [], $noFkCheck = true, $fields = [], $manager = null, $extra = [], &$out = null) |
||
223 | { |
||
224 | self::init($noFkCheck, $manager); |
||
225 | if (is_object($object)) { |
||
226 | self::extract($object); |
||
227 | $tableName = self::$tableName; |
||
228 | } else { |
||
229 | $tableName = $object; |
||
230 | } |
||
231 | $whereSql = self::buildWhere($tableName, $where); |
||
232 | $select = (isset($extra['MODE']) ? 'SELECT '.$extra['MODE'] : 'SELECT').' '; |
||
233 | if (!$fields) { |
||
234 | $sql = $select.'id FROM '.$tableName.' '.$whereSql; |
||
235 | } else { |
||
236 | $sql = $select.(implode(', ', $fields)).' FROM '.$tableName.' '.$whereSql; |
||
237 | } |
||
238 | if (!empty($extra)) { |
||
239 | $extraSql = self::buildExtra($extra); |
||
240 | $sql .= $extraSql; |
||
241 | } |
||
242 | if ($out) { |
||
243 | $out = $sql; |
||
244 | } |
||
245 | $sth = self::$connection->prepare($sql); |
||
246 | $sth->execute(); |
||
247 | $result = $sth->fetchAll(); |
||
248 | if ($one && $result) { |
||
249 | if (!$fields) { |
||
250 | return intval($result[0]['id']); |
||
251 | } else { |
||
252 | if (count($fields) === 1 && $fields[0] !== '*') { |
||
253 | return $result[0][$fields[0]]; |
||
254 | } else { |
||
255 | return $result[0]; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | self::close($noFkCheck); |
||
261 | if ($one || !$result) { |
||
262 | return null; |
||
263 | } |
||
264 | |||
265 | $field = null; |
||
266 | if (!$fields) { |
||
267 | $field = 'id'; |
||
268 | } elseif (count($fields) === 1 && $fields[0] !== '*') { |
||
269 | $field = $fields[0]; |
||
270 | } |
||
271 | |||
272 | if ($field) { |
||
273 | foreach ($result as &$res) { |
||
274 | $res = $res[$field]; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | return $result; |
||
279 | } |
||
280 | |||
281 | public static function persist($object, $full = false, $extraFields = [], $noFkCheck = false, $manager = null, &$out = null) |
||
282 | { |
||
283 | self::init($noFkCheck, $manager); |
||
284 | View Code Duplication | if (is_object($object)) { |
|
285 | self::extract($object); |
||
286 | $tableName = self::$tableName; |
||
287 | $columns = self::$mock[$tableName] ?: []; |
||
288 | $type = 'object'; |
||
289 | } else { |
||
290 | $tableName = $object['table_name']; |
||
291 | unset($object['table_name']); |
||
292 | $type = 'table'; |
||
293 | $columns = array_keys($object) ?: []; |
||
294 | } |
||
295 | |||
296 | $id = self::findNextId($tableName); |
||
297 | $keys = []; |
||
298 | $values = []; |
||
299 | |||
300 | View Code Duplication | if (!empty($extraFields) && isset($extraFields[$tableName])) { |
|
301 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
302 | } |
||
303 | |||
304 | if ($type === 'object') { |
||
305 | foreach ($columns as $value => $key) { |
||
306 | $variable = null; |
||
307 | if (!is_array($key) && !is_array($value)) { |
||
308 | if ($object->{'get'.ucfirst($value)}() instanceof \DateTime) { |
||
309 | $variable = "'".addslashes(trim($object->{'get'.ucfirst($value)}()->format('Y-m-d H:i:s')))."'"; |
||
310 | } else { |
||
311 | $variable = "'".addslashes(trim($object->{'get'.ucfirst($value)}()))."'"; |
||
312 | } |
||
313 | View Code Duplication | if (trim($variable) === '' || trim($variable) === "''" || (is_numeric($variable) && $variable === 0)) { |
|
314 | $variable = null; |
||
315 | } |
||
316 | View Code Duplication | if ($variable) { |
|
317 | $values[] = $variable; |
||
318 | $keys[] = $key; |
||
319 | if ($key === 'id') { |
||
320 | $idd = $object->{'get'.ucfirst($value)}(); |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } else { |
||
326 | foreach ($columns as $value => $key) { |
||
327 | $variable = null; |
||
328 | if (!is_array($key) && !is_array($value) && isset($object[$value])) { |
||
329 | if ($object[$value] instanceof \DateTime) { |
||
330 | $variable = "'".addslashes(trim($object[$value]->format('Y-m-d H:i:s')))."'"; |
||
331 | } else { |
||
332 | $variable = "'".addslashes(trim($object[$value]))."'"; |
||
333 | } |
||
334 | View Code Duplication | if (trim($variable) === '' || trim($variable) === "''" || (is_numeric($variable) && $variable === 0)) { |
|
335 | $variable = null; |
||
336 | } |
||
337 | View Code Duplication | if ($variable) { |
|
338 | $values[] = $variable; |
||
339 | $keys[] = $key; |
||
340 | if ($key === 'id') { |
||
341 | $idd = $object[$value]; |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | |||
348 | $sql = null; |
||
349 | if (!$full && !self::isEmpty($values)) { |
||
350 | $sql = ' |
||
351 | INSERT INTO |
||
352 | '.$tableName.' |
||
353 | (id, '.implode(',', $keys).") |
||
354 | VALUES |
||
355 | ({$id},".implode(',', $values).') |
||
356 | '; |
||
357 | } elseif ($full && !self::isEmpty($values)) { |
||
358 | $id = $idd; |
||
359 | $sql = ' |
||
360 | INSERT INTO |
||
361 | '.$tableName.' |
||
362 | ('.implode(',', $keys).") |
||
363 | VALUES |
||
364 | (".implode(',', $values).') |
||
365 | '; |
||
366 | } else { |
||
367 | $id = null; |
||
368 | } |
||
369 | if ($sql && $id) { |
||
370 | if ($out) { |
||
371 | $out = $sql; |
||
372 | } |
||
373 | $sth = self::$connection->prepare($sql); |
||
374 | $sth->execute(); |
||
375 | } |
||
376 | |||
377 | self::close($noFkCheck); |
||
378 | |||
379 | return $id; |
||
380 | } |
||
381 | |||
382 | public static function update($id, $object, $extraFields = [], $noFkCheck = false, $manager = null, &$out = null) |
||
465 | } |
||
466 | |||
467 | public static function delete($object, $where = [], $noFkCheck = false, $manager = null, &$out = null) |
||
468 | { |
||
469 | self::init($noFkCheck, $manager); |
||
470 | if (is_object($object)) { |
||
471 | self::extract($object); |
||
472 | $tableName = self::$tableName; |
||
473 | } else { |
||
474 | $tableName = $object; |
||
475 | } |
||
476 | $whereSql = self::buildWhere($tableName, $where); |
||
477 | $sql = 'DELETE FROM '.$tableName.' '.$whereSql; |
||
478 | if ($out) { |
||
479 | $out = $sql; |
||
480 | } |
||
481 | $sth = self::$connection->prepare($sql); |
||
482 | $sth->execute(); |
||
483 | |||
484 | self::close($noFkCheck); |
||
485 | } |
||
486 | |||
487 | public static function link($object, $data, $noFkCheck = false, $manager = null, &$out = null) |
||
517 | } |
||
518 | } |
||
519 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.