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 | private function getSeedDependenciesInstances(AbstractSeed $seed) |
||
794 | 11 | ||
795 | /** |
||
796 | 11 | * Order seeds by dependencies |
|
797 | * |
||
798 | * @param AbstractSeed[] $seeds Seeds |
||
799 | * |
||
800 | * @return AbstractSeed[] |
||
801 | */ |
||
802 | private function orderSeedsByDependencies(array $seeds) |
||
818 | |||
819 | /** |
||
820 | 11 | * Gets an array of database seeders. |
|
821 | * |
||
822 | 11 | * @throws \InvalidArgumentException |
|
823 | 11 | * @return \Phinx\Seed\AbstractSeed[] |
|
824 | 11 | */ |
|
825 | public function getSeeds() |
||
877 | 1 | ||
878 | 1 | /** |
|
879 | 1 | * Returns a list of seed files found in the provided seed paths. |
|
880 | * |
||
881 | 2 | * @return string[] |
|
882 | 1 | */ |
|
883 | 1 | View Code Duplication | protected function getSeedFiles() |
884 | { |
||
885 | 1 | $config = $this->getConfig(); |
|
886 | 1 | $paths = $config->getSeedPaths(); |
|
887 | $files = []; |
||
888 | |||
889 | 1 | foreach ($paths as $path) { |
|
890 | $files = array_merge( |
||
891 | 1 | $files, |
|
892 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
893 | 1 | ); |
|
894 | 1 | } |
|
895 | 1 | // glob() can return the same file multiple times |
|
896 | 1 | // This will cause the migration to fail with a |
|
897 | 1 | // false assumption of duplicate migrations |
|
898 | 1 | // http://php.net/manual/en/function.glob.php#110340 |
|
899 | $files = array_unique($files); |
||
900 | |||
901 | return $files; |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Sets the config. |
||
906 | 1 | * |
|
907 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
||
908 | 1 | * @return \Phinx\Migration\Manager |
|
909 | 1 | */ |
|
910 | 1 | public function setConfig(ConfigInterface $config) |
|
916 | |||
917 | /** |
||
918 | * Gets the config. |
||
919 | * |
||
920 | * @return \Phinx\Config\ConfigInterface |
||
921 | */ |
||
922 | public function getConfig() |
||
926 | |||
927 | /** |
||
928 | * Toggles the breakpoint for a specific version. |
||
929 | * |
||
930 | * @param string $environment |
||
931 | * @param int $version |
||
932 | * @return void |
||
933 | */ |
||
934 | public function toggleBreakpoint($environment, $version) |
||
935 | { |
||
936 | $migrations = $this->getMigrations(); |
||
937 | $this->getMigrations(); |
||
938 | $env = $this->getEnvironment($environment); |
||
939 | $versions = $env->getVersionLog(); |
||
940 | |||
941 | if (empty($versions) || empty($migrations)) { |
||
942 | return; |
||
943 | } |
||
944 | |||
945 | if ($version === null) { |
||
946 | $lastVersion = end($versions); |
||
947 | $version = $lastVersion['version']; |
||
948 | } |
||
949 | |||
950 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
951 | $this->output->writeln(sprintf( |
||
952 | '<comment>warning</comment> %s is not a valid version', |
||
953 | $version |
||
954 | )); |
||
955 | |||
956 | return; |
||
957 | } |
||
958 | |||
959 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
960 | |||
961 | $versions = $env->getVersionLog(); |
||
962 | |||
963 | $this->getOutput()->writeln( |
||
964 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
965 | ' for <info>' . $version . '</info>' . |
||
966 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
967 | ); |
||
968 | } |
||
969 | |||
970 | /** |
||
971 | * Remove all breakpoints |
||
972 | * |
||
973 | * @param string $environment |
||
974 | * @return void |
||
975 | */ |
||
976 | public function removeBreakpoints($environment) |
||
977 | { |
||
978 | $this->getOutput()->writeln(sprintf( |
||
979 | ' %d breakpoints cleared.', |
||
980 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
981 | )); |
||
982 | } |
||
983 | } |
||
984 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.