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 int |
||
29 | */ |
||
30 | private $autoCommitMode = false; |
||
|
|||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $options = array(); |
||
36 | |||
37 | // TODO Receive Oci8ConnectionInterface? |
||
38 | 13 | public function __construct($dsn, $username = '', $password = '', $options = array()) |
|
81 | |||
82 | 12 | protected function getCharset($dsn) |
|
88 | |||
89 | /** |
||
90 | * @return Oci8ConnectionInterface |
||
91 | */ |
||
92 | 42 | protected function getConnection() |
|
96 | |||
97 | /** |
||
98 | * @param string $dsn |
||
99 | * @param string $username |
||
100 | * @param string $password |
||
101 | * @return Oci8ConnectionInterface |
||
102 | */ |
||
103 | 13 | protected function getOracleConnection($dsn, $username = null, $password = null) |
|
134 | |||
135 | 13 | protected function getConnectionString($dsn) |
|
145 | |||
146 | 13 | protected function getConnectionStringItems($dsn) |
|
180 | |||
181 | 13 | protected function getDsnRegex() |
|
193 | |||
194 | /** |
||
195 | * @link http://php.net/manual/en/pdo.begintransaction.php |
||
196 | * @return bool |
||
197 | */ |
||
198 | 4 | public function beginTransaction() |
|
206 | |||
207 | /** |
||
208 | * @link http://php.net/manual/en/pdo.commit.php |
||
209 | * @return bool |
||
210 | */ |
||
211 | 1 | public function commit() |
|
223 | |||
224 | /** |
||
225 | * @link http://php.net/manual/en/pdo.errorcode.php |
||
226 | * @return null|string |
||
227 | */ |
||
228 | 1 | View Code Duplication | public function errorCode() |
241 | |||
242 | /** |
||
243 | * @link http://php.net/manual/en/pdo.errorinfo.php |
||
244 | * @return array |
||
245 | */ |
||
246 | 1 | public function errorInfo() |
|
265 | |||
266 | /** |
||
267 | * @param string $statement |
||
268 | * |
||
269 | * @link http://php.net/manual/en/pdo.exec.php |
||
270 | * @return bool|int |
||
271 | */ |
||
272 | 4 | public function exec($statement) |
|
283 | |||
284 | /** |
||
285 | * @param $statement |
||
286 | * @return PdoOci8Statement |
||
287 | */ |
||
288 | 4 | protected function getStatementType($statement) |
|
298 | |||
299 | /** |
||
300 | * @param int $attribute |
||
301 | * |
||
302 | * @link http://php.net/manual/en/pdo.getattribute.php |
||
303 | * @return mixed |
||
304 | */ |
||
305 | 12 | public function getAttribute($attribute) |
|
313 | |||
314 | /** |
||
315 | * @link http://php.net/manual/en/pdo.getavailabledrivers.php |
||
316 | * @return array |
||
317 | */ |
||
318 | 1 | public static function getAvailableDrivers() |
|
322 | |||
323 | /** |
||
324 | * @link http://php.net/manual/en/pdo.intransaction.php |
||
325 | * @return bool |
||
326 | */ |
||
327 | 3 | public function inTransaction() |
|
332 | |||
333 | /** |
||
334 | * @param string $name |
||
335 | * |
||
336 | * @link http://php.net/manual/en/pdo.lastinsertid.php |
||
337 | * @return string |
||
338 | */ |
||
339 | public function lastInsertId($name = null) |
||
346 | |||
347 | /** |
||
348 | * @param string $statement |
||
349 | * @param array $driver_options |
||
350 | * |
||
351 | * @link http://php.net/manual/en/pdo.prepare.php |
||
352 | * @return bool|PdoOci8Statement |
||
353 | */ |
||
354 | 38 | public function prepare($statement, $driver_options = array()) |
|
378 | |||
379 | /** |
||
380 | * @param string $statement |
||
381 | * @param int $mode |
||
382 | * @param int|string|object $arg3 |
||
383 | * @param array $ctorargs |
||
384 | * |
||
385 | * @link http://php.net/manual/en/pdo.query.php |
||
386 | * @return bool|PdoOci8Statement |
||
387 | */ |
||
388 | 3 | public function query($statement, $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, $ctorargs = array()) |
|
412 | |||
413 | /** |
||
414 | * @param string $string |
||
415 | * @param int $parameter_type |
||
416 | * |
||
417 | * @link http://php.net/manual/en/pdo.quote.php |
||
418 | * @return bool|string |
||
419 | */ |
||
420 | 5 | public function quote($string, $parameter_type = \PDO::PARAM_STR) |
|
442 | |||
443 | /** |
||
444 | * @link http://php.net/manual/en/pdo.rollback.php |
||
445 | * @return bool |
||
446 | */ |
||
447 | 2 | public function rollback() |
|
467 | |||
468 | /** |
||
469 | * @param int $attribute |
||
470 | * @param mixed $value |
||
471 | * |
||
472 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
473 | * @return bool |
||
474 | */ |
||
475 | 11 | public function setAttribute($attribute, $value) |
|
495 | } |
||
496 |
This check marks private properties in classes that are never used. Those properties can be removed.