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:
Complex classes like PdoClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PdoClient, and based on these observations, apply Extract Interface, too.
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 | 5 | 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 | 7 | 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 | 10 | private function execute(string $sql, array $filterList = [], array $dataList = []): PDOStatement |
|
129 | |||
130 | 5 | private function fetchData(PDOStatement $stmt, string $classFqcn = null): array |
|
138 | |||
139 | 5 | private function createSqlFilter(array $filterByColumnNames = []): string |
|
153 | |||
154 | 4 | private function createColumnFilter(string $columnName, $value, string $parameterName = null) |
|
172 | |||
173 | 3 | private function createSqlOrderBy(array $orderBy = []): string |
|
186 | |||
187 | 1 | private function createInsertSql(string $tableName, array $dataList): string |
|
195 | |||
196 | 1 | private function createUpdateSql(string $tableName, array $parameterList): string |
|
206 | |||
207 | /** |
||
208 | * @param mixed $value |
||
209 | * |
||
210 | * @throws TypeResolutionMicroOrmException |
||
211 | */ |
||
212 | 8 | private function resolvePdoType($value): int |
|
238 | |||
239 | /** |
||
240 | * @throws BindingMicroOrmException |
||
241 | */ |
||
242 | 9 | private function bindParameterList(PDOStatement $stmt, array $parameterList, string $postfix = '') |
|
255 | |||
256 | /** |
||
257 | * @throws BindingMicroOrmException |
||
258 | */ |
||
259 | 8 | private function bindParameter(PDOStatement $stmt, string $name, $value, string $postfix = '') |
|
274 | } |
||
275 |
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.