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 |
||
| 20 | class DataService implements SingletonInterface |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $tableName |
||
| 25 | * @param array $demand |
||
| 26 | * @return array |
||
| 27 | */ |
||
| 28 | public function getRecord(string $tableName, array $demand = []): array |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string $tableName |
||
| 45 | * @param array $demand |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function getRecords(string $tableName, array $demand = []): array |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $tableName |
||
| 63 | * @param array $demand |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function count(string $tableName, array $demand = []): int |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $tableName |
||
| 81 | * @param array $values |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | public function insert(string $tableName, array $values): int |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $tableName |
||
| 96 | * @param array $values |
||
| 97 | * @param array $identifiers |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function update(string $tableName, array $values, array $identifiers): void |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $tableName |
||
| 112 | * @param array $identifiers |
||
| 113 | */ |
||
| 114 | public function delete(string $tableName, array $identifiers): void |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $demand |
||
| 125 | * @param QueryBuilder $queryBuilder |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | protected function addDemandConstraints(array $demand, $queryBuilder): void |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return object|DeletedRestriction |
||
| 156 | */ |
||
| 157 | protected function getDeletedRestriction(): DeletedRestriction |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return object|HiddenRestriction |
||
| 164 | */ |
||
| 165 | protected function getHiddenRestriction(): HiddenRestriction |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $tableName |
||
| 172 | * @return object|Connection |
||
| 173 | */ |
||
| 174 | protected function getConnection($tableName): Connection |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $tableName |
||
| 183 | * @return object|QueryBuilder |
||
| 184 | */ |
||
| 185 | protected function getQueryBuilder($tableName): QueryBuilder |
||
| 191 | } |
||
| 192 |
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.