Complex classes like Oci8Connection 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 Oci8Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Oci8Connection extends Connection |
||
| 23 | { |
||
| 24 | const RECONNECT_ERRORS = 'reconnect_errors'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $schema; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \Yajra\Oci8\Schema\Sequence |
||
| 33 | */ |
||
| 34 | protected $sequence; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Yajra\Oci8\Schema\Trigger |
||
| 38 | */ |
||
| 39 | protected $trigger; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param PDO|\Closure $pdo |
||
| 43 | * @param string $database |
||
| 44 | * @param string $tablePrefix |
||
| 45 | * @param array $config |
||
| 46 | */ |
||
| 47 | public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get current schema. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getSchema() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set current schema. |
||
| 66 | * |
||
| 67 | * @param string $schema |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function setSchema($schema) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Update oracle session variables. |
||
| 82 | * |
||
| 83 | * @param array $sessionVars |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function setSessionVars(array $sessionVars) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get sequence class. |
||
| 105 | * |
||
| 106 | * @return \Yajra\Oci8\Schema\Sequence |
||
| 107 | */ |
||
| 108 | public function getSequence() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set sequence class. |
||
| 115 | * |
||
| 116 | * @param \Yajra\Oci8\Schema\Sequence $sequence |
||
| 117 | * @return \Yajra\Oci8\Schema\Sequence |
||
| 118 | */ |
||
| 119 | public function setSequence(Sequence $sequence) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get oracle trigger class. |
||
| 126 | * |
||
| 127 | * @return \Yajra\Oci8\Schema\Trigger |
||
| 128 | */ |
||
| 129 | public function getTrigger() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set oracle trigger class. |
||
| 136 | * |
||
| 137 | * @param \Yajra\Oci8\Schema\Trigger $trigger |
||
| 138 | * @return \Yajra\Oci8\Schema\Trigger |
||
| 139 | */ |
||
| 140 | public function setTrigger(Trigger $trigger) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get a schema builder instance for the connection. |
||
| 147 | * |
||
| 148 | * @return \Yajra\Oci8\Schema\OracleBuilder |
||
| 149 | */ |
||
| 150 | public function getSchemaBuilder() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Begin a fluent query against a database table. |
||
| 161 | * |
||
| 162 | * @param string $table |
||
| 163 | * @return \Yajra\Oci8\Query\OracleBuilder |
||
| 164 | */ |
||
| 165 | public function table($table) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Set oracle session date format. |
||
| 176 | * |
||
| 177 | * @param string $format |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS') |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get doctrine connection. |
||
| 192 | * |
||
| 193 | * @return \Doctrine\DBAL\Connection |
||
| 194 | */ |
||
| 195 | public function getDoctrineConnection() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get doctrine driver. |
||
| 210 | * |
||
| 211 | * @return \Doctrine\DBAL\Driver\OCI8\Driver |
||
| 212 | */ |
||
| 213 | protected function getDoctrineDriver() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Execute a PL/SQL Function and return its value. |
||
| 220 | * Usage: DB::executeFunction('function_name(:binding_1,:binding_n)', [':binding_1' => 'hi', ':binding_n' => |
||
| 221 | * 'bye'], PDO::PARAM_LOB). |
||
| 222 | * |
||
| 223 | * @param string $functionName |
||
| 224 | * @param array $bindings (kvp array) |
||
| 225 | * @param int $returnType (PDO::PARAM_*) |
||
| 226 | * @param int $length |
||
| 227 | * @return mixed $returnType |
||
| 228 | */ |
||
| 229 | public function executeFunction($functionName, array $bindings = [], $returnType = PDO::PARAM_STR, $length = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Execute a PL/SQL Procedure and return its results. |
||
| 243 | * |
||
| 244 | * Usage: DB::executeProcedure($procedureName, $bindings). |
||
| 245 | * $bindings looks like: |
||
| 246 | * $bindings = [ |
||
| 247 | * 'p_userid' => $id |
||
| 248 | * ]; |
||
| 249 | * |
||
| 250 | * @param string $procedureName |
||
| 251 | * @param array $bindings |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function executeProcedure($procedureName, array $bindings = []) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Execute a PL/SQL Procedure and return its cursor result. |
||
| 265 | * Usage: DB::executeProcedureWithCursor($procedureName, $bindings). |
||
| 266 | * |
||
| 267 | * https://docs.oracle.com/cd/E17781_01/appdev.112/e18555/ch_six_ref_cur.htm#TDPPH218 |
||
| 268 | * |
||
| 269 | * @param string $procedureName |
||
| 270 | * @param array $bindings |
||
| 271 | * @param string $cursorName |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function executeProcedureWithCursor($procedureName, array $bindings = [], $cursorName = ':cursor') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Creates sql command to run a procedure with bindings. |
||
| 294 | * |
||
| 295 | * @param string $procedureName |
||
| 296 | * @param array $bindings |
||
| 297 | * @param string|bool $cursor |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function createSqlFromProcedure($procedureName, array $bindings, $cursor = false) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Creates statement from procedure. |
||
| 314 | * |
||
| 315 | * @param string $procedureName |
||
| 316 | * @param array $bindings |
||
| 317 | * @param string|bool $cursorName |
||
| 318 | * @return PDOStatement |
||
| 319 | */ |
||
| 320 | public function createStatementFromProcedure($procedureName, array $bindings, $cursorName = false) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Create statement from function. |
||
| 329 | * |
||
| 330 | * @param string $functionName |
||
| 331 | * @param array $bindings |
||
| 332 | * @return PDOStatement |
||
| 333 | */ |
||
| 334 | public function createStatementFromFunction($functionName, array $bindings) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Bind values to their parameters in the given statement. |
||
| 345 | * |
||
| 346 | * @param PDOStatement $statement |
||
| 347 | * @param array $bindings |
||
| 348 | */ |
||
| 349 | public function bindValues($statement, $bindings) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the default query grammar instance. |
||
| 358 | * |
||
| 359 | * @return \Illuminate\Database\Grammar|\Yajra\Oci8\Query\Grammars\OracleGrammar |
||
| 360 | */ |
||
| 361 | protected function getDefaultQueryGrammar() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set the table prefix and return the grammar. |
||
| 368 | * |
||
| 369 | * @param \Illuminate\Database\Grammar|\Yajra\Oci8\Query\Grammars\OracleGrammar|\Yajra\Oci8\Schema\Grammars\OracleGrammar $grammar |
||
| 370 | * @return \Illuminate\Database\Grammar |
||
| 371 | */ |
||
| 372 | public function withTablePrefix(Grammar $grammar) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Set the schema prefix and return the grammar. |
||
| 379 | * |
||
| 380 | * @param \Illuminate\Database\Grammar|\Yajra\Oci8\Query\Grammars\OracleGrammar|\Yajra\Oci8\Schema\Grammars\OracleGrammar $grammar |
||
| 381 | * @return \Illuminate\Database\Grammar |
||
| 382 | */ |
||
| 383 | public function withSchemaPrefix(Grammar $grammar) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get config schema prefix. |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function getConfigSchemaPrefix() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the default schema grammar instance. |
||
| 402 | * |
||
| 403 | * @return \Illuminate\Database\Grammar|\Yajra\Oci8\Schema\Grammars\OracleGrammar |
||
| 404 | */ |
||
| 405 | protected function getDefaultSchemaGrammar() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the default post processor instance. |
||
| 412 | * |
||
| 413 | * @return \Yajra\Oci8\Query\Processors\OracleProcessor |
||
| 414 | */ |
||
| 415 | protected function getDefaultPostProcessor() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add bindings to statement. |
||
| 422 | * |
||
| 423 | * @param array $bindings |
||
| 424 | * @param PDOStatement $stmt |
||
| 425 | * @return PDOStatement |
||
| 426 | */ |
||
| 427 | public function addBindingsToStatement(PDOStatement $stmt, array $bindings) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Determine if the given exception was caused by a lost connection. |
||
| 451 | * |
||
| 452 | * @param \Exception $e |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public function causedByLostConnection(Exception $e) |
||
| 486 | } |
||
| 487 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.