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