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 |
||
42 | class CompileCommand extends Command |
||
43 | { |
||
44 | /** @var array */ |
||
45 | protected $compiledFiles = []; |
||
46 | /** @var array */ |
||
47 | protected $docCreatedFiles = []; |
||
48 | /** @var OutputInterface */ |
||
49 | protected $output; |
||
50 | /** @var string */ |
||
51 | protected $mdPath; |
||
52 | |||
53 | protected function configure() |
||
54 | { |
||
55 | $this |
||
56 | ->setName('atrapalo:mocks-compile') |
||
57 | ->setAliases(['c']) |
||
58 | ->setDescription('Compile .apib files to generate documentation and unique files to load in mock server') |
||
59 | ->addArgument( |
||
60 | 'path', |
||
61 | InputArgument::REQUIRED, |
||
62 | 'Base folder to start to search files index.apib' |
||
63 | ); |
||
64 | } |
||
65 | /** |
||
66 | * @param InputInterface $input |
||
67 | * @param OutputInterface $output |
||
68 | * @return int |
||
69 | */ |
||
70 | protected function execute(InputInterface $input, OutputInterface $output) |
||
71 | { |
||
72 | if (function_exists('xdebug_disable')) { |
||
73 | xdebug_disable(); |
||
74 | } |
||
75 | |||
76 | $this->mdPath = $input->getArgument('path'); |
||
77 | |||
78 | $this->createMockFiles(); |
||
79 | $output->writeln(''); |
||
80 | $this->createDocFiles(); |
||
81 | $output->writeln(''); |
||
82 | |||
83 | return $this->renderFilesInfo(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param InputInterface $input |
||
88 | * @param OutputInterface $output |
||
89 | */ |
||
90 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
91 | { |
||
92 | $this->output = $output; |
||
93 | } |
||
94 | |||
95 | View Code Duplication | protected function createMockFiles() |
|
|
|||
96 | { |
||
97 | $files = $this->getFilesToMock(); |
||
98 | |||
99 | if ($files->count()) { |
||
100 | $this->output->writeln('<info>Compiling "apib" files to mock</info>'); |
||
101 | $progress = new ProgressBar($this->output, count($files)); |
||
102 | $progress->setBarCharacter('<fg=magenta>=</>'); |
||
103 | $progress->setProgressCharacter("\xF0\x9F\x8D\xBA"); |
||
104 | |||
105 | foreach ($files as $file) { |
||
106 | $this->compileToMock($file->getRealPath()); |
||
107 | $progress->advance(); |
||
108 | } |
||
109 | |||
110 | $progress->finish(); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | View Code Duplication | protected function createDocFiles() |
|
115 | { |
||
116 | $files = $this->getFilesToDoc(); |
||
117 | |||
118 | if ($files->count()) { |
||
119 | $this->output->writeln('<info>Creating documentation from mock files compiled</info>'); |
||
120 | $progress = new ProgressBar($this->output, count($files)); |
||
121 | $progress->setBarCharacter('<fg=magenta>=</>'); |
||
122 | $progress->setProgressCharacter("\xF0\x9F\x8D\xBA"); |
||
123 | |||
124 | foreach ($files as $file) { |
||
125 | $this->compileToDoc($file->getRealPath()); |
||
126 | $progress->advance(); |
||
127 | } |
||
128 | |||
129 | $progress->finish(); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param string $fileName |
||
135 | */ |
||
136 | protected function compileToMock($fileName) |
||
140 | |||
141 | /** |
||
142 | * @param string $fileName |
||
143 | */ |
||
144 | protected function compileToDoc($fileName) |
||
148 | |||
149 | /** |
||
150 | * @param string $fileName |
||
151 | * @throws DocException |
||
152 | */ |
||
153 | View Code Duplication | protected function executeAglio($fileName) |
|
190 | |||
191 | /** |
||
192 | * @param string $fileName |
||
193 | * @throws MockException |
||
194 | */ |
||
195 | View Code Duplication | protected function executeMarkdownPP($fileName) |
|
232 | |||
233 | /** |
||
234 | * @return Finder |
||
235 | */ |
||
236 | protected function getFilesToMock() |
||
240 | |||
241 | /** |
||
242 | * @return Finder |
||
243 | */ |
||
244 | protected function getFilesToDoc() |
||
248 | |||
249 | /** |
||
250 | * @param string $path |
||
251 | * @param string $filter |
||
252 | * @return Finder |
||
253 | */ |
||
254 | protected function getFiles($path, $filter) |
||
270 | |||
271 | /** |
||
272 | * @return int |
||
273 | */ |
||
274 | protected function renderFilesInfo() |
||
275 | { |
||
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | protected function hasGeneratedFiles() |
||
305 | } |
||
306 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.