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 namespace Limoncello\Commands\Wrappers; |
||
26 | class ConsoleIoWrapper implements IoInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var InputInterface |
||
30 | */ |
||
31 | private $input; |
||
32 | |||
33 | /** |
||
34 | * @var OutputInterface |
||
35 | */ |
||
36 | private $output; |
||
37 | |||
38 | /** |
||
39 | * @param InputInterface $input |
||
40 | * @param OutputInterface $output |
||
41 | */ |
||
42 | 4 | public function __construct(InputInterface $input, OutputInterface $output) |
|
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | 1 | public function hasArgument(string $name): bool |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | 2 | public function getArgument(string $name) |
|
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | 1 | public function getArguments(): array |
|
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | 1 | public function hasOption(string $name): bool |
|
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | 1 | public function getOption(string $name) |
|
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | */ |
||
91 | 1 | public function getOptions(): array |
|
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | View Code Duplication | public function writeInfo(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | View Code Duplication | public function writeWarning(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | View Code Duplication | public function writeError(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface |
|
131 | |||
132 | /** |
||
133 | * @return OutputInterface |
||
134 | */ |
||
135 | protected function getOutput(): OutputInterface |
||
139 | |||
140 | /** |
||
141 | * @return InputInterface |
||
142 | */ |
||
143 | 3 | protected function getInput(): InputInterface |
|
147 | |||
148 | /** |
||
149 | * @param int $verbosity |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | protected function convertVerbosityLevel(int $verbosity): int |
||
175 | } |
||
176 |
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.