Complex classes like MongoDbAdapter 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 MongoDbAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MongoDbAdapter 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 | public 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) |
||
| 274 | |||
| 275 | public function getQueryBuilder() |
||
| 279 | |||
| 280 | public function query($sql) |
||
| 284 | |||
| 285 | public function fetchRow($sql) |
||
| 289 | |||
| 290 | public function fetchAll($sql) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param Table $table |
||
| 297 | * @param array $row |
||
| 298 | */ |
||
| 299 | public function insert(Table $table, $row) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param Table $table |
||
| 306 | * @param array $rows |
||
| 307 | */ |
||
| 308 | public function bulkinsert(Table $table, $rows) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $tableName |
||
| 315 | * @return mixed|string |
||
| 316 | */ |
||
| 317 | public function quoteTableName($tableName) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $columnName |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function quoteColumnName($columnName) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $tableName |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function hasTable($tableName) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param Table $table |
||
| 342 | * @param array $columns |
||
| 343 | * @param array $indexes |
||
| 344 | */ |
||
| 345 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $tableName |
||
| 351 | */ |
||
| 352 | public function truncateTable($tableName) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $tableName |
||
| 359 | * @return array|Column[] |
||
| 360 | */ |
||
| 361 | public function getColumns($tableName) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $tableName |
||
| 368 | * @param string $columnName |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function hasColumn($tableName, $columnName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $tableName |
||
| 378 | * @param mixed $columns |
||
| 379 | * @return bool|void |
||
| 380 | */ |
||
| 381 | public function hasIndex($tableName, $columns) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $tableName |
||
| 389 | * @param string $indexName |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | public function hasIndexByName($tableName, $indexName) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $tableName |
||
| 400 | * @param string[] $columns |
||
| 401 | * @param null $constraint |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | public function hasPrimaryKey($tableName, $columns, $constraint = null) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param string $tableName |
||
| 411 | * @param string[] $columns |
||
| 412 | * @param null $constraint |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | public function hasForeignKey($tableName, $columns, $constraint = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getColumnTypes() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param Column $column |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function isValidColumnType(Column $column) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $type |
||
| 445 | * @param null $limit |
||
| 446 | * @return array|string[] |
||
| 447 | */ |
||
| 448 | public function getSqlType($type, $limit = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $name |
||
| 455 | * @param array $options |
||
| 456 | */ |
||
| 457 | public function createDatabase($name, $options = []) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $name |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | public function hasDatabase($name) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $name |
||
| 472 | */ |
||
| 473 | public function dropDatabase($name) |
||
| 477 | |||
| 478 | public function createSchema($schemaName = 'public') |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $schemaName |
||
| 485 | */ |
||
| 486 | public function dropSchema($schemaName) |
||
| 490 | |||
| 491 | public function castToBool($value) |
||
| 495 | |||
| 496 | protected function getMigrationCollection() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return Database |
||
| 503 | */ |
||
| 504 | protected function getDatabase() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param \Iterator $iterator |
||
| 511 | * @param $value |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | protected function hasValue(\Iterator $iterator, $value) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param \Iterator $iterator |
||
| 526 | * @param $values |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | protected function hasValues(\Iterator $iterator, $values) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return Client|\PDO |
||
| 541 | */ |
||
| 542 | public function getConnection() |
||
| 546 | } |
||
| 547 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: