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 | 18 | public function __construct( |
|
| 84 | |||
| 85 | /** |
||
| 86 | */ |
||
| 87 | 13 | public function connect() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param PDOException $exception |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Close database connection. |
||
| 141 | */ |
||
| 142 | 1 | public function disconnect() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Check if database connection is open. |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 14 | public function isConnected() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the PDO handle. |
||
| 159 | * |
||
| 160 | * Can be used to gain access to any special PDO methods. |
||
| 161 | * |
||
| 162 | * @return PDO |
||
| 163 | */ |
||
| 164 | 2 | public function getPdo() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Creates and executes a PDO statement. |
||
| 171 | * |
||
| 172 | * @param string $sql |
||
| 173 | * @param array $parameters |
||
| 174 | * @return PDOStatement |
||
| 175 | */ |
||
| 176 | 5 | protected function executeQuery($sql, array $parameters = []) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Execute an SQL statement and return the number of affected rows. |
||
| 192 | * |
||
| 193 | * @param string $sql |
||
| 194 | * @param array $parameters |
||
| 195 | * @return int The number of rows affected |
||
| 196 | */ |
||
| 197 | 2 | public function exec($sql, array $parameters = []) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Execute an SQL statement and return the first row. |
||
| 206 | * |
||
| 207 | * @param string $sql |
||
| 208 | * @param array $parameters |
||
| 209 | * @param bool $indexedKeys |
||
| 210 | * @return array|false |
||
| 211 | */ |
||
| 212 | 1 | public function fetchRow($sql, array $parameters = [], $indexedKeys = false) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Execute an SQL statement and return all rows as an array. |
||
| 221 | * |
||
| 222 | * @param string $sql |
||
| 223 | * @param array $parameters |
||
| 224 | * @param bool $indexedKeys |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | 1 | public function fetchAll($sql, array $parameters = [], $indexedKeys = false) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Execute an SQL statement and return the first column of the first row. |
||
| 236 | * |
||
| 237 | * @param string $sql |
||
| 238 | * @param array $parameters |
||
| 239 | * @return string|false |
||
| 240 | */ |
||
| 241 | 1 | public function fetchOne($sql, array $parameters = []) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Quote a value for a safe use in query (eg. bla'bla -> 'bla''bla'). |
||
| 250 | * |
||
| 251 | * @param mixed $value |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 1 | public function quote($value) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get id of the last inserted row. |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | 1 | public function getLastInsertId() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Prepare parameters for database use. |
||
| 275 | * |
||
| 276 | * @param array $parameters |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 4 | protected function prepareParameters(array $parameters = []) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Begin transaction (turns off autocommit mode). |
||
| 293 | * |
||
| 294 | * @param bool $onlyIfNoActiveTransaction |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Commits current active transaction (restores autocommit mode). |
||
| 313 | */ |
||
| 314 | 2 | public function commit() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Rolls back current active transaction (restores autocommit mode). |
||
| 324 | */ |
||
| 325 | 2 | public function rollBack() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | 3 | public function hasActiveTransaction() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $table |
||
| 343 | * @param array $data |
||
| 344 | * @return int The number of rows affected |
||
| 345 | */ |
||
| 346 | 1 | public function insert($table, array $data) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $table |
||
| 361 | * @param array $data |
||
| 362 | * @param string $whereSql |
||
| 363 | * @param array $whereParameters |
||
| 364 | * @return int The number of rows affected |
||
| 365 | */ |
||
| 366 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
| 382 | } |
||
| 383 |