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 $dbRef; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $environmentName; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $adapter; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $migrations; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $seeds; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var integer |
||
| 89 | 432 | */ |
|
| 90 | const EXIT_STATUS_DOWN = 1; |
||
| 91 | 432 | ||
| 92 | 432 | /** |
|
| 93 | 432 | * @var integer |
|
| 94 | 432 | */ |
|
| 95 | const EXIT_STATUS_MISSING = 2; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Class Constructor. |
||
| 99 | * |
||
| 100 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
| 101 | * @param \Symfony\Component\Console\Input\InputInterface $input Console Input |
||
| 102 | * @param \Symfony\Component\Console\Output\OutputInterface $output Console Output |
||
| 103 | 22 | */ |
|
| 104 | public function __construct(ConfigInterface $config, InputInterface $input, OutputInterface $output) |
||
| 110 | 22 | ||
| 111 | /** |
||
| 112 | * Prints the specified environment's migration status. |
||
| 113 | 21 | * |
|
| 114 | * @param string $environment |
||
| 115 | 21 | * @param null $format |
|
| 116 | 21 | * @return int 0 if all migrations are up, or an error code |
|
| 117 | 19 | */ |
|
| 118 | 19 | public function printStatus($environment, $format = null) |
|
| 266 | |||
| 267 | 10 | /** |
|
| 268 | 1 | * Print Missing Version |
|
| 269 | 1 | * |
|
| 270 | 10 | * @param array $version The missing version to print (in the format returned by Environment.getVersionLog). |
|
| 271 | * @param int $maxNameLength The maximum migration name length. |
||
| 272 | */ |
||
| 273 | private function printMissingVersion($version, $maxNameLength) |
||
| 274 | { |
||
| 275 | $this->getOutput()->writeln(sprintf( |
||
| 276 | ' <error>up</error> %14.0f %19s %19s <comment>%s</comment> <error>** MISSING **</error>', |
||
| 277 | $version['version'], |
||
| 278 | $version['start_time'], |
||
| 279 | $version['end_time'], |
||
| 280 | 4 | str_pad($version['migration_name'], $maxNameLength, ' ') |
|
| 281 | )); |
||
| 282 | 4 | ||
| 283 | 4 | if ($version && $version['breakpoint']) { |
|
| 284 | $this->getOutput()->writeln(' <error>BREAKPOINT SET</error>'); |
||
| 285 | } |
||
| 286 | 4 | } |
|
| 287 | 4 | ||
| 288 | /** |
||
| 289 | 4 | * Migrate to the version of the database on a given date. |
|
| 290 | 3 | * |
|
| 291 | 3 | * @param string $environment Environment |
|
| 292 | 3 | * @param \DateTime $dateTime Date to migrate to |
|
| 293 | 3 | * |
|
| 294 | 4 | * @return void |
|
| 295 | */ |
||
| 296 | public function migrateToDateTime($environment, \DateTime $dateTime) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Migrate an environment to the specified version. |
||
| 314 | 8 | * |
|
| 315 | 5 | * @param string $environment Environment |
|
| 316 | 5 | * @param int $version |
|
| 317 | 3 | * @return void |
|
| 318 | */ |
||
| 319 | public function migrate($environment, $version = null) |
||
| 372 | |||
| 373 | 119 | /** |
|
| 374 | 119 | * Execute a migration against the specified environment. |
|
| 375 | 119 | * |
|
| 376 | * @param string $name Environment Name |
||
| 377 | 119 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
|
| 378 | * @param string $direction Direction |
||
| 379 | 119 | * @return void |
|
| 380 | 119 | */ |
|
| 381 | 119 | public function executeMigration($name, MigrationInterface $migration, $direction = MigrationInterface::UP) |
|
| 402 | 6 | ||
| 403 | 6 | /** |
|
| 404 | 6 | * Execute a seeder against the specified environment. |
|
| 405 | * |
||
| 406 | 6 | * @param string $name Environment Name |
|
| 407 | * @param \Phinx\Seed\SeedInterface $seed Seed |
||
| 408 | 6 | * @return void |
|
| 409 | 6 | */ |
|
| 410 | 6 | public function executeSeed($name, SeedInterface $seed) |
|
| 432 | 349 | ||
| 433 | /** |
||
| 434 | 349 | * Rollback an environment to the specified version. |
|
| 435 | * |
||
| 436 | * @param string $environment Environment |
||
| 437 | 349 | * @param int|string $target |
|
| 438 | 48 | * @param bool $force |
|
| 439 | 48 | * @param bool $targetMustMatchVersion |
|
| 440 | 48 | * @return void |
|
| 441 | */ |
||
| 442 | 349 | public function rollback($environment, $target = null, $force = false, $targetMustMatchVersion = true) |
|
| 542 | 3 | ||
| 543 | 3 | /** |
|
| 544 | 3 | * Run database seeders against an environment. |
|
| 545 | * |
||
| 546 | * @param string $environment Environment |
||
| 547 | 6 | * @param string $seed Seeder |
|
| 548 | * @return void |
||
| 549 | */ |
||
| 550 | public function seed($environment, $seed = null) |
||
| 573 | |||
| 574 | /** |
||
| 575 | 7 | * Set the dbRef property |
|
| 576 | 1 | * |
|
| 577 | 1 | * @param string $ref |
|
| 578 | * @return \Phinx\Migration\Manager |
||
| 579 | 1 | */ |
|
| 580 | public function setDbRef($ref) |
||
| 585 | |||
| 586 | 6 | /** |
|
| 587 | 6 | * Get the adapter property |
|
| 588 | 6 | * |
|
| 589 | 6 | * @return string |
|
| 590 | */ |
||
| 591 | 6 | public function getDbRef() |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Sets the environments. |
||
| 598 | * |
||
| 599 | * @param array $environments Environments |
||
| 600 | 400 | * @return \Phinx\Migration\Manager |
|
| 601 | */ |
||
| 602 | 400 | public function setEnvironments($environments = []) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Gets the manager class for the given environment. |
||
| 611 | 393 | * |
|
| 612 | * @param string $name Environment Name |
||
| 613 | 393 | * @throws \InvalidArgumentException |
|
| 614 | * @return \Phinx\Migration\Manager\Environment |
||
| 615 | */ |
||
| 616 | public function getEnvironment($name) |
||
| 624 | 400 | ||
| 625 | 400 | public function setEnvironmentName($name) |
|
| 630 | |||
| 631 | public function getEnvironmentName() |
||
| 635 | 395 | ||
| 636 | /** |
||
| 637 | * Gets the manager class for the given environment when given as a single instance. |
||
| 638 | * |
||
| 639 | * @param string $name Environment Name |
||
| 640 | * @throws \InvalidArgumentException |
||
| 641 | * @return \Phinx\Migration\Manager\Environment |
||
| 642 | */ |
||
| 643 | public function getSingleEnvironment($name) |
||
| 670 | |||
| 671 | 387 | /** |
|
| 672 | 3 | * Gets the manager class for the given environment when used as part of a mulitple deployment. |
|
| 673 | * |
||
| 674 | * @param string $name Environment Name |
||
| 675 | 387 | * @throws \InvalidArgumentException |
|
| 676 | 387 | * @return \Phinx\Migration\Manager\Environment |
|
| 677 | */ |
||
| 678 | public function getMultiEnvironment($name) |
||
| 705 | 385 | ||
| 706 | 2 | /** |
|
| 707 | 2 | * Sets the console input. |
|
| 708 | 2 | * |
|
| 709 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
| 710 | 2 | * @return \Phinx\Migration\Manager |
|
| 711 | */ |
||
| 712 | public function setInput(InputInterface $input) |
||
| 713 | 383 | { |
|
| 714 | 383 | $this->input = $input; |
|
| 715 | 384 | ||
| 716 | return $this; |
||
| 717 | 379 | } |
|
| 718 | 379 | ||
| 719 | 379 | /** |
|
| 720 | * Gets the console input. |
||
| 721 | 379 | * |
|
| 722 | * @return \Symfony\Component\Console\Input\InputInterface |
||
| 723 | */ |
||
| 724 | public function getInput() |
||
| 725 | { |
||
| 726 | return $this->input; |
||
| 727 | } |
||
| 728 | |||
| 729 | 388 | /** |
|
| 730 | * Sets the console output. |
||
| 731 | 388 | * |
|
| 732 | 388 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
|
| 733 | 388 | * @return \Phinx\Migration\Manager |
|
| 734 | */ |
||
| 735 | 388 | public function setOutput(OutputInterface $output) |
|
| 741 | |||
| 742 | 388 | /** |
|
| 743 | * Gets the console output. |
||
| 744 | * |
||
| 745 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
| 746 | */ |
||
| 747 | public function getOutput() |
||
| 751 | 11 | ||
| 752 | /** |
||
| 753 | 11 | * Sets the database migrations. |
|
| 754 | 11 | * |
|
| 755 | * @param array $migrations Migrations |
||
| 756 | * @return \Phinx\Migration\Manager |
||
| 757 | */ |
||
| 758 | public function setMigrations(array $migrations = null) |
||
| 764 | |||
| 765 | 11 | /** |
|
| 766 | 11 | * Gets an array of the database migrations, indexed by migration name (aka creation time) and sorted in ascending |
|
| 767 | * order |
||
| 768 | * |
||
| 769 | 11 | * @throws \InvalidArgumentException |
|
| 770 | * @return \Phinx\Migration\AbstractMigration[] |
||
| 771 | 11 | */ |
|
| 772 | public function getMigrations() |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Returns a list of migration files found in the provided migration paths. |
||
| 840 | * |
||
| 841 | * @return string[] |
||
| 842 | 400 | */ |
|
| 843 | View Code Duplication | protected function getMigrationFiles() |
|
| 844 | 400 | { |
|
| 845 | 400 | $config = $this->getConfig(); |
|
| 846 | $paths = $config->getMigrationPaths($this->getEnvironmentName(), $this->getDbRef()); |
||
| 847 | $files = []; |
||
| 848 | |||
| 849 | foreach ($paths as $path) { |
||
| 850 | $files = array_merge( |
||
| 851 | $files, |
||
| 852 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
| 853 | 399 | ); |
|
| 854 | } |
||
| 855 | 399 | // glob() can return the same file multiple times |
|
| 856 | // This will cause the migration to fail with a |
||
| 857 | // false assumption of duplicate migrations |
||
| 858 | // http://php.net/manual/en/function.glob.php#110340 |
||
| 859 | $files = array_unique($files); |
||
| 860 | |||
| 861 | return $files; |
||
| 862 | } |
||
| 863 | |||
| 864 | /** |
||
| 865 | 2 | * Sets the database seeders. |
|
| 866 | * |
||
| 867 | 2 | * @param array $seeds Seeders |
|
| 868 | 2 | * @return \Phinx\Migration\Manager |
|
| 869 | 2 | */ |
|
| 870 | 2 | public function setSeeds(array $seeds = null) |
|
| 876 | 2 | ||
| 877 | 1 | /** |
|
| 878 | 1 | * Get seed dependencies instances from seed dependency array |
|
| 879 | 1 | * |
|
| 880 | * @param AbstractSeed $seed Seed |
||
| 881 | 2 | * |
|
| 882 | 1 | * @return AbstractSeed[] |
|
| 883 | 1 | */ |
|
| 884 | private function getSeedDependenciesInstances(AbstractSeed $seed) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Order seeds by dependencies |
||
| 903 | * |
||
| 904 | * @param AbstractSeed[] $seeds Seeds |
||
| 905 | * |
||
| 906 | 1 | * @return AbstractSeed[] |
|
| 907 | */ |
||
| 908 | 1 | private function orderSeedsByDependencies(array $seeds) |
|
| 924 | |||
| 925 | /** |
||
| 926 | * Gets an array of database seeders. |
||
| 927 | * |
||
| 928 | * @throws \InvalidArgumentException |
||
| 929 | * @return \Phinx\Seed\AbstractSeed[] |
||
| 930 | */ |
||
| 931 | public function getSeeds() |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Returns a list of seed files found in the provided seed paths. |
||
| 986 | * |
||
| 987 | * @return string[] |
||
| 988 | */ |
||
| 989 | View Code Duplication | protected function getSeedFiles() |
|
| 990 | { |
||
| 991 | $config = $this->getConfig(); |
||
| 992 | $paths = $config->getSeedPaths($this->getEnvironmentName(), $this->getDbRef()); |
||
| 993 | |||
| 994 | $files = []; |
||
| 995 | |||
| 996 | foreach ($paths as $path) { |
||
| 997 | $files = array_merge( |
||
| 998 | $files, |
||
| 999 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
| 1000 | ); |
||
| 1001 | } |
||
| 1002 | // glob() can return the same file multiple times |
||
| 1003 | // This will cause the migration to fail with a |
||
| 1004 | // false assumption of duplicate migrations |
||
| 1005 | // http://php.net/manual/en/function.glob.php#110340 |
||
| 1006 | $files = array_unique($files); |
||
| 1007 | |||
| 1008 | return $files; |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Sets the config. |
||
| 1013 | * |
||
| 1014 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
| 1015 | * @return \Phinx\Migration\Manager |
||
| 1016 | */ |
||
| 1017 | public function setConfig(ConfigInterface $config) |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Gets the config. |
||
| 1026 | * |
||
| 1027 | * @return \Phinx\Config\ConfigInterface |
||
| 1028 | */ |
||
| 1029 | public function getConfig() |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Toggles the breakpoint for a specific version. |
||
| 1036 | * |
||
| 1037 | * @param string $environment |
||
| 1038 | * @param int $version |
||
| 1039 | * @return void |
||
| 1040 | */ |
||
| 1041 | public function toggleBreakpoint($environment, $version) |
||
| 1042 | { |
||
| 1043 | $migrations = $this->getMigrations(); |
||
| 1044 | $this->getMigrations(); |
||
| 1045 | $env = $this->getEnvironment($environment); |
||
| 1046 | $versions = $env->getVersionLog(); |
||
| 1047 | |||
| 1048 | if (empty($versions) || empty($migrations)) { |
||
| 1049 | return; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | if ($version === null) { |
||
| 1053 | $lastVersion = end($versions); |
||
| 1054 | $version = $lastVersion['version']; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
| 1058 | $this->output->writeln(sprintf( |
||
| 1059 | '<comment>warning</comment> %s is not a valid version', |
||
| 1060 | $version |
||
| 1061 | )); |
||
| 1062 | |||
| 1063 | return; |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
| 1067 | |||
| 1068 | $versions = $env->getVersionLog(); |
||
| 1069 | |||
| 1070 | $this->getOutput()->writeln( |
||
| 1071 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
| 1072 | ' for <info>' . $version . '</info>' . |
||
| 1073 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
| 1074 | ); |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Remove all breakpoints |
||
| 1079 | * |
||
| 1080 | * @param string $environment |
||
| 1081 | * @return void |
||
| 1082 | */ |
||
| 1083 | public function removeBreakpoints($environment) |
||
| 1084 | { |
||
| 1085 | $this->getOutput()->writeln(sprintf( |
||
| 1086 | ' %d breakpoints cleared.', |
||
| 1087 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
| 1088 | )); |
||
| 1089 | } |
||
| 1090 | } |
||
| 1091 |
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.