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 |
||
10 | class BulkReplace extends Base |
||
11 | { |
||
12 | use BulkSqlTrait; |
||
13 | |||
14 | private $primaryKey = null; |
||
15 | |||
16 | private $uniqueKey = null; |
||
17 | |||
18 | private $keysChecked = false; |
||
19 | |||
20 | const QUERY_TEMPLATE = 'REPLACE INTO %s (%s) VALUES %s'; |
||
21 | |||
22 | View Code Duplication | protected function buildQuery(): string |
|
52 | |||
53 | View Code Duplication | protected function bindValues(PDOStatement $statement) |
|
68 | |||
69 | public function getReplacedCount() |
||
74 | |||
75 | public function getInsertedCount() |
||
79 | |||
80 | /** |
||
81 | * To replace work proper, need to check if PRIMARY KEY or UNIQUE KEY is in the beginning of array |
||
82 | * |
||
83 | * @throws LogicException |
||
84 | */ |
||
85 | private function checkIsKeysOnTheBeginning() |
||
101 | |||
102 | /** |
||
103 | * @param array $keys |
||
104 | * |
||
105 | * @return bool True if on the beginning, False if not |
||
106 | */ |
||
107 | private function checkKeys(array $keys) : bool |
||
119 | |||
120 | /** |
||
121 | * Return primary key of the table |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | private function getPrimary() : array |
||
129 | |||
130 | /** |
||
131 | * Return primary key of the table |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | private function getUnique() : array |
||
139 | |||
140 | /** |
||
141 | * Fetch primary key of the table |
||
142 | * |
||
143 | * @return array|string |
||
144 | * @throws LogicException |
||
145 | */ |
||
146 | private function fetchPrimary() |
||
160 | |||
161 | /** |
||
162 | * Fetch unique key of the table |
||
163 | * |
||
164 | * @return array|string |
||
165 | * @throws LogicException |
||
166 | */ |
||
167 | private function fetchUnique() |
||
179 | } |
||
180 |
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.