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 |
||
11 | class PdoClient implements ClientInterface |
||
12 | { |
||
13 | const POSTFIX_FILTER = '_filter'; |
||
14 | |||
15 | /** @var PDO */ |
||
16 | private $pdo; |
||
17 | |||
18 | 13 | public function __construct(PDO $pdo) |
|
22 | |||
23 | /** |
||
24 | * @throws BindingMicroOrmException |
||
25 | * @throws ExecutionMicroOrmException |
||
26 | */ |
||
27 | 3 | public function select(string $table, array $filter = [], array $orderBy = [], string $classFqcn = ''): array |
|
37 | |||
38 | /** |
||
39 | * @param string $table |
||
40 | * @param array $data [$columnName => $value, ...] |
||
41 | * |
||
42 | * @return int The nbr of affected rows |
||
43 | */ |
||
44 | 1 | public function insert(string $table, array $data): int |
|
50 | |||
51 | /** |
||
52 | * @param string $table |
||
53 | * @param array $data [$columnName => $value, ...] |
||
54 | * |
||
55 | * @return int The nbr of affected rows |
||
56 | */ |
||
57 | 1 | View Code Duplication | public function update(string $table, array $filter = [], array $data = []): int |
65 | |||
66 | /** |
||
67 | * @return int The nbr of affected rows |
||
68 | */ |
||
69 | 1 | View Code Duplication | public function delete(string $table, array $filter = []): int |
77 | |||
78 | /** |
||
79 | * @throws BindingMicroOrmException |
||
80 | * @throws ExecutionMicroOrmException |
||
81 | * |
||
82 | * @return array The result list |
||
83 | */ |
||
84 | 2 | public function executeQuery(string $sql, array $filterList = [], string $classFqcn = null): array |
|
88 | |||
89 | /** |
||
90 | * @throws BindingMicroOrmException |
||
91 | * @throws ExecutionMicroOrmException |
||
92 | * |
||
93 | * @return int The nbr of affected rows |
||
94 | */ |
||
95 | 4 | public function executeCommand(string $sql, array $filterList = [], array $dataList = []): int |
|
99 | |||
100 | 1 | public function getLastInsertId(string $idName = null): string |
|
104 | |||
105 | /** |
||
106 | * @throws BindingMicroOrmException |
||
107 | * @throws ExecutionMicroOrmException |
||
108 | * |
||
109 | * @return PDOStatement |
||
110 | */ |
||
111 | 4 | private function execute(string $sql, array $filterList = [], array $dataList = []): PDOStatement |
|
129 | |||
130 | 2 | private function fetchData(PDOStatement $stmt, string $classFqcn = null): array |
|
138 | |||
139 | 5 | private function createSqlFilter(array $filterByColumnNames = []): string |
|
157 | |||
158 | 3 | private function createSqlOrderBy(array $orderBy = []): string |
|
171 | |||
172 | 1 | private function createInsertSql(string $tableName, array $dataList): string |
|
180 | |||
181 | 1 | private function createUpdateSql(string $tableName, array $parameterList): string |
|
191 | |||
192 | /** |
||
193 | * @param mixed $value |
||
194 | * |
||
195 | * @throws TypeResolutionMicroOrmException |
||
196 | */ |
||
197 | 2 | private function resolvePdoType($value): int |
|
224 | |||
225 | /** |
||
226 | * @throws BindingMicroOrmException |
||
227 | */ |
||
228 | 8 | private function bindParameterList(PDOStatement $stmt, array $parameterList, string $postfix = '') |
|
246 | } |
||
247 |
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.