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 Connection |
||
11 | { |
||
12 | /** |
||
13 | * @var ConnectionInterface |
||
14 | */ |
||
15 | private $conn; |
||
16 | |||
17 | /** |
||
18 | * @var Escaper |
||
19 | */ |
||
20 | private $escaper; |
||
21 | |||
22 | /** |
||
23 | * @var LoggerInterface|null |
||
24 | */ |
||
25 | private $logger; |
||
26 | |||
27 | public function __construct(ConnectionInterface $conn) |
||
31 | |||
32 | /** |
||
33 | * @param LoggerInterface|null $logger |
||
34 | */ |
||
35 | public function setLogger(LoggerInterface $logger = null) |
||
39 | |||
40 | /** |
||
41 | * @return LoggerInterface|null |
||
42 | */ |
||
43 | public function getLogger() |
||
47 | |||
48 | 3 | public function executeUpdate($query, array $params = array()) |
|
62 | |||
63 | View Code Duplication | public function executeQuery($query, array $params = array()) |
|
78 | |||
79 | 3 | View Code Duplication | public function executeMultiQuery($query, array $params = array(), array $types = array(), array $resultSetNames = array()) |
94 | |||
95 | public function quote($value) |
||
99 | |||
100 | 3 | public function getEscaper() |
|
108 | |||
109 | public function checkConnection() |
||
113 | |||
114 | /** |
||
115 | * @return QueryBuilder |
||
116 | */ |
||
117 | public function createQueryBuilder() |
||
121 | |||
122 | 3 | protected function prepareQuery($query, $params) |
|
126 | } |
||
127 |
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.