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 |
||
12 | final class BlockingConnector implements Connector |
||
13 | { |
||
14 | /** |
||
15 | * @var ExtendedPdo |
||
16 | */ |
||
17 | private $connection; |
||
18 | |||
19 | /** |
||
20 | * @inheritdoc |
||
21 | * |
||
22 | * @param array $config |
||
23 | * |
||
24 | * @return PromiseInterface |
||
25 | * |
||
26 | * @throws InvalidArgumentException |
||
27 | */ |
||
28 | 1 | public function connect(array $config) |
|
36 | |||
37 | /** |
||
38 | * Returns a new dsn string, for PDO to connect to the database with. |
||
39 | * |
||
40 | * @param array $config |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | 1 | private function newConnectionString(array $config) |
|
62 | |||
63 | /** |
||
64 | * @param array $config |
||
65 | * |
||
66 | * @return array |
||
67 | * |
||
68 | * @throws InvalidArgumentException |
||
69 | */ |
||
70 | 1 | private function validate(array $config) |
|
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | * |
||
87 | * @param string $query |
||
88 | * @param array $values |
||
89 | * |
||
90 | * @return PromiseInterface |
||
91 | * |
||
92 | * @throws InvalidArgumentException |
||
93 | */ |
||
94 | 1 | public function query($query, $values) |
|
100 | } |
||
101 |
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.