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( |
|
| 84 | |||
| 85 | /** |
||
| 86 | */ |
||
| 87 | 13 | public function connect() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param PDOException $exception |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | private function isConnectionExceptionCausedByConnection(PDOException $exception) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Close the database connection. |
||
| 142 | */ |
||
| 143 | 1 | public function disconnect() |
|
| 147 | |||
| 148 | /** |
||
| 149 | */ |
||
| 150 | 1 | public function reconnect() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if database connection is open. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 14 | public function isConnected() |
|
| 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() |
|
| 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 = []) |
|
| 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 = []) |
|
| 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) |
|
| 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) |
|
| 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 = []) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Begin transaction (turns off autocommit mode). |
||
| 302 | * |
||
| 303 | * @param bool $onlyIfNoActiveTransaction |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 4 | public function beginTransaction($onlyIfNoActiveTransaction = false) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Commits current active transaction (restores autocommit mode). |
||
| 322 | */ |
||
| 323 | 2 | public function commit() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Rolls back current active transaction (restores autocommit mode). |
||
| 333 | */ |
||
| 334 | 2 | public function rollBack() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 3 | public function hasActiveTransaction() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $table |
||
| 352 | * @param array $data |
||
| 353 | * @return int The number of rows affected |
||
| 354 | */ |
||
| 355 | 1 | public function insert($table, array $data) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $table |
||
| 370 | * @param array $data |
||
| 371 | * @param string $whereSql |
||
| 372 | * @param array $whereParameters |
||
| 373 | * @return int The number of rows affected |
||
| 374 | */ |
||
| 375 | 1 | public function update($table, array $data, $whereSql = '', array $whereParameters = []) |
|
| 391 | } |
||
| 392 |