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 ScriptCommand 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 ScriptCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ScriptCommand extends AbstractMagentoCommand |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $scriptVars = array(); |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $_scriptFilename = ''; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $_stopOnError = false; |
||
30 | |||
31 | /** |
||
32 | * @var null|\Magento\Framework\App\ProductMetadata |
||
33 | */ |
||
34 | protected $productMetadata = null; |
||
35 | |||
36 | |||
37 | View Code Duplication | protected function configure() |
|
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isEnabled() |
||
118 | |||
119 | protected function execute(InputInterface $input, OutputInterface $output) |
||
157 | |||
158 | /** |
||
159 | * @param InputInterface $input |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | protected function _initDefines(InputInterface $input) |
||
175 | |||
176 | /** |
||
177 | * @param string $filename |
||
178 | * @throws RuntimeException |
||
179 | * @internal param string $input |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function _getContent($filename) |
||
195 | |||
196 | /** |
||
197 | * @param OutputInterface $output |
||
198 | * @param string $commandString |
||
199 | * @throws RuntimeException |
||
200 | * @return void |
||
201 | */ |
||
202 | protected function registerVariable(OutputInterface $output, $commandString) |
||
249 | |||
250 | /** |
||
251 | * @param InputInterface $input |
||
252 | * @param OutputInterface $output |
||
253 | * @param string $commandString |
||
254 | * @throws RuntimeException |
||
255 | */ |
||
256 | protected function runMagerunCommand(InputInterface $input, OutputInterface $output, $commandString) |
||
266 | |||
267 | /** |
||
268 | * @param string $commandString |
||
269 | * @return string |
||
270 | */ |
||
271 | protected function _prepareShellCommand($commandString) |
||
287 | |||
288 | protected function initScriptVars() |
||
289 | { |
||
290 | $rootFolder = $this->getApplication()->getMagentoRootFolder(); |
||
291 | if (!empty($rootFolder)) { |
||
292 | $this->scriptVars['${magento.root}'] = $rootFolder; |
||
293 | $this->scriptVars['${magento.version}'] = $this->getMagentoVersion(); |
||
294 | $this->scriptVars['${magento.edition}'] = $this->getMagentoEdition(); |
||
295 | } |
||
296 | |||
297 | $this->scriptVars['${php.version}'] = substr(phpversion(), 0, strpos(phpversion(), '-')); |
||
298 | $this->scriptVars['${magerun.version}'] = $this->getApplication()->getVersion(); |
||
299 | $this->scriptVars['${script.file}'] = $this->_scriptFilename; |
||
300 | $this->scriptVars['${script.dir}'] = dirname($this->_scriptFilename); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param OutputInterface $output |
||
305 | * @param string $commandString |
||
306 | * @internal param $returnValue |
||
307 | */ |
||
308 | protected function runShellCommand(OutputInterface $output, $commandString) |
||
316 | |||
317 | /** |
||
318 | * @param string $commandString |
||
319 | * @return string |
||
320 | */ |
||
321 | protected function _replaceScriptVars($commandString) |
||
327 | |||
328 | /** |
||
329 | * @return string |
||
330 | */ |
||
331 | private function getMagentoVersion() |
||
335 | |||
336 | /** |
||
337 | * |
||
338 | * @return mixed |
||
339 | */ |
||
340 | private function getMagentoEdition() |
||
344 | |||
345 | /** |
||
346 | * @return \Magento\Framework\App\ProductMetadata |
||
347 | */ |
||
348 | private function getProductMetadata() |
||
358 | } |
||
359 |
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.