Complex classes like OciStubPdo 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 OciStubPdo, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 47 | class OciStubPdo extends AbstractPdo implements DriverInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var resource |
||
| 51 | */ |
||
| 52 | private $_con = null; |
||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | private $_autocommit = true; |
||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $_last_error = null; |
||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $_charset = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * OciStubPdo constructor. |
||
| 68 | * @param string $dsn |
||
| 69 | * @param string $username |
||
| 70 | * @param string $password |
||
| 71 | * @param array $options |
||
| 72 | * @param LoggerInterface|null $logger |
||
| 73 | * @param CacheInterface|null $cache |
||
| 74 | */ |
||
| 75 | public function __construct(string $dsn, string $username='', string $password='', array $options=[], LoggerInterface $logger=null, CacheInterface $cache=null) |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @param bool $include_views |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getTables(bool $include_views=false) : array |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param bool $include_views |
||
| 122 | * @param string $table |
||
| 123 | * @return Column[] |
||
| 124 | */ |
||
| 125 | public function getColumns(bool $include_views=false, string $table='') : array |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $table |
||
| 132 | * @return ForeignKey[] |
||
| 133 | */ |
||
| 134 | public function getForeignKeys(string $table='') : array |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param bool|false $include_views |
||
| 141 | * @param string $table |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getTableCounts(bool $include_views=false, string $table='') : array |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return the charset |
||
| 151 | * |
||
| 152 | * @return string charset |
||
| 153 | */ |
||
| 154 | public function getCharset() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Find the charset |
||
| 161 | * |
||
| 162 | * @param array $charset charset |
||
| 163 | * |
||
| 164 | * @return string charset |
||
| 165 | */ |
||
| 166 | private function _getCharset(array $charset = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the connection |
||
| 191 | * |
||
| 192 | * @return resource handle |
||
| 193 | */ |
||
| 194 | public function getConnection() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Execute a query |
||
| 201 | * |
||
| 202 | * @param string $statement sql query |
||
| 203 | * @param int $mode PDO query() mode |
||
| 204 | * @param int $p1 PDO query() first parameter |
||
| 205 | * @param int $p2 PDO query() second parameter |
||
| 206 | * |
||
| 207 | * @return OciPdoStubStatement |
||
| 208 | * @throws PDOException |
||
| 209 | */ |
||
| 210 | public function query($statement, $mode = null, $p1 = null, $p2 = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Execute query |
||
| 226 | * |
||
| 227 | * @param string $sql query |
||
| 228 | * |
||
| 229 | * @return integer of affected rows |
||
| 230 | * @throws PDOException |
||
| 231 | */ |
||
| 232 | public function exec($sql) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set an attribute |
||
| 246 | * |
||
| 247 | * @param int $attr attribute |
||
| 248 | * @param mixed $value value |
||
| 249 | * |
||
| 250 | * @return boolean|null if set was ok |
||
| 251 | */ |
||
| 252 | public function setAttribute($attr, $value) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return an attribute |
||
| 263 | * |
||
| 264 | * @param int $attr attribute |
||
| 265 | * |
||
| 266 | * @return mixed attribute value |
||
| 267 | */ |
||
| 268 | public function getAttribute($attr) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the auto commit flag |
||
| 281 | * |
||
| 282 | * @return boolean auto commit flag |
||
| 283 | */ |
||
| 284 | public function getAutoCommit() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Commit connection |
||
| 291 | * |
||
| 292 | * @return bool if commit was executed |
||
| 293 | */ |
||
| 294 | public function commit() : bool |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Rollback connection |
||
| 304 | * |
||
| 305 | * @return bool if rollback was executed |
||
| 306 | */ |
||
| 307 | public function rollBack() : bool |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Start a transaction, setting auto commit to off |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function beginTransaction() : bool |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $query |
||
| 329 | * @param integer $limit |
||
| 330 | * @param null|integer $offset |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function setLimit(string $query, int $limit=0, int $offset=0) : string |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Prepare a statement |
||
| 354 | * |
||
| 355 | * @param string $query for statement |
||
| 356 | * @param mixed $options for driver |
||
| 357 | * |
||
| 358 | * @return OciPdoStubStatement |
||
| 359 | * @throws PDOException |
||
| 360 | */ |
||
| 361 | public function prepare($query, $options = null) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Sets the last error found |
||
| 373 | * |
||
| 374 | * @param resource $obj optional object to extract error from |
||
| 375 | * |
||
| 376 | * @return null |
||
| 377 | */ |
||
| 378 | public function setError($obj = null) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns the last error found |
||
| 393 | * |
||
| 394 | * @return int error code |
||
| 395 | */ |
||
| 396 | public function errorCode() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Returns the last error info |
||
| 406 | * |
||
| 407 | * @return array error info |
||
| 408 | */ |
||
| 409 | public function errorInfo() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Close connection |
||
| 423 | * |
||
| 424 | * @return null |
||
| 425 | */ |
||
| 426 | public function close() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Trigger stupid errors who should be exceptions |
||
| 437 | * |
||
| 438 | * @param int $errno error number |
||
| 439 | * @param string $errstr error message |
||
| 440 | * @param mixed $errfile error file |
||
| 441 | * @param int $errline error line |
||
| 442 | * |
||
| 443 | * @return null |
||
| 444 | */ |
||
| 445 | public function errorHandler($errno, $errstr, $errfile, $errline) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Return available drivers |
||
| 458 | * Will insert the OCI driver on the list, if not exist |
||
| 459 | * |
||
| 460 | * @return array with drivers |
||
| 461 | */ |
||
| 462 | public static function getAvailableDrivers() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Return if is on a transaction |
||
| 473 | * |
||
| 474 | * @return boolean on a transaction |
||
| 475 | */ |
||
| 476 | public function inTransaction() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Quote a string |
||
| 483 | * |
||
| 484 | * @param string $string to be quoted |
||
| 485 | * @param int $type parameter type |
||
| 486 | * |
||
| 487 | * @return string quoted |
||
| 488 | */ |
||
| 489 | public function quote($string, $type = null) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Return the last inserted id |
||
| 498 | * If the sequence name is not sent, throws an exception |
||
| 499 | * |
||
| 500 | * @param string $sequence name |
||
| 501 | * |
||
| 502 | * @return integer last id |
||
| 503 | * @throws PDOException |
||
| 504 | */ |
||
| 505 | public function lastInsertId($sequence = null) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $table |
||
| 522 | * @param string $column |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public function getFieldComment(string $table, string $column) : string |
||
| 529 | } |
||
| 530 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.