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 |
||
13 | class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface, PhpCsFixerHandlerInterface |
||
14 | { |
||
15 | /** @var array */ |
||
16 | private $files; |
||
17 | /** @var string */ |
||
18 | private $filesToAnalyze; |
||
19 | /** @var array */ |
||
20 | private $levels = []; |
||
21 | /** |
||
22 | * @var IgnoreFiles |
||
23 | */ |
||
24 | private $ignoreFiles; |
||
25 | |||
26 | /** |
||
27 | * PhpCsFixerHandler constructor. |
||
28 | * |
||
29 | * @param OutputHandlerInterface $outputHandler |
||
30 | * @param IgnoreFiles $ignoreFiles |
||
31 | */ |
||
32 | public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles) |
||
37 | |||
38 | /** |
||
39 | * @throws PhpCsFixerException |
||
40 | */ |
||
41 | public function run(array $messages) |
||
90 | |||
91 | /** |
||
92 | * throw new PhpCsFixerException(implode('', $errors)); |
||
93 | * }. |
||
94 | * |
||
95 | * $this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
||
96 | * } |
||
97 | * |
||
98 | * /** |
||
99 | * @param array $files |
||
100 | */ |
||
101 | public function setFiles(array $files) |
||
105 | |||
106 | /** |
||
107 | * @param string $filesToAnalyze |
||
108 | */ |
||
109 | public function setFilesToAnalyze($filesToAnalyze) |
||
113 | |||
114 | /** |
||
115 | * @param array $levels |
||
116 | */ |
||
117 | public function setLevels(array $levels) |
||
121 | |||
122 | /** |
||
123 | * @param array $errors |
||
124 | * |
||
125 | * @return null|string |
||
126 | */ |
||
127 | private function getErrors(array $errors) |
||
137 | } |
||
138 |
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.