| Total Complexity | 42 |
| Total Lines | 257 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ConnectionPDOOracle 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 ConnectionPDOOracle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | final class ConnectionPDOOracle extends Connection implements ConnectionPDOInterface |
||
| 30 | { |
||
| 31 | private ?PDO $pdo = null; |
||
| 32 | private ?Query $query = null; |
||
| 33 | private ?QueryBuilderInterface $queryBuilder = null; |
||
| 34 | private ?QuoterInterface $quoter = null; |
||
| 35 | private ?SchemaInterface $schema = null; |
||
| 36 | private string $serverVersion = ''; |
||
| 37 | |||
| 38 | public function __construct( |
||
| 39 | private PDODriver $driver, |
||
| 40 | private QueryCache $queryCache, |
||
| 41 | private SchemaCache $schemaCache |
||
| 42 | ) { |
||
| 43 | parent::__construct($queryCache); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Reset the connection after cloning. |
||
| 48 | */ |
||
| 49 | public function __clone() |
||
| 50 | { |
||
| 51 | $this->master = null; |
||
| 52 | $this->slave = null; |
||
| 53 | $this->transaction = null; |
||
| 54 | |||
| 55 | if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) { |
||
| 56 | /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */ |
||
| 57 | $this->pdo = null; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Close the connection before serializing. |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function __sleep(): array |
||
| 67 | { |
||
| 68 | $fields = (array) $this; |
||
| 69 | |||
| 70 | unset( |
||
| 71 | $fields["\000" . __CLASS__ . "\000" . 'pdo'], |
||
| 72 | $fields["\000" . __CLASS__ . "\000" . 'master'], |
||
| 73 | $fields["\000" . __CLASS__ . "\000" . 'slave'], |
||
| 74 | $fields["\000" . __CLASS__ . "\000" . 'transaction'], |
||
| 75 | $fields["\000" . __CLASS__ . "\000" . 'schema'] |
||
| 76 | ); |
||
| 77 | |||
| 78 | return array_keys($fields); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function createCommand(?string $sql = null, array $params = []): CommandPDOOracle |
||
| 98 | } |
||
| 99 | |||
| 100 | public function createTransaction(): TransactionInterface |
||
| 101 | { |
||
| 102 | return new TransactionPDOOracle($this); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function close(): void |
||
| 106 | { |
||
| 107 | if (!empty($this->master)) { |
||
| 108 | /** @var ConnectionPDOOracle */ |
||
| 109 | $db = $this->master; |
||
| 110 | |||
| 111 | if ($this->pdo === $db->getPDO()) { |
||
| 112 | $this->pdo = null; |
||
| 113 | } |
||
| 114 | |||
| 115 | $db->close(); |
||
| 116 | $this->master = null; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($this->pdo !== null) { |
||
| 120 | $this->logger?->log( |
||
| 121 | LogLevel::DEBUG, |
||
| 122 | 'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__, |
||
| 123 | ); |
||
| 124 | |||
| 125 | $this->pdo = null; |
||
| 126 | $this->transaction = null; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (!empty($this->slave)) { |
||
| 130 | $this->slave->close(); |
||
| 131 | $this->slave = null; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getDriver(): PDODriver |
||
| 136 | { |
||
| 137 | return $this->driver; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getDriverName(): string |
||
| 141 | { |
||
| 142 | return 'oci'; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getMasterPdo(): PDO|null |
||
| 146 | { |
||
| 147 | $this->open(); |
||
| 148 | return $this->pdo; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getPDO(): ?PDO |
||
| 152 | { |
||
| 153 | return $this->pdo; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getQuery(): Query |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @throws Exception|InvalidConfigException |
||
| 167 | */ |
||
| 168 | public function getQueryBuilder(): QueryBuilderInterface |
||
| 169 | { |
||
| 170 | if ($this->queryBuilder === null) { |
||
| 171 | $this->queryBuilder = new QueryBuilderPDOOracle( |
||
| 172 | $this->createCommand(), |
||
| 173 | $this->getQuery(), |
||
| 174 | $this->getQuoter(), |
||
| 175 | $this->getSchema(), |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $this->queryBuilder; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getQuoter(): QuoterInterface |
||
| 183 | { |
||
| 184 | if ($this->quoter === null) { |
||
| 185 | $this->quoter = new Quoter('"', '"', $this->getTablePrefix()); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $this->quoter; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @throws Exception |
||
| 193 | */ |
||
| 194 | public function getServerVersion(): string |
||
| 195 | { |
||
| 196 | if ($this->serverVersion === '') { |
||
| 197 | /** @var mixed */ |
||
| 198 | $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
| 199 | $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.'; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $this->serverVersion; |
||
| 203 | } |
||
| 204 | |||
| 205 | public function getSchema(): SchemaInterface |
||
| 206 | { |
||
| 207 | if ($this->schema === null) { |
||
| 208 | $this->schema = new SchemaPDOOracle($this, $this->schemaCache); |
||
| 209 | } |
||
| 210 | |||
| 211 | return $this->schema; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getSlavePdo(bool $fallbackToMaster = true): ?PDO |
||
| 215 | { |
||
| 216 | /** @var ConnectionPDOOracle|null $db */ |
||
| 217 | $db = $this->getSlave(false); |
||
| 218 | |||
| 219 | if ($db === null) { |
||
| 220 | return $fallbackToMaster ? $this->getMasterPdo() : null; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $db->getPDO(); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function isActive(): bool |
||
| 227 | { |
||
| 228 | return $this->pdo !== null; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function open(): void |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Initializes the DB connection. |
||
| 270 | * |
||
| 271 | * This method is invoked right after the DB connection is established. |
||
| 272 | * |
||
| 273 | * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
||
| 274 | * |
||
| 275 | * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
||
| 276 | * |
||
| 277 | * It then triggers an {@see EVENT_AFTER_OPEN} event. |
||
| 278 | */ |
||
| 279 | protected function initConnection(): void |
||
| 286 | } |
||
| 287 | } |
||
| 289 |
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