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 | * @param string $environment Environment |
||
663 | 388 | * @throws \InvalidArgumentException |
|
664 | * @return \Phinx\Migration\AbstractMigration[] |
||
665 | 388 | */ |
|
666 | public function getMigrations($environment) |
||
667 | 388 | { |
|
668 | 387 | if ($this->migrations === null) { |
|
669 | 387 | $phpFiles = $this->getMigrationFiles(); |
|
670 | |||
671 | 387 | // filter the files to only get the ones that match our naming scheme |
|
672 | 3 | $fileNames = []; |
|
673 | /** @var \Phinx\Migration\AbstractMigration[] $versions */ |
||
674 | $versions = []; |
||
675 | 387 | ||
676 | 387 | foreach ($phpFiles as $filePath) { |
|
677 | if (Util::isValidMigrationFileName(basename($filePath))) { |
||
678 | $version = Util::getVersionFromFileName(basename($filePath)); |
||
679 | 387 | ||
680 | if (isset($versions[$version])) { |
||
681 | 387 | throw new \InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion())); |
|
682 | 2 | } |
|
683 | 2 | ||
684 | 2 | $config = $this->getConfig(); |
|
685 | 2 | $namespace = $config instanceof NamespaceAwareInterface ? $config->getMigrationNamespaceByPath(dirname($filePath)) : null; |
|
686 | 2 | ||
687 | // convert the filename to a class name |
||
688 | $class = ($namespace === null ? '' : $namespace . '\\') . Util::mapFileNameToClassName(basename($filePath)); |
||
689 | 387 | ||
690 | if (isset($fileNames[$class])) { |
||
691 | throw new \InvalidArgumentException(sprintf( |
||
692 | 'Migration "%s" has the same name as "%s"', |
||
693 | 387 | basename($filePath), |
|
694 | 387 | $fileNames[$class] |
|
695 | 2 | )); |
|
696 | 2 | } |
|
697 | 2 | ||
698 | $fileNames[$class] = basename($filePath); |
||
699 | 2 | ||
700 | // load the migration file |
||
701 | /** @noinspection PhpIncludeInspection */ |
||
702 | require_once $filePath; |
||
703 | 385 | if (!class_exists($class)) { |
|
704 | throw new \InvalidArgumentException(sprintf( |
||
705 | 385 | 'Could not find class "%s" in file "%s"', |
|
706 | 2 | $class, |
|
707 | 2 | $filePath |
|
708 | 2 | )); |
|
709 | } |
||
710 | 2 | ||
711 | // instantiate it |
||
712 | $migration = new $class($environment, $version, $this->getInput(), $this->getOutput()); |
||
713 | 383 | ||
714 | 383 | if (!($migration instanceof AbstractMigration)) { |
|
715 | 384 | throw new \InvalidArgumentException(sprintf( |
|
716 | 'The class "%s" in file "%s" must extend \Phinx\Migration\AbstractMigration', |
||
717 | 379 | $class, |
|
718 | 379 | $filePath |
|
719 | 379 | )); |
|
720 | } |
||
721 | 379 | ||
722 | $versions[$version] = $migration; |
||
723 | } |
||
724 | } |
||
725 | |||
726 | ksort($versions); |
||
727 | $this->setMigrations($versions); |
||
728 | } |
||
729 | 388 | ||
730 | return $this->migrations; |
||
731 | 388 | } |
|
732 | 388 | ||
733 | 388 | /** |
|
734 | * Returns a list of migration files found in the provided migration paths. |
||
735 | 388 | * |
|
736 | 388 | * @return string[] |
|
737 | 388 | */ |
|
738 | 388 | View Code Duplication | protected function getMigrationFiles() |
739 | 388 | { |
|
740 | 388 | $config = $this->getConfig(); |
|
741 | $paths = $config->getMigrationPaths(); |
||
742 | 388 | $files = []; |
|
743 | |||
744 | foreach ($paths as $path) { |
||
745 | $files = array_merge( |
||
746 | $files, |
||
747 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
||
748 | ); |
||
749 | } |
||
750 | // glob() can return the same file multiple times |
||
751 | 11 | // This will cause the migration to fail with a |
|
752 | // false assumption of duplicate migrations |
||
753 | 11 | // http://php.net/manual/en/function.glob.php#110340 |
|
754 | 11 | $files = array_unique($files); |
|
755 | |||
756 | return $files; |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Sets the database seeders. |
||
761 | * |
||
762 | * @param array $seeds Seeders |
||
763 | 11 | * @return \Phinx\Migration\Manager |
|
764 | */ |
||
765 | 11 | public function setSeeds(array $seeds) |
|
771 | 11 | ||
772 | /** |
||
773 | 11 | * Get seed dependencies instances from seed dependency array |
|
774 | 11 | * |
|
775 | 11 | * @param AbstractSeed $seed Seed |
|
776 | 11 | * |
|
777 | * @return AbstractSeed[] |
||
778 | */ |
||
779 | 11 | private function getSeedDependenciesInstances(AbstractSeed $seed) |
|
795 | |||
796 | 11 | /** |
|
797 | * Order seeds by dependencies |
||
798 | * |
||
799 | * @param AbstractSeed[] $seeds Seeds |
||
800 | * |
||
801 | * @return AbstractSeed[] |
||
802 | */ |
||
803 | private function orderSeedsByDependencies(array $seeds) |
||
819 | |||
820 | 11 | /** |
|
821 | * Gets an array of database seeders. |
||
822 | 11 | * |
|
823 | 11 | * @throws \InvalidArgumentException |
|
824 | 11 | * @return \Phinx\Seed\AbstractSeed[] |
|
825 | */ |
||
826 | 11 | public function getSeeds() |
|
827 | 11 | { |
|
828 | 11 | if ($this->seeds === null) { |
|
829 | 11 | $phpFiles = $this->getSeedFiles(); |
|
830 | 11 | ||
831 | 11 | // filter the files to only get the ones that match our naming scheme |
|
832 | $fileNames = []; |
||
833 | 11 | /** @var \Phinx\Seed\AbstractSeed[] $seeds */ |
|
834 | $seeds = []; |
||
835 | |||
836 | foreach ($phpFiles as $filePath) { |
||
837 | if (Util::isValidSeedFileName(basename($filePath))) { |
||
838 | $config = $this->getConfig(); |
||
839 | $namespace = $config instanceof NamespaceAwareInterface ? $config->getSeedNamespaceByPath(dirname($filePath)) : null; |
||
840 | |||
841 | // convert the filename to a class name |
||
842 | 400 | $class = ($namespace === null ? '' : $namespace . '\\') . pathinfo($filePath, PATHINFO_FILENAME); |
|
843 | $fileNames[$class] = basename($filePath); |
||
844 | 400 | ||
845 | 400 | // load the seed file |
|
846 | /** @noinspection PhpIncludeInspection */ |
||
847 | require_once $filePath; |
||
848 | if (!class_exists($class)) { |
||
849 | throw new \InvalidArgumentException(sprintf( |
||
850 | 'Could not find class "%s" in file "%s"', |
||
851 | $class, |
||
852 | $filePath |
||
853 | 399 | )); |
|
854 | } |
||
855 | 399 | ||
856 | // instantiate it |
||
857 | $seed = new $class($this->getInput(), $this->getOutput()); |
||
858 | |||
859 | if (!($seed instanceof AbstractSeed)) { |
||
860 | throw new \InvalidArgumentException(sprintf( |
||
861 | 'The class "%s" in file "%s" must extend \Phinx\Seed\AbstractSeed', |
||
862 | $class, |
||
863 | $filePath |
||
864 | )); |
||
865 | 2 | } |
|
866 | |||
867 | 2 | $seeds[$class] = $seed; |
|
868 | 2 | } |
|
869 | 2 | } |
|
870 | 2 | ||
871 | ksort($seeds); |
||
872 | 2 | $this->setSeeds($seeds); |
|
873 | } |
||
874 | |||
875 | $this->seeds = $this->orderSeedsByDependencies($this->seeds); |
||
876 | 2 | ||
877 | 1 | return $this->seeds; |
|
878 | 1 | } |
|
879 | 1 | ||
880 | /** |
||
881 | 2 | * Returns a list of seed files found in the provided seed paths. |
|
882 | 1 | * |
|
883 | 1 | * @return string[] |
|
884 | */ |
||
885 | 1 | View Code Duplication | protected function getSeedFiles() |
886 | 1 | { |
|
887 | $config = $this->getConfig(); |
||
888 | $paths = $config->getSeedPaths(); |
||
889 | 1 | $files = []; |
|
890 | |||
891 | 1 | foreach ($paths as $path) { |
|
892 | $files = array_merge( |
||
893 | 1 | $files, |
|
894 | 1 | Util::glob($path . DIRECTORY_SEPARATOR . '*.php') |
|
895 | 1 | ); |
|
896 | 1 | } |
|
897 | 1 | // glob() can return the same file multiple times |
|
898 | 1 | // This will cause the migration to fail with a |
|
899 | // false assumption of duplicate migrations |
||
900 | // http://php.net/manual/en/function.glob.php#110340 |
||
901 | $files = array_unique($files); |
||
902 | |||
903 | return $files; |
||
904 | } |
||
905 | |||
906 | 1 | /** |
|
907 | * Sets the config. |
||
908 | 1 | * |
|
909 | 1 | * @param \Phinx\Config\ConfigInterface $config Configuration Object |
|
910 | 1 | * @return \Phinx\Migration\Manager |
|
911 | 1 | */ |
|
912 | 1 | public function setConfig(ConfigInterface $config) |
|
918 | |||
919 | /** |
||
920 | * Gets the config. |
||
921 | * |
||
922 | * @return \Phinx\Config\ConfigInterface |
||
923 | */ |
||
924 | public function getConfig() |
||
928 | |||
929 | /** |
||
930 | * Toggles the breakpoint for a specific version. |
||
931 | * |
||
932 | * @param string $environment |
||
933 | * @param int $version |
||
934 | * @return void |
||
935 | */ |
||
936 | public function toggleBreakpoint($environment, $version) |
||
937 | { |
||
938 | $migrations = $this->getMigrations($environment); |
||
939 | $this->getMigrations($environment); |
||
940 | $env = $this->getEnvironment($environment); |
||
941 | $versions = $env->getVersionLog(); |
||
942 | |||
943 | if (empty($versions) || empty($migrations)) { |
||
944 | return; |
||
945 | } |
||
946 | |||
947 | if ($version === null) { |
||
948 | $lastVersion = end($versions); |
||
949 | $version = $lastVersion['version']; |
||
950 | } |
||
951 | |||
952 | View Code Duplication | if (0 != $version && !isset($migrations[$version])) { |
|
953 | $this->output->writeln(sprintf( |
||
954 | '<comment>warning</comment> %s is not a valid version', |
||
955 | $version |
||
956 | )); |
||
957 | |||
958 | return; |
||
959 | } |
||
960 | |||
961 | $env->getAdapter()->toggleBreakpoint($migrations[$version]); |
||
962 | |||
963 | $versions = $env->getVersionLog(); |
||
964 | |||
965 | $this->getOutput()->writeln( |
||
966 | ' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . |
||
967 | ' for <info>' . $version . '</info>' . |
||
968 | ' <comment>' . $migrations[$version]->getName() . '</comment>' |
||
969 | ); |
||
970 | } |
||
971 | |||
972 | /** |
||
973 | * Remove all breakpoints |
||
974 | * |
||
975 | * @param string $environment |
||
976 | * @return void |
||
977 | */ |
||
978 | public function removeBreakpoints($environment) |
||
979 | { |
||
980 | $this->getOutput()->writeln(sprintf( |
||
981 | ' %d breakpoints cleared.', |
||
982 | $this->getEnvironment($environment)->getAdapter()->resetAllBreakpoints() |
||
983 | )); |
||
984 | } |
||
985 | } |
||
986 |
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.