| Total Complexity | 40 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConnectionPDOSqlite 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 ConnectionPDOSqlite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | final class ConnectionPDOSqlite extends Connection implements ConnectionPDOInterface |
||
| 31 | { |
||
| 32 | private ?PDO $pdo = 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 |
||
| 79 | } |
||
| 80 | |||
| 81 | public function createCommand(?string $sql = null, array $params = []): CommandInterface |
||
| 82 | { |
||
| 83 | $command = new CommandPDOSqlite($this, $this->queryCache); |
||
| 84 | |||
| 85 | if ($sql !== null) { |
||
| 86 | $command->setSql($sql); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($this->logger !== null) { |
||
| 90 | $command->setLogger($this->logger); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($this->profiler !== null) { |
||
| 94 | $command->setProfiler($this->profiler); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $command->bindValues($params); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function createTransaction(): TransactionInterface |
||
| 101 | { |
||
| 102 | return new TransactionPDOSqlite($this); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function close(): void |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getDriver(): PDODriver |
||
| 136 | { |
||
| 137 | return $this->driver; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getDriverName(): string |
||
| 141 | { |
||
| 142 | return 'sqlite'; |
||
| 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 getQueryBuilder(): QueryBuilderInterface |
||
| 157 | { |
||
| 158 | if ($this->queryBuilder === null) { |
||
| 159 | $this->queryBuilder = new QueryBuilderPDOSqlite( |
||
| 160 | $this->createCommand(), |
||
| 161 | $this->getQuoter(), |
||
| 162 | $this->getSchema(), |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $this->queryBuilder; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getQuoter(): QuoterInterface |
||
| 170 | { |
||
| 171 | if ($this->quoter === null) { |
||
| 172 | $this->quoter = new Quoter('`', '`', $this->getTablePrefix()); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $this->quoter; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @throws Exception |
||
| 180 | */ |
||
| 181 | public function getServerVersion(): string |
||
| 182 | { |
||
| 183 | if ($this->serverVersion === '') { |
||
| 184 | /** @var mixed */ |
||
| 185 | $version = $this->getSlavePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
| 186 | $this->serverVersion = is_string($version) ? $version : 'Version could not be determined.'; |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->serverVersion; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function getSchema(): SchemaInterface |
||
| 193 | { |
||
| 194 | if ($this->schema === null) { |
||
| 195 | $this->schema = new SchemaPDOSqlite($this, $this->schemaCache); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $this->schema; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getSlavePdo(bool $fallbackToMaster = true): ?PDO |
||
| 202 | { |
||
| 203 | /** @var ConnectionPDOSqlite|null $db */ |
||
| 204 | $db = $this->getSlave(false); |
||
| 205 | |||
| 206 | if ($db === null) { |
||
| 207 | return $fallbackToMaster ? $this->getMasterPdo() : null; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $db->getPDO(); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function isActive(): bool |
||
| 214 | { |
||
| 215 | return $this->pdo !== null; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function open(): void |
||
| 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 | private function initConnection(): void |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 |
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