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 |
||
| 30 | class Pdo implements StorageInterface |
||
| 31 | { |
||
| 32 | use DataTypeCasterTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var PhpPdo |
||
| 36 | */ |
||
| 37 | protected $conn; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var PdoConfig |
||
| 41 | */ |
||
| 42 | protected $config; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $quoteMaps = array('mysql' => '`', 'pgsql' => '"'); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $quote = '`'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $transactionalLevel = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $transactional = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor. |
||
| 66 | * |
||
| 67 | * @param PdoConfig $config |
||
| 68 | */ |
||
| 69 | public function __construct(PdoConfig $config) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | public function save(ReadModelInterface $model, $table) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function remove($id, $table) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function findById($id, $table, $class) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function finder($table, $class) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Execute query without statement. |
||
| 135 | * |
||
| 136 | * @param string $sql |
||
| 137 | * @param array $values |
||
| 138 | */ |
||
| 139 | public function exec($sql, array $values = array()) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Execute query with statement. |
||
| 160 | * |
||
| 161 | * @param string $sql |
||
| 162 | * @param int $mode |
||
| 163 | * |
||
| 164 | * @return PDOStatement |
||
| 165 | */ |
||
| 166 | public function query($sql, $mode = PhpPdo::FETCH_ASSOC) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getTransactionalLevel() |
||
| 179 | { |
||
| 180 | return $this->transactionalLevel; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | public function isTransactional() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Begin transaction |
||
| 193 | */ |
||
| 194 | public function beginTransaction() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Rollback transaction |
||
| 206 | */ |
||
| 207 | public function rollback() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Commit transaction. |
||
| 218 | */ |
||
| 219 | public function commit() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Compute fields to expressions. |
||
| 232 | * |
||
| 233 | * @param array $fields |
||
| 234 | * @param PdoFinder $finder |
||
| 235 | * |
||
| 236 | * @return PdoCompositeExpression |
||
| 237 | */ |
||
| 238 | protected function computeFieldsExpression(array $fields, PdoFinder $finder) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param array $values |
||
| 250 | * @param string $class |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected function normalizeRelation(array $values, $class) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Normalize value. |
||
| 291 | * |
||
| 292 | * @param array $values |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | protected function normalize(array $values) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Build query update. |
||
| 320 | * |
||
| 321 | * @param array $values |
||
| 322 | * @param array $conditions |
||
| 323 | * @param string $table |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | protected function buildUpdateQuery(array $values, array $conditions, $table) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Build insert query. |
||
| 340 | * |
||
| 341 | * @param array $values |
||
| 342 | * @param string $table |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function buildInsertQuery(array $values, $table) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Build data sets. |
||
| 358 | * |
||
| 359 | * @param array $parts |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function buildDataSets(array $parts) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Quote field. |
||
| 375 | * |
||
| 376 | * @param string $field |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | protected function quote($field) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param mixed $value |
||
| 387 | * |
||
| 388 | * @return mixed|null|string |
||
| 389 | */ |
||
| 390 | protected function resolveValue($value) |
||
| 402 | } |
||
| 403 |