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:
Complex classes like PdoOci8 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PdoOci8, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class PdoOci8 extends \PDO |
||
17 | { |
||
18 | const OCI_ATTR_SESSION_MODE = 8000; |
||
19 | |||
20 | const OCI_ATTR_RETURN_LOBS = 8001; |
||
21 | |||
22 | /** |
||
23 | * @var Oci8ConnectionInterface |
||
24 | */ |
||
25 | protected $connection; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $autoCommitMode = false; |
||
31 | |||
32 | /** @var bool */ |
||
33 | private $isTransactionStarted = false; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $options = array(); |
||
39 | |||
40 | // TODO Receive Oci8ConnectionInterface? |
||
41 | 14 | public function __construct($dsn, $username = '', $password = '', $options = array()) |
|
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | 3 | protected function getAutocommitMode() |
|
94 | |||
95 | /** |
||
96 | * @param string $dsn |
||
97 | * @return string |
||
98 | */ |
||
99 | 13 | protected function getCharset($dsn) |
|
105 | |||
106 | /** |
||
107 | * @return Oci8ConnectionInterface |
||
108 | */ |
||
109 | 43 | protected function getConnection() |
|
113 | |||
114 | /** |
||
115 | * @param string $dsn |
||
116 | * @param string $username |
||
117 | * @param string $password |
||
118 | * @return Oci8ConnectionInterface |
||
119 | */ |
||
120 | 14 | protected function getOracleConnection($dsn, $username = null, $password = null) |
|
151 | |||
152 | /** |
||
153 | * @param string $dsn |
||
154 | * @return string |
||
155 | */ |
||
156 | 14 | protected function getConnectionString($dsn) |
|
166 | |||
167 | /** |
||
168 | * @param string $dsn |
||
169 | * @return array |
||
170 | */ |
||
171 | 14 | protected function getConnectionStringItems($dsn) |
|
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | 14 | protected function getDsnRegex() |
|
221 | |||
222 | /** |
||
223 | * @param array $overrideOptions |
||
224 | * @return array |
||
225 | */ |
||
226 | 38 | protected function getOptions($overrideOptions = array()) |
|
232 | |||
233 | /** |
||
234 | * @link http://php.net/manual/en/pdo.begintransaction.php |
||
235 | * @return bool |
||
236 | */ |
||
237 | 5 | public function beginTransaction() |
|
248 | |||
249 | /** |
||
250 | * @link http://php.net/manual/en/pdo.commit.php |
||
251 | * @return bool |
||
252 | */ |
||
253 | 1 | public function commit() |
|
265 | |||
266 | /** |
||
267 | * @link http://php.net/manual/en/pdo.errorcode.php |
||
268 | * @return null|string |
||
269 | */ |
||
270 | 1 | View Code Duplication | public function errorCode() |
283 | |||
284 | /** |
||
285 | * @link http://php.net/manual/en/pdo.errorinfo.php |
||
286 | * @return array |
||
287 | */ |
||
288 | 1 | public function errorInfo() |
|
307 | |||
308 | /** |
||
309 | * @param string $statement |
||
310 | * |
||
311 | * @link http://php.net/manual/en/pdo.exec.php |
||
312 | * @return bool|int |
||
313 | */ |
||
314 | 4 | public function exec($statement) |
|
325 | |||
326 | /** |
||
327 | * @param PdoOci8Statement $statement |
||
328 | * @return string |
||
329 | */ |
||
330 | 4 | protected function getStatementType($statement) |
|
341 | |||
342 | /** |
||
343 | * @param int $attribute |
||
344 | * |
||
345 | * @link http://php.net/manual/en/pdo.getattribute.php |
||
346 | * @return mixed |
||
347 | */ |
||
348 | 13 | public function getAttribute($attribute) |
|
356 | |||
357 | /** |
||
358 | * @link http://php.net/manual/en/pdo.getavailabledrivers.php |
||
359 | * @return array |
||
360 | */ |
||
361 | 1 | public static function getAvailableDrivers() |
|
365 | |||
366 | /** |
||
367 | * @link http://php.net/manual/en/pdo.intransaction.php |
||
368 | * @return bool |
||
369 | */ |
||
370 | 6 | public function inTransaction() |
|
374 | |||
375 | /** |
||
376 | * @param string $name |
||
377 | * |
||
378 | * @link http://php.net/manual/en/pdo.lastinsertid.php |
||
379 | * @return string |
||
380 | */ |
||
381 | public function lastInsertId($name = null) |
||
388 | |||
389 | /** |
||
390 | * @param string $statement |
||
391 | * @param array $driver_options |
||
392 | * |
||
393 | * @link http://php.net/manual/en/pdo.prepare.php |
||
394 | * @return bool|PdoOci8Statement |
||
395 | */ |
||
396 | 38 | public function prepare($statement, $driver_options = array()) |
|
421 | |||
422 | /** |
||
423 | * @param string $statement |
||
424 | * @param int $mode |
||
425 | * @param int|string|object $arg3 |
||
426 | * @param array $ctorargs |
||
427 | * |
||
428 | * @link http://php.net/manual/en/pdo.query.php |
||
429 | * @return bool|PdoOci8Statement |
||
430 | */ |
||
431 | 3 | public function query($statement, $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, $ctorargs = array()) |
|
455 | |||
456 | /** |
||
457 | * @param string $string |
||
458 | * @param int $parameter_type |
||
459 | * |
||
460 | * @link http://php.net/manual/en/pdo.quote.php |
||
461 | * @return bool|string |
||
462 | */ |
||
463 | 5 | public function quote($string, $parameter_type = \PDO::PARAM_STR) |
|
485 | |||
486 | 3 | protected function restoreAutocommitMode() |
|
491 | |||
492 | /** |
||
493 | * @link http://php.net/manual/en/pdo.rollback.php |
||
494 | * @return bool |
||
495 | */ |
||
496 | 3 | public function rollback() |
|
512 | |||
513 | /** |
||
514 | * @param bool $onOff |
||
515 | */ |
||
516 | 12 | protected function setAutocommitMode($onOff) |
|
520 | |||
521 | /** |
||
522 | * @param int $attribute |
||
523 | * @param mixed $value |
||
524 | * |
||
525 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
526 | * @return bool |
||
527 | */ |
||
528 | 12 | public function setAttribute($attribute, $value) |
|
548 | } |
||
549 |
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.