Complex classes like MongoMigrationAdapter 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 MongoMigrationAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MongoMigrationAdapter implements PhinxAdapter |
||
| 19 | { |
||
| 20 | |||
| 21 | protected $collectionName = 'phinx_migration'; |
||
| 22 | |||
| 23 | /** @var InputInterface $consoleInput */ |
||
| 24 | protected $consoleInput; |
||
| 25 | |||
| 26 | /** @var OutputInterface $consoleOutput */ |
||
| 27 | protected $consoleOutput; |
||
| 28 | |||
| 29 | /** @var Client $mongoClient */ |
||
| 30 | protected $mongoClient; |
||
| 31 | |||
| 32 | /** @var Database $database */ |
||
| 33 | protected $database; |
||
| 34 | |||
| 35 | /** @var Collection $collection */ |
||
| 36 | protected $collection; |
||
| 37 | |||
| 38 | /** @var string $databaseName */ |
||
| 39 | protected $databaseName; |
||
| 40 | |||
| 41 | /** @var string $uri */ |
||
| 42 | protected $uri; |
||
| 43 | |||
| 44 | protected $session; |
||
| 45 | |||
| 46 | protected $options = [ |
||
| 47 | 'table_prefix' => '' |
||
| 48 | ]; |
||
| 49 | |||
| 50 | function __construct(array $options) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public function getVersions() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getVersionLog() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param array $options |
||
| 82 | * @return $this|PhinxAdapter |
||
| 83 | */ |
||
| 84 | public function setOptions(array $options) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getOptions() |
||
| 96 | |||
| 97 | public function hasOption($name) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $name |
||
| 104 | * @return mixed|null |
||
| 105 | */ |
||
| 106 | public function getOption($name) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param InputInterface $input |
||
| 116 | * @return $this|PhinxAdapter |
||
| 117 | */ |
||
| 118 | public function setInput(InputInterface $input) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return InputInterface |
||
| 126 | */ |
||
| 127 | public function getInput() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param OutputInterface $output |
||
| 134 | * @return PhinxAdapter |
||
| 135 | */ |
||
| 136 | public function setOutput(OutputInterface $output) |
||
| 141 | |||
| 142 | public function getOutput() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param MigrationInterface $migration |
||
| 149 | * @param string $direction |
||
| 150 | * @param int $startTime |
||
| 151 | * @param int $endTime |
||
| 152 | * @return $this|PhinxAdapter |
||
| 153 | */ |
||
| 154 | public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param MigrationInterface $migration |
||
| 174 | * @return $this|PhinxAdapter |
||
| 175 | */ |
||
| 176 | public function toggleBreakpoint(MigrationInterface $migration) |
||
| 180 | |||
| 181 | public function resetAllBreakpoints() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function hasSchemaTable() |
||
| 193 | |||
| 194 | public function createSchemaTable() |
||
| 198 | |||
| 199 | public function getAdapterType() |
||
| 203 | |||
| 204 | public function connect() |
||
| 210 | |||
| 211 | public function disconnect() |
||
| 215 | |||
| 216 | public function hasTransactions() |
||
| 220 | |||
| 221 | public function beginTransaction() |
||
| 225 | |||
| 226 | public function commitTransaction() |
||
| 230 | |||
| 231 | public function rollbackTransaction() |
||
| 235 | |||
| 236 | public function execute($sql) |
||
| 240 | |||
| 241 | public function executeActions(Table $table, array $actions) |
||
| 275 | |||
| 276 | public function getQueryBuilder() |
||
| 280 | |||
| 281 | public function query($sql) |
||
| 285 | |||
| 286 | public function fetchRow($sql) |
||
| 290 | |||
| 291 | public function fetchAll($sql) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param Table $table |
||
| 298 | * @param array $row |
||
| 299 | */ |
||
| 300 | public function insert(Table $table, $row) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param Table $table |
||
| 307 | * @param array $rows |
||
| 308 | */ |
||
| 309 | public function bulkinsert(Table $table, $rows) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $tableName |
||
| 316 | * @return mixed|string |
||
| 317 | */ |
||
| 318 | public function quoteTableName($tableName) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $columnName |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function quoteColumnName($columnName) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $tableName |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function hasTable($tableName) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param Table $table |
||
| 343 | * @param array $columns |
||
| 344 | * @param array $indexes |
||
| 345 | */ |
||
| 346 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $tableName |
||
| 352 | */ |
||
| 353 | public function truncateTable($tableName) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $tableName |
||
| 360 | * @return array|Column[] |
||
| 361 | */ |
||
| 362 | public function getColumns($tableName) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $tableName |
||
| 369 | * @param string $columnName |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function hasColumn($tableName, $columnName) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $tableName |
||
| 379 | * @param mixed $columns |
||
| 380 | * @return bool|void |
||
| 381 | */ |
||
| 382 | public function hasIndex($tableName, $columns) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $tableName |
||
| 390 | * @param string $indexName |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function hasIndexByName($tableName, $indexName) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $tableName |
||
| 401 | * @param string[] $columns |
||
| 402 | * @param null $constraint |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function hasPrimaryKey($tableName, $columns, $constraint = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $tableName |
||
| 412 | * @param string[] $columns |
||
| 413 | * @param null $constraint |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function hasForeignKey($tableName, $columns, $constraint = null) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function getColumnTypes() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Column $column |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function isValidColumnType(Column $column) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $type |
||
| 446 | * @param null $limit |
||
| 447 | * @return array|string[] |
||
| 448 | */ |
||
| 449 | public function getSqlType($type, $limit = null) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $name |
||
| 456 | * @param array $options |
||
| 457 | */ |
||
| 458 | public function createDatabase($name, $options = []) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $name |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function hasDatabase($name) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $name |
||
| 473 | */ |
||
| 474 | public function dropDatabase($name) |
||
| 478 | |||
| 479 | public function createSchema($schemaName = 'public') |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $schemaName |
||
| 486 | */ |
||
| 487 | public function dropSchema($schemaName) |
||
| 491 | |||
| 492 | public function castToBool($value) |
||
| 496 | |||
| 497 | protected function getMigrationCollection() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @return Database |
||
| 504 | */ |
||
| 505 | protected function getDatabase() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param \Iterator $iterator |
||
| 512 | * @param $value |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | protected function hasValue(\Iterator $iterator, $value) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param \Iterator $iterator |
||
| 527 | * @param $values |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | protected function hasValues(\Iterator $iterator, $values) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @return Client|\PDO |
||
| 542 | */ |
||
| 543 | public function getConnection() |
||
| 547 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.