Total Complexity | 75 |
Total Lines | 549 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TDbConnection 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.
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 TDbConnection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
89 | class TDbConnection extends \Prado\TComponent |
||
90 | { |
||
91 | /** |
||
92 | * |
||
93 | * @since 3.1.7 |
||
94 | */ |
||
95 | const DEFAULT_TRANSACTION_CLASS = 'System.Data.TDbTransaction'; |
||
96 | |||
97 | private $_dsn = ''; |
||
98 | private $_username = ''; |
||
99 | private $_password = ''; |
||
100 | private $_charset = ''; |
||
101 | private $_attributes = []; |
||
102 | private $_active = false; |
||
103 | private $_pdo; |
||
104 | private $_transaction; |
||
105 | |||
106 | /** |
||
107 | * @var TDbMetaData |
||
108 | */ |
||
109 | private $_dbMeta; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | * @since 3.1.7 |
||
114 | */ |
||
115 | private $_transactionClass = self::DEFAULT_TRANSACTION_CLASS; |
||
116 | |||
117 | /** |
||
118 | * Constructor. |
||
119 | * Note, the DB connection is not established when this connection |
||
120 | * instance is created. Set {@link setActive Active} property to true |
||
121 | * to establish the connection. |
||
122 | * Since 3.1.2, you can set the charset for MySql connection |
||
123 | * |
||
124 | * @param string The Data Source Name, or DSN, contains the information required to connect to the database. |
||
125 | * @param string The user name for the DSN string. |
||
126 | * @param string The password for the DSN string. |
||
127 | * @param string Charset used for DB Connection (MySql & pgsql only). If not set, will use the default charset of your database server |
||
|
|||
128 | * @see http://www.php.net/manual/en/function.PDO-construct.php |
||
129 | */ |
||
130 | public function __construct($dsn = '', $username = '', $password = '', $charset = '') |
||
131 | { |
||
132 | $this->_dsn = $dsn; |
||
133 | $this->_username = $username; |
||
134 | $this->_password = $password; |
||
135 | $this->_charset = $charset; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Close the connection when serializing. |
||
140 | */ |
||
141 | public function __sleep() |
||
142 | { |
||
143 | // $this->close(); - DO NOT CLOSE the current connection as serializing doesn't neccessarily mean we don't this connection anymore in the current session |
||
144 | return array_diff(parent::__sleep(), ["\0Prado\Data\TDbConnection\0_pdo","\0Prado\Data\TDbConnection\0_active"]); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return array list of available PDO drivers |
||
149 | * @see http://www.php.net/manual/en/function.PDO-getAvailableDrivers.php |
||
150 | */ |
||
151 | public static function getAvailableDrivers() |
||
152 | { |
||
153 | return PDO::getAvailableDrivers(); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return boolean whether the DB connection is established |
||
158 | */ |
||
159 | public function getActive() |
||
160 | { |
||
161 | return $this->_active; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Open or close the DB connection. |
||
166 | * @param boolean whether to open or close DB connection |
||
167 | * @throws TDbException if connection fails |
||
168 | */ |
||
169 | public function setActive($value) |
||
170 | { |
||
171 | $value = TPropertyValue::ensureBoolean($value); |
||
172 | if($value !== $this->_active) |
||
173 | { |
||
174 | if($value) |
||
175 | $this->open(); |
||
176 | else |
||
177 | $this->close(); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Opens DB connection if it is currently not |
||
183 | * @throws TDbException if connection fails |
||
184 | */ |
||
185 | protected function open() |
||
186 | { |
||
187 | if($this->_pdo === null) |
||
188 | { |
||
189 | try |
||
190 | { |
||
191 | $this->_pdo = new PDO($this->getConnectionString(),$this->getUsername(), |
||
192 | $this->getPassword(), $this->_attributes); |
||
193 | // This attribute is only useful for PDO::MySql driver. |
||
194 | // Ignore the warning if a driver doesn't understand this. |
||
195 | @$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
196 | $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
197 | $this->_active = true; |
||
198 | $this->setConnectionCharset(); |
||
199 | } |
||
200 | catch(PDOException $e) |
||
201 | { |
||
202 | throw new TDbException('dbconnection_open_failed', $e->getMessage()); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Closes the currently active DB connection. |
||
209 | * It does nothing if the connection is already closed. |
||
210 | */ |
||
211 | protected function close() |
||
212 | { |
||
213 | $this->_pdo = null; |
||
214 | $this->_active = false; |
||
215 | } |
||
216 | |||
217 | /* |
||
218 | * Set the database connection charset. |
||
219 | * Only MySql databases are supported for now. |
||
220 | * @since 3.1.2 |
||
221 | */ |
||
222 | protected function setConnectionCharset() |
||
223 | { |
||
224 | if ($this->_charset === '' || $this->_active === false) |
||
225 | return; |
||
226 | switch ($this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) |
||
227 | { |
||
228 | case 'mysql': |
||
229 | case 'sqlite': |
||
230 | $stmt = $this->_pdo->prepare('SET NAMES ?'); |
||
231 | break; |
||
232 | case 'pgsql': |
||
233 | $stmt = $this->_pdo->prepare('SET client_encoding TO ?'); |
||
234 | break; |
||
235 | default: |
||
236 | throw new TDbException('dbconnection_unsupported_driver_charset', $driver); |
||
237 | } |
||
238 | $stmt->execute([$this->_charset]); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return string The Data Source Name, or DSN, contains the information required to connect to the database. |
||
243 | */ |
||
244 | public function getConnectionString() |
||
245 | { |
||
246 | return $this->_dsn; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param string The Data Source Name, or DSN, contains the information required to connect to the database. |
||
251 | * @see http://www.php.net/manual/en/function.PDO-construct.php |
||
252 | */ |
||
253 | public function setConnectionString($value) |
||
254 | { |
||
255 | $this->_dsn = $value; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @return string the username for establishing DB connection. Defaults to empty string. |
||
260 | */ |
||
261 | public function getUsername() |
||
262 | { |
||
263 | return $this->_username; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param string the username for establishing DB connection |
||
268 | */ |
||
269 | public function setUsername($value) |
||
270 | { |
||
271 | $this->_username = $value; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @return string the password for establishing DB connection. Defaults to empty string. |
||
276 | */ |
||
277 | public function getPassword() |
||
278 | { |
||
279 | return $this->_password; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param string the password for establishing DB connection |
||
284 | */ |
||
285 | public function setPassword($value) |
||
286 | { |
||
287 | $this->_password = $value; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @return string the charset used for database connection. Defaults to emtpy string. |
||
292 | */ |
||
293 | public function getCharset () |
||
294 | { |
||
295 | return $this->_charset; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param string the charset used for database connection |
||
300 | */ |
||
301 | public function setCharset ($value) |
||
302 | { |
||
303 | $this->_charset = $value; |
||
304 | $this->setConnectionCharset(); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return PDO the PDO instance, null if the connection is not established yet |
||
309 | */ |
||
310 | public function getPdoInstance() |
||
311 | { |
||
312 | return $this->_pdo; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Creates a command for execution. |
||
317 | * @param string SQL statement associated with the new command. |
||
318 | * @return TDbCommand the DB command |
||
319 | * @throws TDbException if the connection is not active |
||
320 | */ |
||
321 | public function createCommand($sql) |
||
322 | { |
||
323 | if($this->getActive()) |
||
324 | return new TDbCommand($this, $sql); |
||
325 | else |
||
326 | throw new TDbException('dbconnection_connection_inactive'); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return TDbTransaction the currently active transaction. Null if no active transaction. |
||
331 | */ |
||
332 | public function getCurrentTransaction() |
||
333 | { |
||
334 | if($this->_transaction !== null) |
||
335 | { |
||
336 | if($this->_transaction->getActive()) |
||
337 | return $this->_transaction; |
||
338 | } |
||
339 | return null; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Starts a transaction. |
||
344 | * @return TDbTransaction the transaction initiated |
||
345 | * @throws TDbException if the connection is not active |
||
346 | */ |
||
347 | public function beginTransaction() |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @return string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'. |
||
360 | * @since 3.1.7 |
||
361 | */ |
||
362 | public function getTransactionClass() |
||
363 | { |
||
364 | return $this->_transactionClass; |
||
365 | } |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @param string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. |
||
370 | * @since 3.1.7 |
||
371 | */ |
||
372 | public function setTransactionClass($value) |
||
373 | { |
||
374 | $this->_transactionClass = (string)$value; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Returns the ID of the last inserted row or sequence value. |
||
379 | * @param string name of the sequence object (required by some DBMS) |
||
380 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
381 | * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
||
382 | */ |
||
383 | public function getLastInsertID($sequenceName = '') |
||
384 | { |
||
385 | if($this->getActive()) |
||
386 | return $this->_pdo->lastInsertId($sequenceName); |
||
387 | else |
||
388 | throw new TDbException('dbconnection_connection_inactive'); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Quotes a string for use in a query. |
||
393 | * @param string string to be quoted |
||
394 | * @return string the properly quoted string |
||
395 | * @see http://www.php.net/manual/en/function.PDO-quote.php |
||
396 | */ |
||
397 | public function quoteString($str) |
||
398 | { |
||
399 | if($this->getActive()) |
||
400 | return $this->_pdo->quote($str); |
||
401 | else |
||
402 | throw new TDbException('dbconnection_connection_inactive'); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Quotes a table name for use in a query. |
||
407 | * @param string $name table name |
||
408 | * @return string the properly quoted table name |
||
409 | */ |
||
410 | public function quoteTableName($name) |
||
411 | { |
||
412 | return $this->getDbMetaData()->quoteTableName($name); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Quotes a column name for use in a query. |
||
417 | * @param string $name column name |
||
418 | * @return string the properly quoted column name |
||
419 | */ |
||
420 | public function quoteColumnName($name) |
||
421 | { |
||
422 | return $this->getDbMetaData()->quoteColumnName($name); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Quotes a column alias for use in a query. |
||
427 | * @param string $name column name |
||
428 | * @return string the properly quoted column alias |
||
429 | */ |
||
430 | public function quoteColumnAlias($name) |
||
431 | { |
||
432 | return $this->getDbMetaData()->quoteColumnAlias($name); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @return TDbMetaData |
||
437 | */ |
||
438 | public function getDbMetaData() |
||
439 | { |
||
440 | if($this->_dbMeta === null) |
||
441 | { |
||
442 | $this->_dbMeta = TDbMetaData::getInstance($this); |
||
443 | } |
||
444 | return $this->_dbMeta; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @return TDbColumnCaseMode the case of the column names |
||
449 | */ |
||
450 | public function getColumnCase() |
||
451 | { |
||
452 | switch($this->getAttribute(PDO::ATTR_CASE)) |
||
453 | { |
||
454 | case PDO::CASE_NATURAL: |
||
455 | return TDbColumnCaseMode::Preserved; |
||
456 | case PDO::CASE_LOWER: |
||
457 | return TDbColumnCaseMode::LowerCase; |
||
458 | case PDO::CASE_UPPER: |
||
459 | return TDbColumnCaseMode::UpperCase; |
||
460 | } |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param TDbColumnCaseMode the case of the column names |
||
465 | */ |
||
466 | public function setColumnCase($value) |
||
467 | { |
||
468 | switch(TPropertyValue::ensureEnum($value, 'Prado\\Data\\TDbColumnCaseMode')) |
||
469 | { |
||
470 | case TDbColumnCaseMode::Preserved: |
||
471 | $value = PDO::CASE_NATURAL; |
||
472 | break; |
||
473 | case TDbColumnCaseMode::LowerCase: |
||
474 | $value = PDO::CASE_LOWER; |
||
475 | break; |
||
476 | case TDbColumnCaseMode::UpperCase: |
||
477 | $value = PDO::CASE_UPPER; |
||
478 | break; |
||
479 | } |
||
480 | $this->setAttribute(PDO::ATTR_CASE, $value); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @return TDbNullConversionMode how the null and empty strings are converted |
||
485 | */ |
||
486 | public function getNullConversion() |
||
487 | { |
||
488 | switch($this->getAttribute(PDO::ATTR_ORACLE_NULLS)) |
||
489 | { |
||
490 | case PDO::NULL_NATURAL: |
||
491 | return TDbNullConversionMode::Preserved; |
||
492 | case PDO::NULL_EMPTY_STRING: |
||
493 | return TDbNullConversionMode::EmptyStringToNull; |
||
494 | case PDO::NULL_TO_STRING: |
||
495 | return TDbNullConversionMode::NullToEmptyString; |
||
496 | } |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @param TDbNullConversionMode how the null and empty strings are converted |
||
501 | */ |
||
502 | public function setNullConversion($value) |
||
503 | { |
||
504 | switch(TPropertyValue::ensureEnum($value, 'Prado\\Data\\TDbNullConversionMode')) |
||
505 | { |
||
506 | case TDbNullConversionMode::Preserved: |
||
507 | $value = PDO::NULL_NATURAL; |
||
508 | break; |
||
509 | case TDbNullConversionMode::EmptyStringToNull: |
||
510 | $value = PDO::NULL_EMPTY_STRING; |
||
511 | break; |
||
512 | case TDbNullConversionMode::NullToEmptyString: |
||
513 | $value = PDO::NULL_TO_STRING; |
||
514 | break; |
||
515 | } |
||
516 | $this->setAttribute(PDO::ATTR_ORACLE_NULLS, $value); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @return boolean whether creating or updating a DB record will be automatically committed. |
||
521 | * Some DBMS (such as sqlite) may not support this feature. |
||
522 | */ |
||
523 | public function getAutoCommit() |
||
524 | { |
||
525 | return $this->getAttribute(PDO::ATTR_AUTOCOMMIT); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * @param boolean whether creating or updating a DB record will be automatically committed. |
||
530 | * Some DBMS (such as sqlite) may not support this feature. |
||
531 | */ |
||
532 | public function setAutoCommit($value) |
||
533 | { |
||
534 | $this->setAttribute(PDO::ATTR_AUTOCOMMIT, TPropertyValue::ensureBoolean($value)); |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * @return boolean whether the connection is persistent or not |
||
539 | * Some DBMS (such as sqlite) may not support this feature. |
||
540 | */ |
||
541 | public function getPersistent() |
||
542 | { |
||
543 | return $this->getAttribute(PDO::ATTR_PERSISTENT); |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * @param boolean whether the connection is persistent or not |
||
548 | * Some DBMS (such as sqlite) may not support this feature. |
||
549 | */ |
||
550 | public function setPersistent($value) |
||
551 | { |
||
552 | return $this->setAttribute(PDO::ATTR_PERSISTENT, TPropertyValue::ensureBoolean($value)); |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * @return string name of the DB driver |
||
557 | */ |
||
558 | public function getDriverName() |
||
559 | { |
||
560 | return $this->getAttribute(PDO::ATTR_DRIVER_NAME); |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * @return string the version information of the DB driver |
||
565 | */ |
||
566 | public function getClientVersion() |
||
567 | { |
||
568 | return $this->getAttribute(PDO::ATTR_CLIENT_VERSION); |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * @return string the status of the connection |
||
573 | * Some DBMS (such as sqlite) may not support this feature. |
||
574 | */ |
||
575 | public function getConnectionStatus() |
||
576 | { |
||
577 | return $this->getAttribute(PDO::ATTR_CONNECTION_STATUS); |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * @return boolean whether the connection performs data prefetching |
||
582 | */ |
||
583 | public function getPrefetch() |
||
584 | { |
||
585 | return $this->getAttribute(PDO::ATTR_PREFETCH); |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * @return string the information of DBMS server |
||
590 | */ |
||
591 | public function getServerInfo() |
||
592 | { |
||
593 | return $this->getAttribute(PDO::ATTR_SERVER_INFO); |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @return string the version information of DBMS server |
||
598 | */ |
||
599 | public function getServerVersion() |
||
600 | { |
||
601 | return $this->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * @return int timeout settings for the connection |
||
606 | */ |
||
607 | public function getTimeout() |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Obtains a specific DB connection attribute information. |
||
614 | * @param int the attribute to be queried |
||
615 | * @return mixed the corresponding attribute information |
||
616 | * @see http://www.php.net/manual/en/function.PDO-getAttribute.php |
||
617 | */ |
||
618 | public function getAttribute($name) |
||
619 | { |
||
620 | if($this->getActive()) |
||
621 | return $this->_pdo->getAttribute($name); |
||
622 | else |
||
623 | throw new TDbException('dbconnection_connection_inactive'); |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Sets an attribute on the database connection. |
||
628 | * @param int the attribute to be set |
||
629 | * @param mixed the attribute value |
||
630 | * @see http://www.php.net/manual/en/function.PDO-setAttribute.php |
||
631 | */ |
||
632 | public function setAttribute($name, $value) |
||
638 | } |
||
639 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths