Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Manager |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var \Phinx\Config\ConfigInterface |
||
| 44 | */ |
||
| 45 | protected $config; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 49 | */ |
||
| 50 | protected $input; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 54 | */ |
||
| 55 | protected $output; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $environments; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $environmentName; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $adapter; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $migrations; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $seeds; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var integer |
||
| 84 | */ |
||
| 85 | const EXIT_STATUS_DOWN = 1; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var integer |
||
| 89 | 432 | */ |
|
| 90 | const EXIT_STATUS_MISSING = 2; |
||
| 91 | 432 | ||
| 92 | 432 | /** |
|
| 93 | 432 | * Class Constructor. |
|
| 94 | 432 | * |
|
| 95 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
| 96 | * @param \Symfony\Component\Console\Input\InputInterface $input Console Input |
||
| 97 | * @param \Symfony\Component\Console\Output\OutputInterface $output Console Output |
||
| 98 | */ |
||
| 99 | public function __construct(ConfigInterface $config, InputInterface $input, OutputInterface $output) |
||
| 105 | 22 | ||
| 106 | 22 | /** |
|
| 107 | 22 | * Prints the specified environment's migration status. |
|
| 108 | 22 | * |
|
| 109 | 22 | * @param string $environment |
|
| 110 | 22 | * @param null $format |
|
| 111 | * @return int 0 if all migrations are up, or an error code |
||
| 112 | */ |
||
| 113 | 21 | public function printStatus($environment, $format = null) |
|
| 261 | 10 | ||
| 262 | 10 | /** |
|
| 263 | 10 | * Print Missing Version |
|
| 264 | 10 | * |
|
| 265 | 10 | * @param array $version The missing version to print (in the format returned by Environment.getVersionLog). |
|
| 266 | * @param int $maxNameLength The maximum migration name length. |
||
| 267 | 10 | */ |
|
| 268 | 1 | private function printMissingVersion($version, $maxNameLength) |
|
| 269 | 1 | { |
|
| 270 | 10 | $this->getOutput()->writeln(sprintf( |
|
| 271 | ' <error>up</error> %14.0f %19s %19s <comment>%s</comment> <error>** MISSING **</error>', |
||
| 272 | $version['version'], |
||
| 273 | $version['start_time'], |
||
| 274 | $version['end_time'], |
||
| 275 | str_pad($version['migration_name'], $maxNameLength, ' ') |
||
| 276 | )); |
||
| 277 | |||
| 278 | if ($version && $version['breakpoint']) { |
||
| 279 | $this->getOutput()->writeln(' <error>BREAKPOINT SET</error>'); |
||
| 280 | 4 | } |
|
| 281 | } |
||
| 282 | 4 | ||
| 283 | 4 | /** |
|
| 284 | * Migrate to the version of the database on a given date. |
||
| 285 | * |
||
| 286 | 4 | * @param string $environment Environment |
|
| 287 | 4 | * @param \DateTime $dateTime Date to migrate to |
|
| 288 | * |
||
| 289 | 4 | * @return void |
|
| 290 | 3 | */ |
|
| 291 | 3 | public function migrateToDateTime($environment, \DateTime $dateTime) |
|
| 306 | 8 | ||
| 307 | 8 | /** |
|
| 308 | 8 | * Migrate an environment to the specified version. |
|
| 309 | * |
||
| 310 | 8 | * @param string $environment Environment |
|
| 311 | * @param int $version |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | 8 | public function migrate($environment, $version = null) |
|
| 367 | |||
| 368 | 119 | /** |
|
| 369 | 119 | * Execute a migration against the specified environment. |
|
| 370 | 119 | * |
|
| 371 | * @param string $name Environment Name |
||
| 372 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
| 373 | 119 | * @param string $direction Direction |
|
| 374 | 119 | * @return void |
|
| 375 | 119 | */ |
|
| 376 | public function executeMigration($name, MigrationInterface $migration, $direction = MigrationInterface::UP) |
||
| 397 | 6 | ||
| 398 | 6 | /** |
|
| 399 | 6 | * Execute a seeder against the specified environment. |
|
| 400 | * |
||
| 401 | * @param string $name Environment Name |
||
| 402 | 6 | * @param \Phinx\Seed\SeedInterface $seed Seed |
|
| 403 | 6 | * @return void |
|
| 404 | 6 | */ |
|
| 405 | public function executeSeed($name, SeedInterface $seed) |
||
| 427 | |||
| 428 | /** |
||
| 429 | 349 | * Rollback an environment to the specified version. |
|
| 430 | * |
||
| 431 | * @param string $environment Environment |
||
| 432 | 349 | * @param int|string $target |
|
| 433 | * @param bool $force |
||
| 434 | 349 | * @param bool $targetMustMatchVersion |
|
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | 349 | public function rollback($environment, $target = null, $force = false, $targetMustMatchVersion = true) |
|
| 537 | 3 | ||
| 538 | 3 | /** |
|
| 539 | 3 | * Run database seeders against an environment. |
|
| 540 | * |
||
| 541 | 6 | * @param string $environment Environment |
|
| 542 | 3 | * @param string $seed Seeder |
|
| 543 | 3 | * @return void |
|
| 544 | 3 | */ |
|
| 545 | public function seed($environment, $seed = null) |
||
| 568 | 382 | ||
| 569 | /** |
||
| 570 | 382 | * Set the dbRef property |
|
| 571 | 380 | * |
|
| 572 | * @param string $ref |
||
| 573 | * @return \Phinx\Migration\Manager |
||
| 574 | */ |
||
| 575 | 7 | public function setDbRef($ref) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Get the adapter property |
||
| 583 | 6 | * |
|
| 584 | 6 | * @return string |
|
| 585 | */ |
||
| 586 | 6 | public function getDbRef() |
|
| 590 | |||
| 591 | 6 | /** |
|
| 592 | * Sets the environments. |
||
| 593 | * |
||
| 594 | * @param array $environments Environments |
||
| 595 | * @return \Phinx\Migration\Manager |
||
| 596 | */ |
||
| 597 | public function setEnvironments($environments = []) |
||
| 603 | 400 | ||
| 604 | /** |
||
| 605 | * Gets the manager class for the given environment. |
||
| 606 | * |
||
| 607 | * @param string $name Environment Name |
||
| 608 | * @throws \InvalidArgumentException |
||
| 609 | * @return \Phinx\Migration\Manager\Environment |
||
| 610 | */ |
||
| 611 | 393 | public function getEnvironment($name) |
|
| 619 | |||
| 620 | public function setEnvironmentName($name) |
||
| 625 | 400 | ||
| 626 | public function getEnvironmentName() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Gets the manager class for the given environment when given as a single instance. |
||
| 633 | 395 | * |
|
| 634 | * @param string $name Environment Name |
||
| 635 | 395 | * @throws \InvalidArgumentException |
|
| 636 | * @return \Phinx\Migration\Manager\Environment |
||
| 637 | */ |
||
| 638 | public function getSingleEnvironment($name) |
||
| 666 | |||
| 667 | 388 | /** |
|
| 668 | 387 | * Gets the manager class for the given environment when used as part of a mulitple deployment. |
|
| 669 | 387 | * |
|
| 670 | * @param string $name Environment Name |
||
| 671 | 387 | * @throws \InvalidArgumentException |
|
| 672 | 3 | * @return \Phinx\Migration\Manager\Environment |
|
| 673 | */ |
||
| 674 | public function getMultiEnvironment($name) |
||
| 702 | |||
| 703 | 385 | /** |
|
| 704 | * Sets the console input. |
||
| 705 | 385 | * |
|
| 706 | 2 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
|
| 707 | 2 | * @return \Phinx\Migration\Manager |
|
| 708 | 2 | */ |
|
| 709 | public function setInput(InputInterface $input) |
||
| 710 | 2 | { |
|
| 711 | $this->input = $input; |
||
| 712 | |||
| 713 | 383 | return $this; |
|
| 714 | 383 | } |
|
| 715 | 384 | ||
| 716 | /** |
||
| 717 | 379 | * Gets the console input. |
|
| 718 | 379 | * |
|
| 719 | 379 | * @return \Symfony\Component\Console\Input\InputInterface |
|
| 720 | */ |
||
| 721 | 379 | public function getInput() |
|
| 722 | { |
||
| 723 | return $this->input; |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Sets the console output. |
||
| 728 | * |
||
| 729 | 388 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
|
| 730 | * @return \Phinx\Migration\Manager |
||
| 731 | 388 | */ |
|
| 732 | 388 | public function setOutput(OutputInterface $output) |
|
| 738 | 388 | ||
| 739 | 388 | /** |
|
| 740 | 388 | * Gets the console output. |
|
| 741 | * |
||
| 742 | 388 | * @return \Symfony\Component\Console\Output\OutputInterface |
|
| 743 | */ |
||
| 744 | public function getOutput() |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Sets the database migrations. |
||
| 751 | 11 | * |
|
| 752 | * @param array $migrations Migrations |
||
| 753 | 11 | * @return \Phinx\Migration\Manager |
|
| 754 | 11 | */ |
|
| 755 | public function setMigrations(array $migrations = null) |
||
| 761 | |||
| 762 | /** |
||
| 763 | 11 | * Gets an array of the database migrations, indexed by migration name (aka creation time) and sorted in ascending |
|
| 764 | * order |
||
| 765 | 11 | * |
|
| 766 | 11 | * @throws \InvalidArgumentException |
|
| 767 | * @return \Phinx\Migration\AbstractMigration[] |
||
| 768 | */ |
||
| 769 | 11 | public function getMigrations() |
|
| 834 | |||
| 835 | /** |
||
| 836 | * Returns a list of migration files found in the provided migration paths. |
||
| 837 | * |
||
| 838 | * @return string[] |
||
| 839 | */ |
||
| 840 | View Code Duplication | protected function getMigrationFiles() |
|
| 841 | { |
||
| 842 | 400 | $config = $this->getConfig(); |
|
| 843 | $paths = $config->getMigrationPaths($this->getEnvironmentName(), $this->getDbRef()); |
||
| 844 | 400 | $files = []; |
|
| 845 | 400 | ||
| 846 | foreach ($paths as $path) { |
||
| 847 | $files = array_merge( |
||
| 848 | $files, |
||
| 849 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
| 850 | ); |
||
| 851 | } |
||
| 852 | // glob() can return the same file multiple times |
||
| 853 | 399 | // This will cause the migration to fail with a |
|
| 854 | // false assumption of duplicate migrations |
||
| 855 | 399 | // http://php.net/manual/en/function.glob.php#110340 |
|
| 856 | $files = array_unique($files); |
||
| 857 | |||
| 858 | return $files; |
||
| 859 | } |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Sets the database seeders. |
||
| 863 | * |
||
| 864 | * @param array $seeds Seeders |
||
| 865 | 2 | * @return \Phinx\Migration\Manager |
|
| 866 | */ |
||
| 867 | 2 | public function setSeeds(array $seeds = null) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Gets an array of database seeders. |
||
| 876 | 2 | * |
|
| 877 | 1 | * @throws \InvalidArgumentException |
|
| 878 | 1 | * @return \Phinx\Seed\AbstractSeed[] |
|
| 879 | 1 | */ |
|
| 880 | public function getSeeds() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Returns a list of seed files found in the provided seed paths. |
||
| 934 | * |
||
| 935 | * @return string[] |
||
| 936 | */ |
||
| 937 | View Code Duplication | protected function getSeedFiles() |
|
| 938 | { |
||
| 939 | $config = $this->getConfig(); |
||
| 940 | $paths = $config->getSeedPaths($this->getEnvironmentName(), $this->getDbRef()); |
||
| 941 | |||
| 942 | $files = []; |
||
| 943 | |||
| 944 | foreach ($paths as $path) { |
||
| 945 | $files = array_merge( |
||
| 946 | $files, |
||
| 947 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
| 948 | ); |
||
| 949 | } |
||
| 950 | // glob() can return the same file multiple times |
||
| 951 | // This will cause the migration to fail with a |
||
| 952 | // false assumption of duplicate migrations |
||
| 953 | // http://php.net/manual/en/function.glob.php#110340 |
||
| 954 | $files = array_unique($files); |
||
| 955 | |||
| 956 | return $files; |
||
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Sets the config. |
||
| 961 | * |
||
| 962 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
| 963 | * @return \Phinx\Migration\Manager |
||
| 964 | */ |
||
| 965 | public function setConfig(ConfigInterface $config) |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Gets the config. |
||
| 974 | * |
||
| 975 | * @return \Phinx\Config\ConfigInterface |
||
| 976 | */ |
||
| 977 | public function getConfig() |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Toggles the breakpoint for a specific version. |
||
| 984 | * |
||
| 985 | * @param string $environment |
||
| 986 | * @param int $version |
||
| 987 | * @return void |
||
| 988 | */ |
||
| 989 | public function toggleBreakpoint($environment, $version) |
||
| 990 | { |
||
| 991 | $migrations = $this->getMigrations(); |
||
| 992 | $this->getMigrations(); |
||
| 993 | $env = $this->getEnvironment($environment); |
||
| 994 | $versions = $env->getVersionLog(); |
||
| 995 | |||
| 996 | if (empty($versions) || empty($migrations)) { |
||
| 997 | return; |
||
| 998 | } |
||
| 999 | |||
| 1000 | if ($version === null) { |
||
| 1001 | $lastVersion = end($versions); |
||
| 1002 | $version = $lastVersion['version']; |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
| 1006 | $this->output->writeln(sprintf( |
||
| 1007 | '<comment>warning</comment> %s is not a valid version', |
||
| 1008 | $version |
||
| 1009 | )); |
||
| 1010 | |||
| 1011 | return; |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
| 1015 | |||
| 1016 | $versions = $env->getVersionLog(); |
||
| 1017 | |||
| 1018 | $this->getOutput()->writeln( |
||
| 1019 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
| 1020 | ' for <info>' . $version . '</info>' . |
||
| 1021 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
| 1022 | ); |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Remove all breakpoints |
||
| 1027 | * |
||
| 1028 | * @param string $environment |
||
| 1029 | * @return void |
||
| 1030 | */ |
||
| 1031 | public function removeBreakpoints($environment) |
||
| 1032 | { |
||
| 1033 | $this->getOutput()->writeln(sprintf( |
||
| 1034 | ' %d breakpoints cleared.', |
||
| 1035 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
| 1036 | )); |
||
| 1037 | } |
||
| 1038 | } |
||
| 1039 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.