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 |
||
24 | View Code Duplication | class SnapshotCommand extends AbstractCommand |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * The default command name |
||
28 | * |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected static $defaultName = 'snapshot'; |
||
32 | |||
33 | /** |
||
34 | * JSON view for displaying the statistics. |
||
35 | * |
||
36 | * @var StatsJsonView |
||
37 | */ |
||
38 | private $view; |
||
39 | |||
40 | /** |
||
41 | * Filesystem adapter for the snapshots space. |
||
42 | * |
||
43 | * @var Filesystem |
||
44 | */ |
||
45 | private $filesystem; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param StatsJsonView $view JSON view for displaying the statistics. |
||
51 | * @param Filesystem $filesystem Filesystem adapter for the snapshots space. |
||
52 | */ |
||
53 | 3 | public function __construct(StatsJsonView $view, Filesystem $filesystem) |
|
60 | |||
61 | /** |
||
62 | * Internal function to execute the command. |
||
63 | * |
||
64 | * @param InputInterface $input The input to inject into the command. |
||
65 | * @param OutputInterface $output The output to inject into the command. |
||
66 | * |
||
67 | * @return integer The command exit code |
||
68 | */ |
||
69 | 3 | protected function doExecute(InputInterface $input, OutputInterface $output): int |
|
111 | |||
112 | /** |
||
113 | * Configures the current command. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | 3 | protected function configure(): void |
|
127 | } |
||
128 |
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.