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 |
||
12 | class DefaultCommand extends Command |
||
13 | { |
||
14 | const COMMAND = 'magium:configuration:init'; |
||
15 | |||
16 | 6 | protected function configure() |
|
23 | |||
24 | 4 | protected function getPossibleLocations() |
|
25 | { |
||
26 | 4 | $path = __DIR__; |
|
27 | 4 | $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); |
|
28 | 4 | $path = preg_replace('@/lib/.+@', '', $path); |
|
29 | 4 | $paths = explode('/', $path); |
|
30 | 4 | array_shift($paths); // Probably should not be in the root path... |
|
31 | 4 | $configurationLocation = realpath(DIRECTORY_SEPARATOR); |
|
32 | 4 | $foundVendor = false; |
|
33 | 4 | $possibleLocations = []; |
|
34 | 4 | foreach ($paths as $path) { |
|
35 | 4 | $configurationLocation .= DIRECTORY_SEPARATOR . $path; |
|
36 | 4 | $configurationLocation = realpath($configurationLocation); |
|
37 | 4 | $file = $configurationLocation . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
|
38 | |||
39 | 4 | if (file_exists($file)) { |
|
40 | return false; |
||
41 | } |
||
42 | 4 | if (basename($path) == 'vendor') { |
|
43 | $foundVendor = true; |
||
44 | } |
||
45 | 4 | if (!$foundVendor) { |
|
46 | 4 | $possibleLocations[] = $file; |
|
47 | } |
||
48 | } |
||
49 | 4 | $possibleLocations = array_unique($possibleLocations); // just in case... |
|
50 | 4 | return $possibleLocations; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string|null $file |
||
55 | */ |
||
56 | 2 | protected function writeMagiumConfigurationFile($file) |
|
76 | |||
77 | /** |
||
78 | * @param string|null $configPath |
||
79 | */ |
||
80 | 2 | protected function getContextFileFromConfigPath($configPath) |
|
86 | |||
87 | /** |
||
88 | * @param string $contextPath |
||
89 | */ |
||
90 | 1 | protected function writeContextFileXml($contextPath) |
|
101 | |||
102 | 2 | protected function executeChoices(array $possibleLocations, InputInterface $input, OutputInterface $output) |
|
117 | |||
118 | View Code Duplication | protected function askConfigurationQuestion(array $possibleLocations, InputInterface $input, OutputInterface $output) |
|
131 | |||
132 | |||
133 | /** |
||
134 | * @param string $contextPath |
||
135 | */ |
||
136 | View Code Duplication | protected function askContextFileQuestion(InputInterface $input, OutputInterface $output, $contextPath) |
|
146 | |||
147 | 2 | protected function execute(InputInterface $input, OutputInterface $output) |
|
158 | |||
159 | |||
160 | } |
||
161 |