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 |
||
17 | class PostgreSQLDriver extends DBDriverAbstract{ |
||
18 | |||
19 | protected $dialect = 'pgsql'; |
||
20 | protected $quotes = ['"', '"']; |
||
21 | |||
22 | /** |
||
23 | * Holds the database resource object |
||
24 | * |
||
25 | * @var resource |
||
26 | */ |
||
27 | protected $db; |
||
28 | |||
29 | /** |
||
30 | * Establishes a database connection and returns the connection object |
||
31 | * |
||
32 | * @return \chillerlan\Database\Drivers\DBDriverInterface |
||
33 | * @throws \chillerlan\Database\Drivers\DBDriverException |
||
34 | */ |
||
35 | public function connect():DBDriverInterface{ |
||
66 | |||
67 | /** |
||
68 | * Closes a database connection |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function disconnect():bool{ |
||
75 | |||
76 | /** |
||
77 | * Returns info about the used php client |
||
78 | * |
||
79 | * @return string php's database client string |
||
80 | */ |
||
81 | public function getClientInfo():string{ |
||
86 | |||
87 | /** |
||
88 | * Returns info about the database server |
||
89 | * |
||
90 | * @return string database's serverinfo string |
||
91 | */ |
||
92 | public function getServerInfo():string{ |
||
97 | |||
98 | /** |
||
99 | * @param $data |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function escape($data){ |
||
106 | |||
107 | /** |
||
108 | * @param $result |
||
109 | * @param string|null $index |
||
110 | * @param bool $assoc |
||
111 | * |
||
112 | * @return bool|\chillerlan\Database\DBResult |
||
113 | */ |
||
114 | protected function __getResult($result, string $index = null, bool $assoc = true){ |
||
150 | |||
151 | /** |
||
152 | * @param string $sql |
||
153 | * @param string|null $index |
||
154 | * @param bool $assoc |
||
155 | * |
||
156 | * @return bool|\chillerlan\Database\DBResult |
||
157 | */ |
||
158 | protected function __raw(string $sql, string $index = null, bool $assoc = true){ |
||
161 | |||
162 | /** |
||
163 | * @param string $sql |
||
164 | * @param array $values |
||
165 | * @param string|null $index |
||
166 | * @param bool $assoc |
||
167 | * |
||
168 | * @return bool|\chillerlan\Database\DBResult |
||
169 | */ |
||
170 | protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
||
175 | |||
176 | /** |
||
177 | * @param string $sql |
||
178 | * @param array $values |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | View Code Duplication | protected function __multi(string $sql, array $values){ |
|
191 | |||
192 | /** |
||
193 | * @param string $sql |
||
194 | * @param array $data |
||
195 | * @param $callback |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | View Code Duplication | protected function __multi_callback(string $sql, array $data, $callback){ |
|
208 | |||
209 | protected function replaceParams(string $sql):string{ |
||
216 | |||
217 | } |
||
218 |
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.