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 array |
||
| 64 | */ |
||
| 65 | protected $migrations; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $seeds; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | const EXIT_STATUS_DOWN = 1; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var integer |
||
| 79 | */ |
||
| 80 | const EXIT_STATUS_MISSING = 2; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Class Constructor. |
||
| 84 | * |
||
| 85 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
| 86 | * @param \Symfony\Component\Console\Input\InputInterface $input Console Input |
||
| 87 | * @param \Symfony\Component\Console\Output\OutputInterface $output Console Output |
||
| 88 | */ |
||
| 89 | 432 | public function __construct(ConfigInterface $config, InputInterface $input, OutputInterface $output) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Prints the specified environment's migration status. |
||
| 98 | * |
||
| 99 | * @param string $environment |
||
| 100 | * @param null $format |
||
| 101 | * @return int 0 if all migrations are up, or an error code |
||
| 102 | */ |
||
| 103 | 22 | public function printStatus($environment, $format = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Print Missing Version |
||
| 253 | * |
||
| 254 | * @param array $version The missing version to print (in the format returned by Environment.getVersionLog). |
||
| 255 | * @param int $maxNameLength The maximum migration name length. |
||
| 256 | */ |
||
| 257 | 10 | private function printMissingVersion($version, $maxNameLength) |
|
| 258 | { |
||
| 259 | 10 | $this->getOutput()->writeln(sprintf( |
|
| 260 | 10 | ' <error>up</error> %14.0f %19s %19s <comment>%s</comment> <error>** MISSING **</error>', |
|
| 261 | 10 | $version['version'], |
|
| 262 | 10 | $version['start_time'], |
|
| 263 | 10 | $version['end_time'], |
|
| 264 | 10 | str_pad($version['migration_name'], $maxNameLength, ' ') |
|
| 265 | 10 | )); |
|
| 266 | |||
| 267 | 10 | if ($version && $version['breakpoint']) { |
|
| 268 | 1 | $this->getOutput()->writeln(' <error>BREAKPOINT SET</error>'); |
|
| 269 | 1 | } |
|
| 270 | 10 | } |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Migrate to the version of the database on a given date. |
||
| 274 | * |
||
| 275 | * @param string $environment Environment |
||
| 276 | * @param \DateTime $dateTime Date to migrate to |
||
| 277 | * |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | 4 | public function migrateToDateTime($environment, \DateTime $dateTime) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Migrate an environment to the specified version. |
||
| 298 | * |
||
| 299 | * @param string $environment Environment |
||
| 300 | * @param int $version |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | 8 | public function migrate($environment, $version = null) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Execute a migration against the specified environment. |
||
| 358 | * |
||
| 359 | * @param string $name Environment Name |
||
| 360 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
| 361 | * @param string $direction Direction |
||
| 362 | * @return void |
||
| 363 | 119 | */ |
|
| 364 | public function executeMigration($name, MigrationInterface $migration, $direction = MigrationInterface::UP) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Execute a seeder against the specified environment. |
||
| 388 | * |
||
| 389 | * @param string $name Environment Name |
||
| 390 | * @param \Phinx\Seed\SeedInterface $seed Seed |
||
| 391 | * @return void |
||
| 392 | 6 | */ |
|
| 393 | public function executeSeed($name, SeedInterface $seed) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Rollback an environment to the specified version. |
||
| 417 | * |
||
| 418 | * @param string $environment Environment |
||
| 419 | * @param int|string $target |
||
| 420 | * @param bool $force |
||
| 421 | * @param bool $targetMustMatchVersion |
||
| 422 | * @return void |
||
| 423 | 349 | */ |
|
| 424 | public function rollback($environment, $target = null, $force = false, $targetMustMatchVersion = true) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Run database seeders against an environment. |
||
| 527 | * |
||
| 528 | 9 | * @param string $environment Environment |
|
| 529 | * @param string $seed Seeder |
||
| 530 | 9 | * @return void |
|
| 531 | */ |
||
| 532 | 9 | public function seed($environment, $seed = null) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Sets the environments. |
||
| 555 | 381 | * |
|
| 556 | * @param array $environments Environments |
||
| 557 | 381 | * @return \Phinx\Migration\Manager |
|
| 558 | 381 | */ |
|
| 559 | public function setEnvironments($environments = []) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Gets the manager class for the given environment. |
||
| 568 | 382 | * |
|
| 569 | * @param string $name Environment Name |
||
| 570 | 382 | * @throws \InvalidArgumentException |
|
| 571 | 380 | * @return \Phinx\Migration\Manager\Environment |
|
| 572 | */ |
||
| 573 | public function getEnvironment($name) |
||
| 598 | |||
| 599 | /** |
||
| 600 | 400 | * Sets the console input. |
|
| 601 | * |
||
| 602 | 400 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
|
| 603 | 400 | * @return \Phinx\Migration\Manager |
|
| 604 | */ |
||
| 605 | public function setInput(InputInterface $input) |
||
| 606 | { |
||
| 607 | $this->input = $input; |
||
| 608 | |||
| 609 | return $this; |
||
| 610 | } |
||
| 611 | 393 | ||
| 612 | /** |
||
| 613 | 393 | * Gets the console input. |
|
| 614 | * |
||
| 615 | * @return \Symfony\Component\Console\Input\InputInterface |
||
| 616 | */ |
||
| 617 | public function getInput() |
||
| 618 | { |
||
| 619 | return $this->input; |
||
| 620 | } |
||
| 621 | |||
| 622 | 400 | /** |
|
| 623 | * Sets the console output. |
||
| 624 | 400 | * |
|
| 625 | 400 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
|
| 626 | * @return \Phinx\Migration\Manager |
||
| 627 | */ |
||
| 628 | public function setOutput(OutputInterface $output) |
||
| 634 | |||
| 635 | 395 | /** |
|
| 636 | * Gets the console output. |
||
| 637 | * |
||
| 638 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
| 639 | */ |
||
| 640 | public function getOutput() |
||
| 644 | 379 | ||
| 645 | /** |
||
| 646 | 379 | * Sets the database migrations. |
|
| 647 | 379 | * |
|
| 648 | * @param array $migrations Migrations |
||
| 649 | * @return \Phinx\Migration\Manager |
||
| 650 | */ |
||
| 651 | public function setMigrations(array $migrations) |
||
| 657 | 388 | ||
| 658 | /** |
||
| 659 | 388 | * Gets an array of the database migrations, indexed by migration name (aka creation time) and sorted in ascending |
|
| 660 | 388 | * order |
|
| 661 | * |
||
| 662 | * @throws \InvalidArgumentException |
||
| 663 | 388 | * @return \Phinx\Migration\AbstractMigration[] |
|
| 664 | */ |
||
| 665 | 388 | public function getMigrations() |
|
| 731 | 388 | ||
| 732 | 388 | /** |
|
| 733 | 388 | * Returns a list of migration files found in the provided migration paths. |
|
| 734 | * |
||
| 735 | 388 | * @return string[] |
|
| 736 | 388 | */ |
|
| 737 | 388 | View Code Duplication | protected function getMigrationFiles() |
| 738 | 388 | { |
|
| 739 | 388 | $config = $this->getConfig(); |
|
| 740 | 388 | $paths = $config->getMigrationPaths(); |
|
| 741 | $files = []; |
||
| 742 | 388 | ||
| 743 | foreach ($paths as $path) { |
||
| 744 | $files = array_merge( |
||
| 745 | $files, |
||
| 746 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
| 747 | ); |
||
| 748 | } |
||
| 749 | // glob() can return the same file multiple times |
||
| 750 | // This will cause the migration to fail with a |
||
| 751 | 11 | // false assumption of duplicate migrations |
|
| 752 | // http://php.net/manual/en/function.glob.php#110340 |
||
| 753 | 11 | $files = array_unique($files); |
|
| 754 | 11 | ||
| 755 | return $files; |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Sets the database seeders. |
||
| 760 | * |
||
| 761 | * @param array $seeds Seeders |
||
| 762 | * @return \Phinx\Migration\Manager |
||
| 763 | 11 | */ |
|
| 764 | public function setSeeds(array $seeds) |
||
| 770 | |||
| 771 | 11 | /** |
|
| 772 | * Get seed dependencies instances from seed dependency array |
||
| 773 | 11 | * |
|
| 774 | 11 | * @param AbstractSeed $seed Seed |
|
| 775 | 11 | * |
|
| 776 | 11 | * @return AbstractSeed[] |
|
| 777 | */ |
||
| 778 | protected function getSeedDependenciesInstances(AbstractSeed $seed) |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Order seeds by dependencies |
||
| 801 | * |
||
| 802 | * @param AbstractSeed[] $seeds Seeds |
||
| 803 | * |
||
| 804 | 11 | * @return AbstractSeed[] |
|
| 805 | 11 | */ |
|
| 806 | 11 | protected function orderSeedsByDependencies(array $seeds) |
|
| 822 | 11 | ||
| 823 | 11 | /** |
|
| 824 | 11 | * Gets an array of database seeders. |
|
| 825 | * |
||
| 826 | 11 | * @throws \InvalidArgumentException |
|
| 827 | 11 | * @return \Phinx\Seed\AbstractSeed[] |
|
| 828 | 11 | */ |
|
| 829 | 11 | public function getSeeds() |
|
| 881 | 2 | ||
| 882 | 1 | /** |
|
| 883 | 1 | * Returns a list of seed files found in the provided seed paths. |
|
| 884 | * |
||
| 885 | 1 | * @return string[] |
|
| 886 | 1 | */ |
|
| 887 | View Code Duplication | protected function getSeedFiles() |
|
| 888 | { |
||
| 889 | 1 | $config = $this->getConfig(); |
|
| 890 | $paths = $config->getSeedPaths(); |
||
| 891 | 1 | $files = []; |
|
| 892 | |||
| 893 | 1 | foreach ($paths as $path) { |
|
| 894 | 1 | $files = array_merge( |
|
| 895 | 1 | $files, |
|
| 896 | 1 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
|
| 897 | 1 | ); |
|
| 898 | 1 | } |
|
| 899 | // glob() can return the same file multiple times |
||
| 900 | // This will cause the migration to fail with a |
||
| 901 | // false assumption of duplicate migrations |
||
| 902 | // http://php.net/manual/en/function.glob.php#110340 |
||
| 903 | $files = array_unique($files); |
||
| 904 | |||
| 905 | return $files; |
||
| 906 | 1 | } |
|
| 907 | |||
| 908 | 1 | /** |
|
| 909 | 1 | * Sets the config. |
|
| 910 | 1 | * |
|
| 911 | 1 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
|
| 912 | 1 | * @return \Phinx\Migration\Manager |
|
| 913 | */ |
||
| 914 | public function setConfig(ConfigInterface $config) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Gets the config. |
||
| 923 | * |
||
| 924 | * @return \Phinx\Config\ConfigInterface |
||
| 925 | */ |
||
| 926 | public function getConfig() |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Toggles the breakpoint for a specific version. |
||
| 933 | * |
||
| 934 | * @param string $environment |
||
| 935 | * @param int $version |
||
| 936 | * @return void |
||
| 937 | */ |
||
| 938 | public function toggleBreakpoint($environment, $version) |
||
| 939 | { |
||
| 940 | $migrations = $this->getMigrations(); |
||
| 941 | $this->getMigrations(); |
||
| 942 | $env = $this->getEnvironment($environment); |
||
| 943 | $versions = $env->getVersionLog(); |
||
| 944 | |||
| 945 | if (empty($versions) || empty($migrations)) { |
||
| 946 | return; |
||
| 947 | } |
||
| 948 | |||
| 949 | if ($version === null) { |
||
| 950 | $lastVersion = end($versions); |
||
| 951 | $version = $lastVersion['version']; |
||
| 952 | } |
||
| 953 | |||
| 954 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
| 955 | $this->output->writeln(sprintf( |
||
| 956 | '<comment>warning</comment> %s is not a valid version', |
||
| 957 | $version |
||
| 958 | )); |
||
| 959 | |||
| 960 | return; |
||
| 961 | } |
||
| 962 | |||
| 963 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
| 964 | |||
| 965 | $versions = $env->getVersionLog(); |
||
| 966 | |||
| 967 | $this->getOutput()->writeln( |
||
| 968 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
| 969 | ' for <info>' . $version . '</info>' . |
||
| 970 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
| 971 | ); |
||
| 972 | } |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Remove all breakpoints |
||
| 976 | * |
||
| 977 | * @param string $environment |
||
| 978 | * @return void |
||
| 979 | */ |
||
| 980 | public function removeBreakpoints($environment) |
||
| 981 | { |
||
| 982 | $this->getOutput()->writeln(sprintf( |
||
| 983 | ' %d breakpoints cleared.', |
||
| 984 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
| 985 | )); |
||
| 986 | } |
||
| 987 | } |
||
| 988 |
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.