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) |
||
263 | |||
264 | /** |
||
265 | * Return an attribute |
||
266 | * |
||
267 | * @param int $attr attribute |
||
268 | * |
||
269 | * @return mixed attribute value |
||
270 | */ |
||
271 | public function getAttribute($attr) |
||
281 | |||
282 | /** |
||
283 | * Return the auto commit flag |
||
284 | * |
||
285 | * @return boolean auto commit flag |
||
286 | */ |
||
287 | public function getAutoCommit() |
||
291 | |||
292 | /** |
||
293 | * Commit connection |
||
294 | * |
||
295 | * @return bool if commit was executed |
||
296 | */ |
||
297 | public function commit() : bool |
||
304 | |||
305 | /** |
||
306 | * Rollback connection |
||
307 | * |
||
308 | * @return bool if rollback was executed |
||
309 | */ |
||
310 | public function rollBack() : bool |
||
317 | |||
318 | /** |
||
319 | * Start a transaction, setting auto commit to off |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function beginTransaction() : bool |
||
329 | |||
330 | /** |
||
331 | * @param string $query |
||
332 | * @param integer $limit |
||
333 | * @param null|integer $offset |
||
334 | * @return string |
||
335 | */ |
||
336 | public function setLimit(string $query, int $limit=0, int $offset=0) : string |
||
354 | |||
355 | /** |
||
356 | * Prepare a statement |
||
357 | * |
||
358 | * @param string $query for statement |
||
359 | * @param mixed $options for driver |
||
360 | * |
||
361 | * @return OciPdoStubStatement |
||
362 | * @throws PDOException |
||
363 | */ |
||
364 | public function prepare($query, $options = null) |
||
373 | |||
374 | /** |
||
375 | * Sets the last error found |
||
376 | * |
||
377 | * @param resource $obj optional object to extract error from |
||
378 | * |
||
379 | * @return null |
||
380 | */ |
||
381 | public function setError($obj = null) |
||
395 | |||
396 | /** |
||
397 | * Returns the last error found |
||
398 | * |
||
399 | * @return int error code |
||
400 | */ |
||
401 | public function errorCode() |
||
408 | |||
409 | /** |
||
410 | * Returns the last error info |
||
411 | * |
||
412 | * @return array error info |
||
413 | */ |
||
414 | public function errorInfo() |
||
425 | |||
426 | /** |
||
427 | * Close connection |
||
428 | * |
||
429 | * @return null |
||
430 | */ |
||
431 | public function close() |
||
441 | |||
442 | /** |
||
443 | * Trigger stupid errors who should be exceptions |
||
444 | * |
||
445 | * @param int $errno error number |
||
446 | * @param string $errstr error message |
||
447 | * @param mixed $errfile error file |
||
448 | * @param int $errline error line |
||
449 | * |
||
450 | * @return null |
||
451 | */ |
||
452 | public function errorHandler($errno, $errstr, $errfile, $errline) |
||
464 | |||
465 | /** |
||
466 | * Return available drivers |
||
467 | * Will insert the OCI driver on the list, if not exist |
||
468 | * |
||
469 | * @return array with drivers |
||
470 | */ |
||
471 | public static function getAvailableDrivers() |
||
479 | |||
480 | /** |
||
481 | * Return if is on a transaction |
||
482 | * |
||
483 | * @return boolean on a transaction |
||
484 | */ |
||
485 | public function inTransaction() |
||
489 | |||
490 | /** |
||
491 | * Quote a string |
||
492 | * |
||
493 | * @param string $string to be quoted |
||
494 | * @param int $type parameter type |
||
495 | * |
||
496 | * @return string quoted |
||
497 | */ |
||
498 | public function quote($string, $type = null) |
||
504 | |||
505 | /** |
||
506 | * Return the last inserted id |
||
507 | * If the sequence name is not sent, throws an exception |
||
508 | * |
||
509 | * @param string $sequence name |
||
510 | * |
||
511 | * @return integer last id |
||
512 | * @throws PDOException |
||
513 | */ |
||
514 | public function lastInsertId($sequence = null) |
||
528 | |||
529 | /** |
||
530 | * @param string $table |
||
531 | * @param string $column |
||
532 | * @return string |
||
533 | */ |
||
534 | public function getFieldComment(string $table, string $column) : string |
||
538 | } |
||
539 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.