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 |
||
8 | class Pdo extends PdoConfiguration |
||
9 | { |
||
10 | const CHARSET_UTF8 = 'utf8'; |
||
11 | |||
12 | /** |
||
13 | * Charset to use |
||
14 | * @return string |
||
15 | */ |
||
16 | 2 | protected function getCharset() |
|
21 | |||
22 | /** |
||
23 | * @param \Pdo $instance |
||
24 | * @return $this |
||
25 | */ |
||
26 | 1 | protected function defaultConfiguration(\Pdo $instance) |
|
35 | |||
36 | /** |
||
37 | * Fetch all rows for a given statement |
||
38 | * @param object $statement |
||
39 | * @return array |
||
40 | * @throws Exception |
||
41 | */ |
||
42 | 3 | View Code Duplication | public function fetchAll($statement) |
56 | |||
57 | /** |
||
58 | * Begin a transaction |
||
59 | * @return bool |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | 3 | public function beginTransaction() |
|
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | * @throws Exception |
||
80 | */ |
||
81 | 2 | View Code Duplication | public function rollback() |
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | * @throws Exception |
||
95 | */ |
||
96 | 2 | public function errorCode() |
|
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | * @throws Exception |
||
109 | */ |
||
110 | 2 | public function errorInfo() |
|
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | * @throws Exception |
||
123 | */ |
||
124 | 2 | View Code Duplication | public function commit() |
134 | |||
135 | /** |
||
136 | * Execute a statement for given values |
||
137 | * @param object $statement |
||
138 | * @param array $values |
||
139 | * @return bool |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | 3 | public function execute($statement, $values = array()) |
|
157 | |||
158 | /** |
||
159 | * Prepare a SQL string to a statement |
||
160 | * @param string $sql |
||
161 | * @param array $options |
||
162 | * @return \PDOStatement |
||
163 | * @throws Exception |
||
164 | */ |
||
165 | 3 | public function prepare($sql, array $options = array()) |
|
175 | |||
176 | /** |
||
177 | * Retrieve id inserted |
||
178 | * @param string $name optional |
||
179 | * @return string |
||
180 | * @throws Exception |
||
181 | */ |
||
182 | 2 | View Code Duplication | public function lastInsertId($name = null) |
191 | |||
192 | /** |
||
193 | * Retrieve number of affected rows of a statement |
||
194 | * @param mixed $statement |
||
195 | * @return int |
||
196 | * @throws Exception |
||
197 | */ |
||
198 | View Code Duplication | public function count($statement) |
|
212 | } |
||
213 |
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.