Complex classes like PdoAdapter 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 PdoAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | abstract class PdoAdapter extends AbstractAdapter |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var \PDO|null |
||
| 44 | */ |
||
| 45 | protected $connection; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | public function setOptions(array $options) |
||
| 51 | 287 | { |
|
| 52 | parent::setOptions($options); |
||
| 53 | 287 | ||
| 54 | if (isset($options['connection'])) { |
||
| 55 | 287 | $this->setConnection($options['connection']); |
|
| 56 | 3 | } |
|
| 57 | 3 | ||
| 58 | return $this; |
||
| 59 | 287 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Sets the database connection. |
||
| 63 | * |
||
| 64 | * @param \PDO $connection Connection |
||
| 65 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
| 66 | */ |
||
| 67 | public function setConnection(\PDO $connection) |
||
| 68 | 193 | { |
|
| 69 | $this->connection = $connection; |
||
| 70 | 193 | ||
| 71 | // Create the schema table if it doesn't already exist |
||
| 72 | if (!$this->hasSchemaTable()) { |
||
| 73 | 193 | $this->createSchemaTable(); |
|
| 74 | 191 | } else { |
|
| 75 | 191 | $table = new Table($this->getSchemaTableName(), [], $this); |
|
| 76 | 74 | if (!$table->hasColumn('migration_name')) { |
|
| 77 | 74 | $table |
|
| 78 | ->addColumn( |
||
| 79 | 'migration_name', |
||
| 80 | 'string', |
||
| 81 | ['limit' => 100, 'after' => 'version', 'default' => null, 'null' => true] |
||
| 82 | ) |
||
| 83 | ->save(); |
||
| 84 | } |
||
| 85 | if (!$table->hasColumn('breakpoint')) { |
||
| 86 | 74 | $table |
|
| 87 | ->addColumn('breakpoint', 'boolean', ['default' => false]) |
||
| 88 | ->save(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this; |
||
| 93 | 193 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Gets the database connection |
||
| 97 | * |
||
| 98 | * @return \PDO |
||
| 99 | */ |
||
| 100 | public function getConnection() |
||
| 101 | 191 | { |
|
| 102 | if ($this->connection === null) { |
||
| 103 | 191 | $this->connect(); |
|
| 104 | 189 | } |
|
| 105 | 189 | ||
| 106 | 191 | return $this->connection; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | 1 | public function connect() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function disconnect() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 218 | public function execute($sql) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Executes a query and returns PDOStatement. |
||
| 139 | * |
||
| 140 | * @param string $sql SQL |
||
| 141 | * @return \PDOStatement |
||
| 142 | 220 | */ |
|
| 143 | public function query($sql) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | 151 | */ |
|
| 151 | public function fetchRow($sql) |
||
| 157 | |||
| 158 | /** |
||
| 159 | 213 | * {@inheritdoc} |
|
| 160 | */ |
||
| 161 | 213 | public function fetchAll($sql) |
|
| 171 | |||
| 172 | 1 | /** |
|
| 173 | * {@inheritdoc} |
||
| 174 | 1 | */ |
|
| 175 | 1 | public function insert(Table $table, $row) |
|
| 183 | 1 | ||
| 184 | 1 | /** |
|
| 185 | 1 | * Inserts data into the table. |
|
| 186 | * |
||
| 187 | * @param Table $table The table where to insert the data |
||
| 188 | * @param array $row The data to be inserted as assoc array column name -> value |
||
| 189 | */ |
||
| 190 | 11 | private function doInsert(Table $table, $row) |
|
| 204 | 11 | ||
| 205 | 11 | /** |
|
| 206 | 11 | * Dumps the insert SQL statement. |
|
| 207 | * |
||
| 208 | 11 | * @param Table $table The table where to insert the data |
|
| 209 | 11 | * @param array $row The data to be inserted as assoc array column name -> value |
|
| 210 | */ |
||
| 211 | 11 | private function dryRunInsert(Table $table, $row) |
|
| 224 | 5 | ||
| 225 | /** |
||
| 226 | 5 | * Quotes a database value. |
|
| 227 | * |
||
| 228 | * @param mixed $value |
||
| 229 | * @return mixed |
||
| 230 | */ |
||
| 231 | private function quoteValue($value) |
||
| 243 | 1 | ||
| 244 | 1 | /** |
|
| 245 | 8 | * {@inheritdoc} |
|
| 246 | */ |
||
| 247 | 7 | public function bulkinsert(Table $table, $rows) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Bulk inserts data into the table. |
||
| 258 | 5 | * |
|
| 259 | * @param Table $table The table where to insert the data |
||
| 260 | 5 | * @param array $rows The data as array |
|
| 261 | */ |
||
| 262 | 5 | private function doBulkinsert(Table $table, $rows) |
|
| 290 | 5 | ||
| 291 | /** |
||
| 292 | * Dump the bulk insert statement. |
||
| 293 | * |
||
| 294 | * @param Table $table The table where to insert the data |
||
| 295 | * @param array $rows The data as array |
||
| 296 | 1 | */ |
|
| 297 | private function dryRunBulkInsert(Table $table, $rows) |
||
| 323 | 1 | ||
| 324 | 1 | /** |
|
| 325 | 1 | * {@inheritdoc} |
|
| 326 | 1 | */ |
|
| 327 | 1 | public function getVersions() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function getVersionLog() |
||
| 338 | { |
||
| 339 | $result = []; |
||
| 340 | |||
| 341 | switch ($this->options['version_order']) { |
||
| 342 | case \Phinx\Config\Config::VERSION_ORDER_CREATION_TIME: |
||
| 343 | $orderBy = 'version ASC'; |
||
| 344 | break; |
||
| 345 | case \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME: |
||
| 346 | $orderBy = 'start_time ASC, version ASC'; |
||
| 347 | break; |
||
| 348 | default: |
||
| 349 | 208 | throw new \RuntimeException('Invalid version_order configuration option'); |
|
| 350 | } |
||
| 351 | |||
| 352 | 208 | $rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->getSchemaTableName(), $orderBy)); |
|
| 353 | 208 | foreach ($rows as $version) { |
|
| 354 | 208 | $result[$version['version']] = $version; |
|
| 355 | 208 | } |
|
| 356 | 208 | ||
| 357 | 208 | return $result; |
|
| 358 | 208 | } |
|
| 359 | 208 | ||
| 360 | 208 | /** |
|
| 361 | 208 | * {@inheritdoc} |
|
| 362 | 208 | */ |
|
| 363 | 208 | public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @inheritDoc |
||
| 400 | */ |
||
| 401 | public function toggleBreakpoint(MigrationInterface $migration) |
||
| 402 | { |
||
| 403 | $this->query( |
||
| 404 | sprintf( |
||
| 405 | 'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END, %7$s = %7$s WHERE %5$s = \'%6$s\';', |
||
| 406 | $this->getSchemaTableName(), |
||
| 407 | $this->quoteColumnName('breakpoint'), |
||
| 408 | $this->castToBool(true), |
||
| 409 | $this->castToBool(false), |
||
| 410 | $this->quoteColumnName('version'), |
||
| 411 | $migration->getVersion(), |
||
| 412 | $this->quoteColumnName('start_time') |
||
| 413 | ) |
||
| 414 | ); |
||
| 415 | |||
| 416 | return $this; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @inheritDoc |
||
| 421 | */ |
||
| 422 | public function resetAllBreakpoints() |
||
| 423 | { |
||
| 424 | return $this->execute( |
||
| 425 | sprintf( |
||
| 426 | 'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %2$s <> %3$s;', |
||
| 427 | $this->getSchemaTableName(), |
||
| 428 | $this->quoteColumnName('breakpoint'), |
||
| 429 | $this->castToBool(false), |
||
| 430 | $this->quoteColumnName('start_time') |
||
| 431 | ) |
||
| 432 | ); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritdoc} |
||
| 437 | */ |
||
| 438 | public function createSchema($schemaName = 'public') |
||
| 442 | |||
| 443 | /** |
||
| 444 | * {@inheritdoc} |
||
| 445 | */ |
||
| 446 | public function dropSchema($name) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | public function getColumnTypes() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function castToBool($value) |
||
| 485 | { |
||
| 486 | return (bool)$value ? 1 : 0; |
||
| 487 | } |
||
| 488 | } |
||
| 489 |