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 |
||
31 | final class AddPrefixCommand extends Command |
||
32 | { |
||
33 | /** @internal */ |
||
34 | const PATH_ARG = 'paths'; |
||
35 | /** @internal */ |
||
36 | const PREFIX_OPT = 'prefix'; |
||
37 | /** @internal */ |
||
38 | const OUTPUT_DIR_OPT = 'output-dir'; |
||
39 | /** @internal */ |
||
40 | const FORCE_OPT = 'force'; |
||
41 | /** @internal */ |
||
42 | const STOP_ON_FAILURE_OPT = 'stop-on-failure'; |
||
43 | /** @internal */ |
||
44 | const CONFIG_FILE = 'config'; |
||
45 | /** @internal */ |
||
46 | const CONFIG_FILE_DEFAULT = 'scoper.inc.php'; |
||
47 | |||
48 | private $fileSystem; |
||
49 | private $handle; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 22 | public function __construct(Filesystem $fileSystem, HandleAddPrefix $handle) |
|
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | 22 | protected function configure() |
|
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 20 | protected function execute(InputInterface $input, OutputInterface $output) |
|
117 | { |
||
118 | 20 | $io = new SymfonyStyle($input, $output); |
|
119 | |||
120 | 20 | $this->validatePrefix($input); |
|
121 | 15 | $this->validatePaths($input); |
|
122 | 15 | $this->validateOutputDir($input, $io); |
|
123 | |||
124 | 15 | $config = $this->retrieveConfig($input, $io); |
|
125 | |||
126 | 13 | $logger = new ConsoleLogger( |
|
127 | 13 | $this->getApplication(), |
|
128 | 13 | $io |
|
129 | ); |
||
130 | |||
131 | 13 | $logger->outputScopingStart( |
|
132 | 13 | $input->getOption(self::PREFIX_OPT), |
|
133 | 13 | $input->getArgument(self::PATH_ARG) |
|
134 | ); |
||
135 | |||
136 | 13 | $paths = $this->retrievePaths($input, $config); |
|
137 | |||
138 | try { |
||
139 | 13 | $this->handle->__invoke( |
|
140 | 13 | $input->getOption(self::PREFIX_OPT), |
|
141 | 13 | $paths, |
|
142 | 13 | $input->getOption(self::OUTPUT_DIR_OPT), |
|
143 | 13 | $config->getPatchers(), |
|
144 | 13 | $config->getGlobalNamespaceWhitelisters(), |
|
145 | 13 | $input->getOption(self::STOP_ON_FAILURE_OPT), |
|
146 | 13 | $logger |
|
147 | ); |
||
148 | 1 | } catch (Throwable $throwable) { |
|
149 | 1 | $logger->outputScopingEndWithFailure(); |
|
150 | |||
151 | 1 | throw $throwable; |
|
152 | } |
||
153 | |||
154 | 12 | $logger->outputScopingEnd(); |
|
155 | } |
||
156 | |||
157 | 20 | private function validatePrefix(InputInterface $input) |
|
158 | { |
||
159 | 20 | $prefix = $input->getOption(self::PREFIX_OPT); |
|
160 | |||
161 | 20 | if (null === $prefix) { |
|
162 | 1 | $prefix = uniqid('PhpScoper'); |
|
163 | } else { |
||
164 | 19 | $prefix = trim($prefix); |
|
165 | } |
||
166 | |||
167 | 20 | if (1 === preg_match('/(?<prefix>.*?)\\\\*$/', $prefix, $matches)) { |
|
168 | 20 | $prefix = $matches['prefix']; |
|
169 | } |
||
170 | |||
171 | 20 | if ('' === $prefix) { |
|
172 | 5 | throw new RuntimeException( |
|
173 | 5 | sprintf( |
|
174 | 5 | 'Expected "%s" argument to be a non empty string.', |
|
175 | 5 | self::PREFIX_OPT |
|
176 | ) |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | 15 | $input->setOption(self::PREFIX_OPT, $prefix); |
|
181 | } |
||
182 | |||
183 | 15 | private function validatePaths(InputInterface $input) |
|
201 | |||
202 | 15 | private function validateOutputDir(InputInterface $input, OutputStyle $io) |
|
203 | { |
||
263 | |||
264 | 15 | private function retrieveConfig(InputInterface $input, OutputStyle $io): Configuration |
|
305 | |||
306 | /** |
||
307 | * @param InputInterface $input |
||
308 | * @param Configuration $configuration |
||
309 | * |
||
310 | * @return string[] List of absolute paths |
||
311 | */ |
||
312 | 13 | private function retrievePaths(InputInterface $input, Configuration $configuration): array |
|
330 | |||
331 | 15 | private function makeAbsolutePath(string $path): string |
|
339 | } |
||
340 |
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.