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 |
||
17 | class PdoUniqueKeyExtractor extends UniqueKeyExtractorAbstract |
||
18 | { |
||
19 | use PdoExtractorTrait; |
||
20 | |||
21 | /** |
||
22 | * generic extraction from tables with unique (composite) key |
||
23 | * |
||
24 | * @param \PDO $pdo |
||
25 | * @param string $extractQuery |
||
26 | * @param array|string $uniqueKey can be either a unique key name as |
||
27 | * string |
||
28 | * '(table.)compositeKeyName' // ('id' by default) |
||
29 | * |
||
30 | * or an array : |
||
31 | * ['(table.)compositeKey1'] // single unique key |
||
32 | * ['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key |
||
33 | * |
||
34 | * or an associative array in case you are using aliases : |
||
35 | * [ |
||
36 | * '(table.)compositeKey1' => 'aliasNameAsInRecord', |
||
37 | * ] |
||
38 | * |
||
39 | * and : |
||
40 | * [ |
||
41 | * '(table.)compositeKey1' => 'aliasNameAsInRecord1', |
||
42 | * '(table.)compositeKey2' => 'aliasNameAsInRecord2', |
||
43 | * // ... |
||
44 | * ] |
||
45 | */ |
||
46 | public function __construct(\PDO $pdo, $extractQuery = null, $uniqueKey = 'id') |
||
51 | |||
52 | /** |
||
53 | * leave no trace |
||
54 | * implement here to allow easier overidding |
||
55 | */ |
||
56 | public function __destruct() |
||
63 | |||
64 | /** |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function fetchRecords() |
||
94 | |||
95 | /** |
||
96 | * This method sets offset and limit in the query |
||
97 | * WARNING : if you set an offset without limit, |
||
98 | * the limit will be set to $this->maxdefaultLimit |
||
99 | * |
||
100 | * @return string the paginated query with current offset and limit |
||
101 | */ |
||
102 | protected function getPaginatedQuery() |
||
114 | } |
||
115 |