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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 29 | class PDO extends BasePDO implements PDOInterface |
||
| 30 | { |
||
| 31 | const VERSION = '0.7.0'; |
||
| 32 | const DRIVER_NAME = 'crate'; |
||
| 33 | |||
| 34 | const DSN_REGEX = '/^(?:crate:)(?:((?:[\w\d\.-]+:\d+\,?)+))\/?([\w]+)?$/'; |
||
| 35 | |||
| 36 | const CRATE_ATTR_HTTP_BASIC_AUTH = 1000; |
||
| 37 | /** |
||
| 38 | * deprecated since version 0.4 |
||
| 39 | * @deprecated |
||
| 40 | */ |
||
| 41 | const ATTR_HTTP_BASIC_AUTH = self::CRATE_ATTR_HTTP_BASIC_AUTH; |
||
| 42 | const CRATE_ATTR_DEFAULT_SCHEMA = 1001; |
||
| 43 | |||
| 44 | const PARAM_FLOAT = 6; |
||
| 45 | const PARAM_DOUBLE = 7; |
||
| 46 | const PARAM_LONG = 8; |
||
| 47 | const PARAM_ARRAY = 9; |
||
| 48 | const PARAM_OBJECT = 10; |
||
| 49 | const PARAM_TIMESTAMP = 11; |
||
| 50 | const PARAM_IP = 12; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $attributes = [ |
||
| 56 | 'defaultFetchMode' => self::FETCH_BOTH, |
||
| 57 | 'errorMode' => self::ERRMODE_SILENT, |
||
| 58 | 'statementClass' => 'Crate\PDO\PDOStatement', |
||
| 59 | 'timeout' => 0.0, |
||
| 60 | 'auth' => [], |
||
| 61 | 'defaultSchema' => 'doc' |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Http\ClientInterface |
||
| 66 | */ |
||
| 67 | private $client; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var PDOStatement|null |
||
| 71 | */ |
||
| 72 | private $lastStatement; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var callable |
||
| 76 | */ |
||
| 77 | private $request; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritDoc} |
||
| 81 | * |
||
| 82 | * @param string $dsn The HTTP endpoint to call |
||
| 83 | * @param null $username Unused |
||
| 84 | * @param null $passwd Unused |
||
| 85 | * @param null|array $options Attributes to set on the PDO |
||
| 86 | */ |
||
| 87 | 4 | public function __construct($dsn, $username, $passwd, $options) |
|
| 88 | { |
||
| 89 | 4 | foreach (ArrayUtils::toArray($options) as $attribute => $value) { |
|
| 90 | 1 | $this->setAttribute($attribute, $value); |
|
| 91 | 4 | } |
|
| 92 | |||
| 93 | 4 | $dsnParts = self::parseDSN($dsn); |
|
| 94 | 4 | $servers = self::serversFromDsnParts($dsnParts); |
|
| 95 | |||
| 96 | 4 | $this->client = new Http\Client($servers, [ |
|
| 97 | 4 | 'timeout' => $this->attributes['timeout'] |
|
| 98 | 4 | ]); |
|
| 99 | |||
| 100 | 4 | if (!empty($username) && !empty($passwd)) { |
|
| 101 | 1 | $this->setAttribute(PDO::CRATE_ATTR_HTTP_BASIC_AUTH, [$username, $passwd]); |
|
| 102 | 1 | } |
|
| 103 | |||
| 104 | 4 | if (!empty($dsnParts[1])) { |
|
| 105 | $this->setAttribute(PDO::CRATE_ATTR_DEFAULT_SCHEMA, $dsnParts[1]); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Define a callback that will be used in the PDOStatements |
||
| 109 | // This way we don't expose this as a public api to the end users. |
||
| 110 | $this->request = function (PDOStatement $statement, $sql, array $parameters) { |
||
| 111 | |||
| 112 | $this->lastStatement = $statement; |
||
| 113 | |||
| 114 | try { |
||
|
|
|||
| 115 | |||
| 116 | return $this->client->execute($sql, $parameters); |
||
| 117 | |||
| 118 | } catch (Exception\RuntimeException $e) { |
||
| 119 | |||
| 120 | if ($this->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_EXCEPTION) { |
||
| 121 | throw new Exception\PDOException($e->getMessage(), $e->getCode()); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($this->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_WARNING) { |
||
| 125 | trigger_error(sprintf('[%d] %s', $e->getCode(), $e->getMessage()), E_USER_WARNING); |
||
| 126 | } |
||
| 127 | |||
| 128 | // should probably wrap this in a error object ? |
||
| 129 | return [ |
||
| 130 | 'code' => $e->getCode(), |
||
| 131 | 'message' => $e->getMessage() |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | }; |
||
| 135 | 4 | } |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Extract servers and optional custom schema from DSN string |
||
| 139 | * |
||
| 140 | * @param string $dsn The DSN string |
||
| 141 | * |
||
| 142 | * @return array An array of ['host:post,host:port,...', 'schema'] |
||
| 143 | */ |
||
| 144 | 36 | private static function parseDSN($dsn) |
|
| 145 | { |
||
| 146 | 36 | $matches = array(); |
|
| 147 | |||
| 148 | 36 | if (!preg_match(static::DSN_REGEX, $dsn, $matches)) { |
|
| 149 | 4 | throw new PDOException(sprintf('Invalid DSN %s', $dsn)); |
|
| 150 | } |
||
| 151 | |||
| 152 | 32 | return array_slice($matches, 1); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Extract host:port pairs out of the DSN parts |
||
| 157 | * |
||
| 158 | * @param array $dsnParts The parts of the parsed DSN string |
||
| 159 | * |
||
| 160 | * @return array An array of host:port strings |
||
| 161 | */ |
||
| 162 | 31 | private static function serversFromDsnParts($dsnParts) |
|
| 163 | { |
||
| 164 | 31 | return explode(',', trim($dsnParts[0], ',')); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritDoc} |
||
| 169 | */ |
||
| 170 | 1 | public function prepare($statement, $options = null) |
|
| 171 | { |
||
| 172 | 1 | $options = ArrayUtils::toArray($options); |
|
| 173 | |||
| 174 | 1 | if (isset($options[PDO::ATTR_CURSOR])) { |
|
| 175 | trigger_error(sprintf('%s not supported', __METHOD__), E_USER_WARNING); |
||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | 1 | return new PDOStatement($this, $this->request, $statement, $options); |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritDoc} |
||
| 184 | */ |
||
| 185 | 1 | public function beginTransaction() |
|
| 186 | { |
||
| 187 | 1 | return true; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritDoc} |
||
| 192 | */ |
||
| 193 | 1 | public function commit() |
|
| 194 | { |
||
| 195 | 1 | return true; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritDoc} |
||
| 200 | */ |
||
| 201 | 1 | public function rollBack() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritDoc} |
||
| 208 | */ |
||
| 209 | 1 | public function inTransaction() |
|
| 210 | { |
||
| 211 | 1 | return false; |
|
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritDoc} |
||
| 216 | */ |
||
| 217 | public function exec($statement) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritDoc} |
||
| 227 | */ |
||
| 228 | public function query($statement) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritDoc} |
||
| 238 | */ |
||
| 239 | 1 | public function lastInsertId($name = null) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritDoc} |
||
| 246 | */ |
||
| 247 | public function errorCode() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritDoc} |
||
| 254 | */ |
||
| 255 | public function errorInfo() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritDoc} |
||
| 262 | */ |
||
| 263 | 6 | public function setAttribute($attribute, $value) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritDoc} |
||
| 303 | */ |
||
| 304 | 9 | public function getAttribute($attribute) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritDoc} |
||
| 355 | */ |
||
| 356 | 4 | public function quote($string, $parameter_type = PDO::PARAM_STR) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritDoc} |
||
| 381 | */ |
||
| 382 | 1 | public static function getAvailableDrivers() |
|
| 386 | |||
| 387 | public function getServerVersion() |
||
| 393 | |||
| 394 | public function getServerInfo() |
||
| 398 | } |
||
| 399 |