| Total Complexity | 71 |
| Total Lines | 436 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 0 |
Complex classes like PDO 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 PDO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class PDO extends BasePDO implements PDOInterface |
||
| 35 | { |
||
| 36 | public const VERSION = '1.1.0'; |
||
| 37 | public const DRIVER_NAME = 'crate'; |
||
| 38 | |||
| 39 | public const DSN_REGEX = '/^(?:crate:)(?:((?:[\w\.-]+:\d+\,?)+))\/?([\w]+)?$/'; |
||
| 40 | |||
| 41 | public const CRATE_ATTR_HTTP_BASIC_AUTH = 1000; |
||
| 42 | public const CRATE_ATTR_DEFAULT_SCHEMA = 1001; |
||
| 43 | |||
| 44 | public const CRATE_ATTR_SSL_MODE = 1008; |
||
| 45 | public const CRATE_ATTR_SSL_MODE_DISABLED = 1; |
||
| 46 | public const CRATE_ATTR_SSL_MODE_ENABLED_BUT_WITHOUT_HOST_VERIFICATION = 2; |
||
| 47 | public const CRATE_ATTR_SSL_MODE_REQUIRED = 3; |
||
| 48 | |||
| 49 | public const CRATE_ATTR_SSL_KEY_PATH = 1002; |
||
| 50 | public const CRATE_ATTR_SSL_KEY_PASSWORD = 1003; |
||
| 51 | public const CRATE_ATTR_SSL_CERT_PATH = 1004; |
||
| 52 | public const CRATE_ATTR_SSL_CERT_PASSWORD = 1005; |
||
| 53 | public const CRATE_ATTR_SSL_CA_PATH = 1006; |
||
| 54 | public const CRATE_ATTR_SSL_CA_PASSWORD = 1007; |
||
| 55 | |||
| 56 | public const PARAM_FLOAT = 6; |
||
| 57 | public const PARAM_DOUBLE = 7; |
||
| 58 | public const PARAM_LONG = 8; |
||
| 59 | public const PARAM_ARRAY = 9; |
||
| 60 | public const PARAM_OBJECT = 10; |
||
| 61 | public const PARAM_TIMESTAMP = 11; |
||
| 62 | public const PARAM_IP = 12; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $attributes = [ |
||
| 68 | 'defaultFetchMode' => self::FETCH_BOTH, |
||
| 69 | 'errorMode' => self::ERRMODE_SILENT, |
||
| 70 | 'sslMode' => self::CRATE_ATTR_SSL_MODE_DISABLED, |
||
| 71 | 'statementClass' => PDOStatement::class, |
||
| 72 | 'timeout' => 0.0, |
||
| 73 | 'auth' => [], |
||
| 74 | 'defaultSchema' => 'doc', |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Http\ServerInterface |
||
| 79 | */ |
||
| 80 | private $server; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var PDOStatement|null |
||
| 84 | */ |
||
| 85 | private $lastStatement; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var callable |
||
| 89 | */ |
||
| 90 | private $request; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | * |
||
| 95 | * @param string $dsn The HTTP endpoint to call |
||
| 96 | * @param null $username Username for basic auth |
||
|
|
|||
| 97 | * @param null $passwd Password for basic auth |
||
| 98 | * @param null|array $options Attributes to set on the PDO |
||
| 99 | */ |
||
| 100 | public function __construct($dsn, $username = null, $passwd = null, $options = []) |
||
| 101 | { |
||
| 102 | $dsnParts = self::parseDSN($dsn); |
||
| 103 | $servers = self::serversFromDsnParts($dsnParts); |
||
| 104 | |||
| 105 | $this->setServer(new ServerPool($servers)); |
||
| 106 | |||
| 107 | foreach ((array)$options as $attribute => $value) { |
||
| 108 | $this->setAttribute($attribute, $value); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (!empty($username)) { |
||
| 112 | $this->setAttribute(self::CRATE_ATTR_HTTP_BASIC_AUTH, [$username, $passwd]); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (!empty($dsnParts[1])) { |
||
| 116 | $this->setAttribute(self::CRATE_ATTR_DEFAULT_SCHEMA, $dsnParts[1]); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Define a callback that will be used in the PDOStatements |
||
| 120 | // This way we don't expose this as a public api to the end users. |
||
| 121 | $this->request = function (PDOStatement $statement, $sql, array $parameters) { |
||
| 122 | |||
| 123 | $this->lastStatement = $statement; |
||
| 124 | |||
| 125 | try { |
||
| 126 | return $this->server->execute($sql, $parameters); |
||
| 127 | } catch (Exception\RuntimeException $e) { |
||
| 128 | if ($this->getAttribute(self::ATTR_ERRMODE) === self::ERRMODE_EXCEPTION) { |
||
| 129 | throw new Exception\PDOException($e->getMessage(), $e->getCode()); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($this->getAttribute(self::ATTR_ERRMODE) === self::ERRMODE_WARNING) { |
||
| 133 | trigger_error(sprintf('[%d] %s', $e->getCode(), $e->getMessage()), E_USER_WARNING); |
||
| 134 | } |
||
| 135 | |||
| 136 | // should probably wrap this in a error object ? |
||
| 137 | return [ |
||
| 138 | 'code' => $e->getCode(), |
||
| 139 | 'message' => $e->getMessage(), |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | }; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Change the server implementation |
||
| 147 | * |
||
| 148 | * @param ServerInterface $server |
||
| 149 | */ |
||
| 150 | public function setServer(ServerInterface $server): void |
||
| 151 | { |
||
| 152 | $this->server = $server; |
||
| 153 | $this->server->configure($this); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Extract servers and optional custom schema from DSN string |
||
| 158 | * |
||
| 159 | * @param string $dsn The DSN string |
||
| 160 | * |
||
| 161 | * @throws \Crate\PDO\Exception\PDOException on an invalid DSN string |
||
| 162 | * |
||
| 163 | * @return array An array of ['host:post,host:port,...', 'schema'] |
||
| 164 | */ |
||
| 165 | private static function parseDSN($dsn) |
||
| 166 | { |
||
| 167 | $matches = []; |
||
| 168 | |||
| 169 | if (!preg_match(static::DSN_REGEX, $dsn, $matches)) { |
||
| 170 | throw new PDOException(sprintf('Invalid DSN %s', $dsn)); |
||
| 171 | } |
||
| 172 | |||
| 173 | return array_slice($matches, 1); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Extract host:port pairs out of the DSN parts |
||
| 178 | * |
||
| 179 | * @param array $dsnParts The parts of the parsed DSN string |
||
| 180 | * |
||
| 181 | * @return array An array of host:port strings |
||
| 182 | */ |
||
| 183 | private static function serversFromDsnParts($dsnParts) |
||
| 184 | { |
||
| 185 | return explode(',', trim($dsnParts[0], ',')); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritDoc} |
||
| 190 | */ |
||
| 191 | public function prepare($statement, $options = null) |
||
| 192 | { |
||
| 193 | $options = ArrayUtils::toArray($options); |
||
| 194 | |||
| 195 | if (isset($options[self::ATTR_CURSOR])) { |
||
| 196 | trigger_error(sprintf('%s not supported', __METHOD__), E_USER_WARNING); |
||
| 197 | |||
| 198 | return true; |
||
| 199 | } |
||
| 200 | |||
| 201 | return new PDOStatement($this, $this->request, $statement, $options); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritDoc} |
||
| 206 | */ |
||
| 207 | public function beginTransaction() |
||
| 208 | { |
||
| 209 | return true; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritDoc} |
||
| 214 | */ |
||
| 215 | public function commit() |
||
| 216 | { |
||
| 217 | return true; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritDoc} |
||
| 222 | */ |
||
| 223 | public function rollBack() |
||
| 224 | { |
||
| 225 | throw new Exception\UnsupportedException; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritDoc} |
||
| 230 | */ |
||
| 231 | public function inTransaction() |
||
| 232 | { |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritDoc} |
||
| 238 | */ |
||
| 239 | public function exec($statement) |
||
| 240 | { |
||
| 241 | $statement = $this->prepare($statement); |
||
| 242 | $result = $statement->execute(); |
||
| 243 | |||
| 244 | return $result === false ? false : $statement->rowCount(); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritDoc} |
||
| 249 | */ |
||
| 250 | public function query($statement) |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritDoc} |
||
| 260 | */ |
||
| 261 | public function lastInsertId($name = null) |
||
| 262 | { |
||
| 263 | throw new Exception\UnsupportedException; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritDoc} |
||
| 268 | */ |
||
| 269 | public function errorCode() |
||
| 270 | { |
||
| 271 | return $this->lastStatement === null ? null : $this->lastStatement->errorCode(); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritDoc} |
||
| 276 | */ |
||
| 277 | public function errorInfo() |
||
| 278 | { |
||
| 279 | return $this->lastStatement === null ? null : $this->lastStatement->errorInfo(); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritDoc} |
||
| 284 | * |
||
| 285 | * @throws \Crate\PDO\Exception\PDOException |
||
| 286 | * @throws \Crate\PDO\Exception\InvalidArgumentException |
||
| 287 | */ |
||
| 288 | public function setAttribute($attribute, $value) |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritDoc} |
||
| 355 | * |
||
| 356 | * @throws \Crate\PDO\Exception\PDOException |
||
| 357 | */ |
||
| 358 | public function getAttribute($attribute) |
||
| 359 | { |
||
| 360 | switch ($attribute) { |
||
| 361 | case self::ATTR_PERSISTENT: |
||
| 362 | return false; |
||
| 363 | |||
| 364 | case self::ATTR_PREFETCH: |
||
| 365 | return false; |
||
| 366 | |||
| 367 | case self::ATTR_CLIENT_VERSION: |
||
| 368 | return self::VERSION; |
||
| 369 | |||
| 370 | case self::ATTR_SERVER_VERSION: |
||
| 371 | return $this->server->getServerVersion(); |
||
| 372 | |||
| 373 | case self::ATTR_SERVER_INFO: |
||
| 374 | return $this->server->getServerInfo(); |
||
| 375 | |||
| 376 | case self::ATTR_TIMEOUT: |
||
| 377 | return $this->attributes['timeout']; |
||
| 378 | |||
| 379 | case self::CRATE_ATTR_HTTP_BASIC_AUTH: |
||
| 380 | return $this->attributes['auth']; |
||
| 381 | |||
| 382 | case self::ATTR_DEFAULT_FETCH_MODE: |
||
| 383 | return $this->attributes['defaultFetchMode']; |
||
| 384 | |||
| 385 | case self::ATTR_ERRMODE: |
||
| 386 | return $this->attributes['errorMode']; |
||
| 387 | |||
| 388 | case self::ATTR_DRIVER_NAME: |
||
| 389 | return static::DRIVER_NAME; |
||
| 390 | |||
| 391 | case self::ATTR_STATEMENT_CLASS: |
||
| 392 | return [$this->attributes['statementClass']]; |
||
| 393 | |||
| 394 | case self::CRATE_ATTR_DEFAULT_SCHEMA: |
||
| 395 | return $this->attributes['defaultSchema']; |
||
| 396 | |||
| 397 | case self::CRATE_ATTR_SSL_MODE: |
||
| 398 | return $this->attributes['sslMode']; |
||
| 399 | |||
| 400 | case self::CRATE_ATTR_SSL_CA_PATH: |
||
| 401 | return $this->attributes['sslCa'] ?? null; |
||
| 402 | |||
| 403 | case self::CRATE_ATTR_SSL_CA_PASSWORD: |
||
| 404 | return $this->attributes['sslCaPassword'] ?? null; |
||
| 405 | |||
| 406 | case self::CRATE_ATTR_SSL_CERT_PATH: |
||
| 407 | return $this->attributes['sslCert'] ?? null; |
||
| 408 | |||
| 409 | case self::CRATE_ATTR_SSL_CERT_PASSWORD: |
||
| 410 | return $this->attributes['sslCertPassword'] ?? null; |
||
| 411 | |||
| 412 | case self::CRATE_ATTR_SSL_KEY_PATH: |
||
| 413 | return $this->attributes['sslKey'] ?? null; |
||
| 414 | |||
| 415 | case self::CRATE_ATTR_SSL_KEY_PASSWORD: |
||
| 416 | return $this->attributes['sslKeyPassword'] ?? null; |
||
| 417 | |||
| 418 | default: |
||
| 419 | // PHP Switch is a lose comparison |
||
| 420 | if ($attribute === self::ATTR_AUTOCOMMIT) { |
||
| 421 | return true; |
||
| 422 | } |
||
| 423 | |||
| 424 | throw new Exception\PDOException(sprintf('Unsupported driver attribute: %s', $attribute)); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritDoc} |
||
| 430 | */ |
||
| 431 | public function quote($string, $parameter_type = self::PARAM_STR) |
||
| 432 | { |
||
| 433 | switch ($parameter_type) { |
||
| 434 | case self::PARAM_INT: |
||
| 435 | return (int)$string; |
||
| 436 | |||
| 437 | case self::PARAM_BOOL: |
||
| 438 | return (bool)$string; |
||
| 439 | |||
| 440 | case self::PARAM_NULL: |
||
| 441 | return null; |
||
| 442 | |||
| 443 | case self::PARAM_LOB: |
||
| 444 | throw new Exception\UnsupportedException('This is not supported by crate.io'); |
||
| 445 | |||
| 446 | case self::PARAM_STR: |
||
| 447 | throw new Exception\UnsupportedException('This is not supported, please use prepared statements.'); |
||
| 448 | |||
| 449 | default: |
||
| 450 | throw new Exception\InvalidArgumentException('Unknown param type'); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritDoc} |
||
| 456 | */ |
||
| 457 | public static function getAvailableDrivers() |
||
| 458 | { |
||
| 459 | return array_merge(parent::getAvailableDrivers(), [static::DRIVER_NAME]); |
||
| 460 | } |
||
| 461 | |||
| 462 | public function getServerVersion() |
||
| 463 | { |
||
| 464 | return $this->server->getServerVersion(); |
||
| 465 | } |
||
| 466 | |||
| 467 | public function getServerInfo() |
||
| 470 | } |
||
| 471 | } |
||
| 472 |