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:
| 1 | <?php |
||
| 11 | class DefaultCommand extends AbstractCommand |
||
| 12 | { |
||
| 13 | const COMMAND = 'magium:configuration:init'; |
||
| 14 | |||
| 15 | 6 | protected function configure() |
|
| 16 | { |
||
| 17 | 6 | $this->setName(self::COMMAND) |
|
| 18 | 6 | ->setHelp('Initializes Magium Configuration') |
|
| 19 | 6 | ->setDescription('Initializes Magium Configuration by creating the default magium-configuration.xml and ' |
|
| 20 | 6 | . 'contexts.xml files'); |
|
| 21 | 6 | } |
|
| 22 | |||
| 23 | 4 | protected function getPossibleLocations() |
|
| 24 | { |
||
| 25 | 4 | $path = __DIR__; |
|
| 26 | 4 | $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); |
|
| 27 | 4 | $path = preg_replace('@/lib/.+@', '', $path); |
|
| 28 | 4 | $paths = explode('/', $path); |
|
| 29 | 4 | array_shift($paths); // Probably should not be in the root path... |
|
| 30 | 4 | $configurationLocation = realpath(DIRECTORY_SEPARATOR); |
|
| 31 | 4 | $foundVendor = false; |
|
| 32 | 4 | $possibleLocations = []; |
|
| 33 | 4 | foreach ($paths as $path) { |
|
| 34 | 4 | $configurationLocation .= DIRECTORY_SEPARATOR . $path; |
|
| 35 | 4 | $configurationLocation = realpath($configurationLocation); |
|
| 36 | 4 | $file = $configurationLocation . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
|
| 37 | |||
| 38 | 4 | if (file_exists($file)) { |
|
| 39 | return false; |
||
| 40 | } |
||
| 41 | 4 | if (basename($path) == 'vendor') { |
|
| 42 | $foundVendor = true; |
||
| 43 | } |
||
| 44 | 4 | if (!$foundVendor) { |
|
| 45 | 4 | $possibleLocations[] = $file; |
|
| 46 | } |
||
| 47 | } |
||
| 48 | 4 | $possibleLocations = array_unique($possibleLocations); // just in case... |
|
| 49 | 4 | return $possibleLocations; |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|null $file |
||
| 54 | */ |
||
| 55 | 2 | protected function writeMagiumConfigurationFile($file) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string|null $configPath |
||
| 78 | */ |
||
| 79 | 2 | protected function getContextFileFromConfigPath($configPath) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $contextPath |
||
| 88 | */ |
||
| 89 | 1 | protected function writeContextFileXml($contextPath) |
|
| 100 | |||
| 101 | 2 | protected function executeChoices(array $possibleLocations, InputInterface $input, OutputInterface $output) |
|
| 116 | |||
| 117 | View Code Duplication | protected function askConfigurationQuestion(array $possibleLocations, InputInterface $input, OutputInterface $output) |
|
| 118 | { |
||
| 119 | $question = new ChoiceQuestion( |
||
| 120 | 'Could not find a magium-configuration.xml file. Where would you like me to put it?', |
||
| 121 | $possibleLocations |
||
| 122 | ); |
||
| 123 | $ask = $this->getHelper('question'); |
||
| 124 | if ($ask instanceof QuestionHelper) { |
||
| 125 | $result = $ask->ask($input, $output, $question); |
||
| 126 | return $result; |
||
| 127 | } |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $contextPath |
||
| 134 | */ |
||
| 135 | View Code Duplication | protected function askContextFileQuestion(InputInterface $input, OutputInterface $output, $contextPath) |
|
| 145 | |||
| 146 | 2 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 157 | |||
| 158 | |||
| 159 | } |
||
| 160 |