| Total Complexity | 41 |
| Total Lines | 250 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConnectionPDOMysql 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 ConnectionPDOMysql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | final class ConnectionPDOMysql extends Connection implements ConnectionPDOInterface |
||
| 30 | { |
||
| 31 | private ?PDO $pdo = null; |
||
| 32 | private ?QueryBuilderInterface $queryBuilder = null; |
||
| 33 | private ?QuoterInterface $quoter = null; |
||
| 34 | private ?SchemaInterface $schema = null; |
||
| 35 | private string $serverVersion = ''; |
||
| 36 | |||
| 37 | public function __construct( |
||
| 38 | private PDODriver $driver, |
||
| 39 | private QueryCache $queryCache, |
||
| 40 | private SchemaCache $schemaCache |
||
| 41 | ) { |
||
| 42 | parent::__construct($queryCache); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Reset the connection after cloning. |
||
| 47 | */ |
||
| 48 | public function __clone() |
||
| 49 | { |
||
| 50 | $this->master = null; |
||
| 51 | $this->slave = null; |
||
| 52 | $this->transaction = null; |
||
| 53 | |||
| 54 | if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) { |
||
| 55 | /** reset PDO connection, unless its sqlite in-memory, which can only have one connection */ |
||
| 56 | $this->pdo = null; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Close the connection before serializing. |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function __sleep(): array |
||
| 78 | } |
||
| 79 | |||
| 80 | public function createCommand(?string $sql = null, array $params = []): CommandInterface |
||
| 97 | } |
||
| 98 | |||
| 99 | public function createTransaction(): TransactionInterface |
||
| 100 | { |
||
| 101 | return new TransactionPDOMysql($this); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function close(): void |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getDriver(): PDODriver |
||
| 135 | { |
||
| 136 | return $this->driver; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getDriverName(): string |
||
| 140 | { |
||
| 141 | return 'mysql'; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getMasterPDO(): ?PDO |
||
| 145 | { |
||
| 146 | $this->open(); |
||
| 147 | return $this->pdo; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getPDO(): ?PDO |
||
| 151 | { |
||
| 152 | return $this->pdo; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @throws Exception|InvalidConfigException |
||
| 157 | */ |
||
| 158 | public function getQueryBuilder(): QueryBuilderInterface |
||
| 159 | { |
||
| 160 | if ($this->queryBuilder === null) { |
||
| 161 | $this->queryBuilder = new QueryBuilderPDOMysql( |
||
| 162 | $this->createCommand(), |
||
| 163 | $this->getQuoter(), |
||
| 164 | $this->getSchema(), |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->queryBuilder; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function getQuoter(): QuoterInterface |
||
| 172 | { |
||
| 173 | if ($this->quoter === null) { |
||
| 174 | $this->quoter = new Quoter('`', '`', $this->getTablePrefix()); |
||
| 175 | } |
||
| 176 | |||
| 177 | return $this->quoter; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @throws Exception |
||
| 182 | */ |
||
| 183 | public function getServerVersion(): string |
||
| 184 | { |
||
| 185 | if ($this->serverVersion === '') { |
||
| 186 | /** @var mixed */ |
||
| 187 | $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
| 188 | $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.'; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->serverVersion; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getSchema(): SchemaInterface |
||
| 195 | { |
||
| 196 | if ($this->schema === null) { |
||
| 197 | $this->schema = new SchemaPDOMysql($this, $this->schemaCache); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->schema; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getSlavePDO(bool $fallbackToMaster = true): ?PDO |
||
| 204 | { |
||
| 205 | $db = $this->getSlave(false); |
||
| 206 | |||
| 207 | if ($db === null) { |
||
| 208 | return $fallbackToMaster ? $this->getMasterPdo() : null; |
||
| 209 | } |
||
| 210 | |||
| 211 | return $db->getPDO(); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function isActive(): bool |
||
| 217 | } |
||
| 218 | |||
| 219 | public function open(): void |
||
| 220 | { |
||
| 221 | if (!empty($this->pdo)) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (!empty($this->masters)) { |
||
| 226 | $db = $this->getMaster(); |
||
| 227 | |||
| 228 | if ($db !== null) { |
||
| 229 | $this->pdo = $db->getPDO(); |
||
| 230 | return; |
||
| 231 | } |
||
| 232 | |||
| 233 | throw new InvalidConfigException('None of the master DB servers is available.'); |
||
| 234 | } |
||
| 235 | |||
| 236 | if (empty($this->driver->getDsn())) { |
||
| 237 | throw new InvalidConfigException('Connection::dsn cannot be empty.'); |
||
| 238 | } |
||
| 239 | |||
| 240 | $token = 'Opening DB connection: ' . $this->driver->getDsn(); |
||
| 241 | |||
| 242 | try { |
||
| 243 | $this->logger?->log(LogLevel::INFO, $token); |
||
| 244 | $this->profiler?->begin($token, [__METHOD__]); |
||
| 245 | $this->initConnection(); |
||
| 246 | $this->profiler?->end($token, [__METHOD__]); |
||
| 247 | } catch (PDOException $e) { |
||
| 248 | $this->profiler?->end($token, [__METHOD__]); |
||
| 249 | $this->logger?->log(LogLevel::ERROR, $token); |
||
| 250 | |||
| 251 | throw new Exception($e->getMessage(), (array) $e->errorInfo, $e); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Initializes the DB connection. |
||
| 257 | * |
||
| 258 | * This method is invoked right after the DB connection is established. |
||
| 259 | * |
||
| 260 | * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
||
| 261 | * |
||
| 262 | * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
||
| 263 | * |
||
| 264 | * It then triggers an {@see EVENT_AFTER_OPEN} event. |
||
| 265 | */ |
||
| 266 | 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