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 |
||
28 | class StartCliReportingService extends Command |
||
29 | { |
||
30 | const COMMAND_NAME = 'mslwk:cli-reporting-start'; |
||
31 | const COMMAND_DESCRIPTION = 'Start asynchronous CLI reporting service'; |
||
32 | const ARGUMENT_NUMBER_OF_THREADS = 'threads'; |
||
33 | |||
34 | /** |
||
35 | * @var TimerInterface |
||
36 | */ |
||
37 | private $timer; |
||
38 | |||
39 | /** |
||
40 | * @var CustomerIdsProviderInterface |
||
41 | */ |
||
42 | private $customerIdsProvider; |
||
43 | |||
44 | /** |
||
45 | * @var LoopInterface |
||
46 | */ |
||
47 | private $loop; |
||
48 | |||
49 | /** |
||
50 | * @var ProcessFactory |
||
51 | */ |
||
52 | private $processFactory; |
||
53 | |||
54 | /** |
||
55 | * @var Json |
||
56 | */ |
||
57 | private $jsonHandler; |
||
58 | |||
59 | /** |
||
60 | * @var ChunkSizeCalculatorInterface |
||
61 | */ |
||
62 | private $chunkSizeCalculator; |
||
63 | |||
64 | /** |
||
65 | * StartCliReportingService constructor. |
||
66 | * @param TimerInterface $timer |
||
67 | * @param CustomerIdsProviderInterface $customerIdsProvider |
||
68 | * @param Factory $loopFactory |
||
69 | * @param ProcessFactory $processFactory |
||
70 | * @param Json $jsonHandler |
||
71 | * @param ChunkSizeCalculatorInterface $chunkSizeCalculator |
||
72 | * @param null $name |
||
73 | */ |
||
74 | View Code Duplication | public function __construct( |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | protected function configure() |
||
105 | |||
106 | /** |
||
107 | * @param InputInterface $input |
||
108 | * @param OutputInterface $output |
||
109 | * @return void |
||
110 | */ |
||
111 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) |
|
124 | |||
125 | /** |
||
126 | * @param int[] $customerIds |
||
127 | * @param int $numberOfThreads |
||
128 | */ |
||
129 | View Code Duplication | protected function startProcesses(array $customerIds, int $numberOfThreads): void |
|
138 | |||
139 | /** |
||
140 | * @param string $command |
||
141 | */ |
||
142 | protected function createProcessDefinition(string $command): void |
||
151 | |||
152 | /** |
||
153 | * @param int[] $customerIds |
||
154 | * @return string |
||
155 | */ |
||
156 | protected function getFullCommand(array $customerIds): string |
||
165 | } |
||
166 |
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.