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 | class StartWebapiReportingService extends Command |
||
32 | { |
||
33 | const COMMAND_NAME = 'mslwk:webapi-reporting-start'; |
||
34 | const COMMAND_DESCRIPTION = 'Start asynchronous WebAPI reporting service'; |
||
35 | const ARGUMENT_NUMBER_OF_THREADS = 'threads'; |
||
36 | |||
37 | const API_ENDPOINT_PATH = 'rest/V1/mslwk/customer-report/generate'; |
||
38 | |||
39 | /** |
||
40 | * @var TimerInterface |
||
41 | */ |
||
42 | private $timer; |
||
43 | |||
44 | /** |
||
45 | * @var CustomerIdsProviderInterface |
||
46 | */ |
||
47 | private $customerIdsProvider; |
||
48 | |||
49 | /** |
||
50 | * @var LoopInterface |
||
51 | */ |
||
52 | private $loop; |
||
53 | |||
54 | /** |
||
55 | * @var ClientFactory |
||
56 | */ |
||
57 | private $clientFactory; |
||
58 | |||
59 | /** |
||
60 | * @var Json |
||
61 | */ |
||
62 | private $jsonHandler; |
||
63 | |||
64 | /** |
||
65 | * @var StoreManagerInterface |
||
66 | */ |
||
67 | private $storeManager; |
||
68 | |||
69 | /** |
||
70 | * @var ChunkSizeCalculatorInterface |
||
71 | */ |
||
72 | private $chunkSizeCalculator; |
||
73 | |||
74 | /** |
||
75 | * StartWebapiReportingService constructor. |
||
76 | * @param TimerInterface $timer |
||
77 | * @param CustomerIdsProviderInterface $customerIdsProvider |
||
78 | * @param Factory $loopFactory |
||
79 | * @param ClientFactory $clientFactory |
||
80 | * @param Json $jsonHandler |
||
81 | * @param StoreManagerInterface $storeManager |
||
82 | * @param ChunkSizeCalculatorInterface $chunkSizeCalculator |
||
83 | * @param null $name |
||
84 | */ |
||
85 | View Code Duplication | public function __construct( |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | protected function configure() |
||
118 | |||
119 | /** |
||
120 | * @param InputInterface $input |
||
121 | * @param OutputInterface $output |
||
122 | * @return void |
||
123 | */ |
||
124 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) |
|
137 | |||
138 | /** |
||
139 | * @param array $customerIds |
||
140 | * @param int $numberOfThreads |
||
141 | */ |
||
142 | View Code Duplication | protected function startProcesses(array $customerIds, int $numberOfThreads): void |
|
151 | |||
152 | /** |
||
153 | * @param int[] $customerIds |
||
154 | */ |
||
155 | protected function createRequestDefinition(array $customerIds): void |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | protected function getRequestUrl(): string |
||
194 | } |
||
195 |
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.