| Total Complexity | 42 |
| Total Lines | 252 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConnectionPDOPgsql 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 ConnectionPDOPgsql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class ConnectionPDOPgsql extends Connection implements ConnectionPDOInterface |
||
| 28 | { |
||
| 29 | private ?PDO $pdo = null; |
||
| 30 | private ?QueryBuilderInterface $queryBuilder = null; |
||
| 31 | private ?QuoterInterface $quoter = null; |
||
| 32 | private ?SchemaInterface $schema = null; |
||
| 33 | private string $serverVersion = ''; |
||
| 34 | |||
| 35 | public function __construct( |
||
| 36 | private PDODriver $driver, |
||
| 37 | private QueryCache $queryCache, |
||
| 38 | private SchemaCache $schemaCache |
||
| 39 | ) { |
||
| 40 | parent::__construct($queryCache); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Reset the connection after cloning. |
||
| 45 | */ |
||
| 46 | public function __clone() |
||
| 47 | { |
||
| 48 | $this->master = null; |
||
| 49 | $this->slave = null; |
||
| 50 | $this->transaction = null; |
||
| 51 | |||
| 52 | if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) { |
||
| 53 | /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */ |
||
| 54 | $this->pdo = null; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Close the connection before serializing. |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function __sleep(): array |
||
| 64 | { |
||
| 65 | $fields = (array) $this; |
||
| 66 | |||
| 67 | unset( |
||
| 68 | $fields["\000" . __CLASS__ . "\000" . 'pdo'], |
||
| 69 | $fields["\000" . __CLASS__ . "\000" . 'master'], |
||
| 70 | $fields["\000" . __CLASS__ . "\000" . 'slave'], |
||
| 71 | $fields["\000" . __CLASS__ . "\000" . 'transaction'], |
||
| 72 | $fields["\000" . __CLASS__ . "\000" . 'schema'] |
||
| 73 | ); |
||
| 74 | |||
| 75 | return array_keys($fields); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function createCommand(?string $sql = null, array $params = []): CommandInterface |
||
| 79 | { |
||
| 80 | $command = new CommandPDOPgsql($this, $this->queryCache); |
||
| 81 | |||
| 82 | if ($sql !== null) { |
||
| 83 | $command->setSql($sql); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($this->logger !== null) { |
||
| 87 | $command->setLogger($this->logger); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($this->profiler !== null) { |
||
| 91 | $command->setProfiler($this->profiler); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $command->bindValues($params); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function createTransaction(): TransactionInterface |
||
| 98 | { |
||
| 99 | return new TransactionPDOPgsql($this); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function close(): void |
||
| 103 | { |
||
| 104 | if (!empty($this->master)) { |
||
| 105 | /** @var ConnectionPDOPgsql */ |
||
| 106 | $db = $this->master; |
||
| 107 | |||
| 108 | if ($this->pdo === $db->getPDO()) { |
||
| 109 | $this->pdo = null; |
||
| 110 | } |
||
| 111 | |||
| 112 | $db->close(); |
||
| 113 | $this->master = null; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($this->pdo !== null) { |
||
| 117 | $this->logger?->log( |
||
| 118 | LogLevel::DEBUG, |
||
| 119 | 'Closing DB connection: ' . $this->driver->getDsn() . ' ' . __METHOD__, |
||
| 120 | ); |
||
| 121 | |||
| 122 | $this->pdo = null; |
||
| 123 | $this->transaction = null; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!empty($this->slave)) { |
||
| 127 | $this->slave->close(); |
||
| 128 | $this->slave = null; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getDriver(): PDODriver |
||
| 133 | { |
||
| 134 | return $this->driver; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getDriverName(): string |
||
| 138 | { |
||
| 139 | return 'pgsql'; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getMasterPdo(): PDO|null |
||
| 143 | { |
||
| 144 | $this->open(); |
||
| 145 | return $this->pdo; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getPDO(): ?PDO |
||
| 149 | { |
||
| 150 | return $this->pdo; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getQueryBuilder(): QueryBuilderInterface |
||
| 154 | { |
||
| 155 | if ($this->queryBuilder === null) { |
||
| 156 | $this->queryBuilder = new QueryBuilderPDOPgsql( |
||
| 157 | $this->createCommand(), |
||
| 158 | $this->getQuoter(), |
||
| 159 | $this->getSchema(), |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $this->queryBuilder; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getQuoter(): QuoterInterface |
||
| 167 | { |
||
| 168 | if ($this->quoter === null) { |
||
| 169 | $this->quoter = new Quoter('"', '"', $this->getTablePrefix()); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $this->quoter; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @throws Exception |
||
| 177 | */ |
||
| 178 | public function getServerVersion(): string |
||
| 179 | { |
||
| 180 | if ($this->serverVersion === '') { |
||
| 181 | /** @var mixed */ |
||
| 182 | $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
| 183 | $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.'; |
||
| 184 | } |
||
| 185 | |||
| 186 | return $this->serverVersion; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getSchema(): SchemaInterface |
||
| 190 | { |
||
| 191 | if ($this->schema === null) { |
||
| 192 | $this->schema = new SchemaPDOPgsql($this, $this->schemaCache); |
||
| 193 | } |
||
| 194 | |||
| 195 | return $this->schema; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getSlavePdo(bool $fallbackToMaster = true): ?PDO |
||
| 199 | { |
||
| 200 | /** @var ConnectionPDOPgsql|null $db */ |
||
| 201 | $db = $this->getSlave(false); |
||
| 202 | |||
| 203 | if ($db === null) { |
||
| 204 | return $fallbackToMaster ? $this->getMasterPdo() : null; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $db->getPDO(); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function isActive(): bool |
||
| 213 | } |
||
| 214 | |||
| 215 | public function open(): void |
||
| 216 | { |
||
| 217 | if (!empty($this->pdo)) { |
||
| 218 | return; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Initializes the DB connection. |
||
| 254 | * |
||
| 255 | * This method is invoked right after the DB connection is established. |
||
| 256 | * |
||
| 257 | * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
||
| 258 | * |
||
| 259 | * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
||
| 260 | * |
||
| 261 | * It then triggers an {@see EVENT_AFTER_OPEN} event. |
||
| 262 | */ |
||
| 263 | protected function initConnection(): void |
||
| 279 | } |
||
| 280 | } |
||
| 281 | } |
||
| 282 |
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