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 |
||
33 | class Driver extends DriverAbstract |
||
34 | { |
||
35 | /** |
||
36 | * the connection link |
||
37 | * |
||
38 | * @var \PDO |
||
39 | * @access protected |
||
40 | */ |
||
41 | protected $link; |
||
42 | |||
43 | /** |
||
44 | * Default PDO attributes |
||
45 | * |
||
46 | * @var array |
||
47 | * @access protected |
||
48 | */ |
||
49 | protected $attributes = [ |
||
50 | 'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT, |
||
51 | 'PDO::ATTR_CASE' => \PDO::CASE_NATURAL, |
||
52 | 'PDO::ATTR_ORACLE_NULLS' => \PDO::NULL_NATURAL, |
||
53 | 'PDO::ATTR_DEFAULT_FETCH_MODE' => \PDO::FETCH_ASSOC, |
||
54 | 'PDO::ATTR_EMULATE_PREPARES' => false, |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Driver constructor |
||
59 | * |
||
60 | * @param array $connectInfo |
||
61 | * @param StatementInterface $statementPrototype |
||
62 | * @throws InvalidArgumentException if link type not right |
||
63 | * @throws LogicException driver specific extension not loaded |
||
64 | * @access public |
||
65 | */ |
||
66 | public function __construct( |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | protected function extensionLoaded()/*# : bool */ |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | protected function realLastId($name) |
||
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | protected function realConnect(array $parameters) |
||
109 | |||
110 | /** |
||
111 | * Disconnect the \PDO link |
||
112 | * |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | protected function realDisconnect() |
||
118 | |||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | protected function realPing()/*# : bool */ |
||
130 | |||
131 | /** |
||
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | View Code Duplication | protected function realSetAttribute(/*# string */ $attribute, $value) |
|
144 | |||
145 | /** |
||
146 | * {@inheritDoc} |
||
147 | */ |
||
148 | View Code Duplication | protected function realGetAttribute(/*# string */ $attribute) |
|
157 | |||
158 | /** |
||
159 | * {@inheritDoc} |
||
160 | */ |
||
161 | protected function realBegin() |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | protected function realCommit() |
||
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | protected function realRollback() |
||
184 | |||
185 | /** |
||
186 | * Set default attributes |
||
187 | * |
||
188 | * @access protected |
||
189 | */ |
||
190 | protected function setDefaultAttributes() |
||
198 | |||
199 | /** |
||
200 | * Is attribute defined ? |
||
201 | * |
||
202 | * @param string $attribute |
||
203 | * @throws LogicException |
||
204 | * @access protected |
||
205 | */ |
||
206 | protected function checkAttribute(/*# string */ $attribute) |
||
215 | } |
||
216 |
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.