Complex classes like Db 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 Db, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Db |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Database handle/connection. |
||
| 26 | * |
||
| 27 | * @var PDO |
||
| 28 | */ |
||
| 29 | protected $pdo; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $dsn; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $username; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $password; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $options; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $hasActiveTransaction = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @param string|PDO $hostDsnOrPdo A MySQL host, a dsn or an existing PDO instance. |
||
| 60 | * @param string|null $username |
||
| 61 | * @param string|null $password |
||
| 62 | * @param string|null $database |
||
| 63 | * @param array $options |
||
| 64 | */ |
||
| 65 | 19 | public function __construct( |
|
| 66 | $hostDsnOrPdo, |
||
| 67 | $username = null, |
||
| 68 | $password = null, |
||
| 69 | $database = null, |
||
| 70 | array $options = [] |
||
| 71 | ) { |
||
| 72 | 19 | if ($hostDsnOrPdo instanceof PDO) { |
|
| 73 | 19 | $this->pdo = $hostDsnOrPdo; |
|
| 74 | 19 | } elseif (strpos($hostDsnOrPdo, ':') !== false) { |
|
| 75 | $this->dsn = $hostDsnOrPdo; |
||
| 76 | } else { |
||
| 77 | $this->dsn = "mysql:host={$hostDsnOrPdo}" . ($database ? ";dbname={$database}" : ''); |
||
| 78 | } |
||
| 79 | |||
| 80 | 19 | $this->username = $username; |
|
| 81 | 19 | $this->password = $password; |
|
| 82 | 19 | $this->options = $options; |
|
| 83 | 19 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | */ |
||
| 87 | 13 | public function connect() |
|
| 88 | { |
||
| 89 | 13 | if ($this->isConnected()) { |
|
| 90 | 13 | return; |
|
| 91 | } |
||
| 92 | |||
| 93 | $retries = isset($this->options['connectRetries'])? $this->options['connectRetries'] : 0; |
||
| 94 | do { |
||
| 95 | try { |
||
| 96 | $defaultPdoOptions = [ |
||
| 97 | PDO::ATTR_TIMEOUT => 5, |
||
| 98 | // We want emulation by default (faster for single queries). Disable if you want to |
||
| 99 | // use proper native prepared statements |
||
| 100 | PDO::ATTR_EMULATE_PREPARES => true, |
||
| 101 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 102 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
||
| 103 | ]; |
||
| 104 | $pdoOptions = $defaultPdoOptions + (isset($this->options['pdo']) ? $this->options['pdo'] : []); |
||
| 105 | |||
| 106 | $this->pdo = new PDO( |
||
| 107 | $this->dsn, |
||
| 108 | $this->username, |
||
| 109 | $this->password, |
||
| 110 | $pdoOptions |
||
| 111 | ); |
||
| 112 | |||
| 113 | return; |
||
| 114 | } catch (PDOException $e) { |
||
| 115 | if ($this->isConnectionExceptionCausedByConnection($e) && $retries > 0) { |
||
| 116 | // Sleep for 100 - 500 ms until next retry |
||
| 117 | usleep(rand(100000, 500000)); |
||
| 118 | } else { |
||
| 119 | throw new ConnectionException($e); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | } while ($retries-- > 0); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param PDOException $exception |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
||
| 131 | { |
||
| 132 | return in_array($exception->getCode(), [ |
||
| 133 | 2002, // Can't connect to MySQL server (Socket) |
||
| 134 | 2003, // Can't connect to MySQL server (TCP) |
||
| 135 | 2006, // MySQL server has gone away |
||
| 136 | 2013, // Lost connection to MySQL server during query |
||
| 137 | ]); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Close the database connection. |
||
| 142 | */ |
||
| 143 | 1 | public function disconnect() |
|
| 144 | { |
||
| 145 | 1 | $this->pdo = null; |
|
| 146 | 1 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | */ |
||
| 150 | 1 | public function reconnect() |
|
| 151 | { |
||
| 152 | 1 | $this->disconnect(); |
|
| 153 | 1 | $this->connect(); |
|
| 154 | 1 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if database connection is open. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 14 | public function isConnected() |
|
| 162 | { |
||
| 163 | 14 | return ($this->pdo instanceof PDO); |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the PDO handle. |
||
| 168 | * |
||
| 169 | * Can be used to gain access to any special PDO methods. |
||
| 170 | * |
||
| 171 | * @return PDO |
||
| 172 | */ |
||
| 173 | 2 | public function getPdo() |
|
| 174 | { |
||
| 175 | 2 | return $this->pdo; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Creates and executes a PDO statement. |
||
| 180 | * |
||
| 181 | * @param string $sql |
||
| 182 | * @param array $parameters |
||
| 183 | * @return PDOStatement |
||
| 184 | */ |
||
| 185 | 5 | protected function executeQuery($sql, array $parameters = []) |
|
| 186 | { |
||
| 187 | 5 | $this->connect(); |
|
| 188 | |||
| 189 | try { |
||
| 190 | 5 | $pdoStatement = $this->pdo->prepare($sql); |
|
| 191 | 4 | $pdoStatement->execute($this->prepareParameters($parameters)); |
|
| 192 | |||
| 193 | 4 | return $pdoStatement; |
|
| 194 | 1 | } catch (PDOException $e) { |
|
| 195 | 1 | throw new QueryException($e, $sql, $parameters); |
|
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Execute an SQL statement and return the number of affected rows. |
||
| 201 | * |
||
| 202 | * @param string $sql |
||
| 203 | * @param array $parameters |
||
| 204 | * @return int The number of rows affected |
||
| 205 | */ |
||
| 206 | 2 | public function exec($sql, array $parameters = []) |
|
| 207 | { |
||
| 208 | 2 | $statement = $this->executeQuery($sql, $parameters); |
|
| 209 | |||
| 210 | 1 | return $statement->rowCount(); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Execute an SQL statement and return the first row. |
||
| 215 | * |
||
| 216 | * @param string $sql |
||
| 217 | * @param array $parameters |
||
| 218 | * @param bool $indexedKeys |
||
| 219 | * @return array|false |
||
| 220 | */ |
||
| 221 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
| 222 | { |
||
| 223 | 1 | $statement = $this->executeQuery($sql, $parameters); |
|
| 224 | |||
| 225 | 1 | return $statement->fetch($indexedKeys ? PDO::FETCH_NUM : PDO::FETCH_ASSOC); |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Execute an SQL statement and return all rows as an array. |
||
| 230 | * |
||
| 231 | * @param string $sql |
||
| 232 | * @param array $parameters |
||
| 233 | * @param bool $indexedKeys |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 1 | public function fetchRows($sql, array $parameters = [], $indexedKeys = false) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Execute an SQL statement and return the first column of the first row. |
||
| 245 | * |
||
| 246 | * @param string $sql |
||
| 247 | * @param array $parameters |
||
| 248 | * @return string|false |
||
| 249 | */ |
||
| 250 | 1 | public function fetchValue($sql, array $parameters = []) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
| 259 | * |
||
| 260 | * @param mixed $value |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | 1 | public function quote($value) |
|
| 264 | { |
||
| 265 | 1 | $this->connect(); |
|
| 266 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Get id of the last inserted row. |
||
| 272 | * |
||
| 273 | * @return int |
||
| 274 | */ |
||
| 275 | 1 | public function getLastInsertId() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Prepare parameters for database use. |
||
| 284 | * |
||
| 285 | * @param array $parameters |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 4 | protected function prepareParameters(array $parameters = []) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Begin transaction (turns off autocommit mode). |
||
| 306 | 4 | * |
|
| 307 | * @param bool $onlyIfNoActiveTransaction |
||
| 308 | 4 | * @return bool |
|
| 309 | */ |
||
| 310 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
| 323 | 2 | ||
| 324 | /** |
||
| 325 | 2 | * Commits current active transaction (restores autocommit mode). |
|
| 326 | */ |
||
| 327 | 2 | public function commit() |
|
| 334 | 2 | ||
| 335 | /** |
||
| 336 | 2 | * Rolls back current active transaction (restores autocommit mode). |
|
| 337 | */ |
||
| 338 | 2 | public function rollBack() |
|
| 345 | 3 | ||
| 346 | /** |
||
| 347 | 3 | * @return bool |
|
| 348 | */ |
||
| 349 | public function hasActiveTransaction() |
||
| 353 | |||
| 354 | /** |
||
| 355 | 1 | * @param string $table |
|
| 356 | * @param array $data |
||
| 357 | 1 | * @return int The number of rows affected |
|
| 358 | 1 | */ |
|
| 359 | public function insert($table, array $data) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $table |
||
| 374 | * @param array $data |
||
| 375 | 1 | * @param string $whereSql |
|
| 376 | * @param array $whereParameters |
||
| 377 | 1 | * @return int The number of rows affected |
|
| 378 | 1 | */ |
|
| 379 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
| 395 | } |
||
| 396 |