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 |
||
17 | abstract class AbstractCronCommand extends AbstractMagentoCommand |
||
18 | { |
||
19 | /** |
||
20 | * @var \Magento\Framework\App\State |
||
21 | */ |
||
22 | protected $state; |
||
23 | |||
24 | /** |
||
25 | * @var \Magento\Cron\Model\ConfigInterface |
||
26 | */ |
||
27 | protected $cronConfig; |
||
28 | |||
29 | /** |
||
30 | * @var \Magento\Framework\App\Config\ScopeConfigInterface |
||
31 | */ |
||
32 | protected $scopeConfig; |
||
33 | |||
34 | /** |
||
35 | * @var \Magento\Cron\Model\ResourceModel\Schedule\Collection |
||
36 | */ |
||
37 | protected $cronScheduleCollection; |
||
38 | |||
39 | /** |
||
40 | * @var \Magento\Framework\App\ProductMetadataInterface $productMetadata |
||
41 | */ |
||
42 | private $productMetadata; |
||
43 | |||
44 | /** |
||
45 | * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface |
||
46 | */ |
||
47 | protected $timezone; |
||
48 | |||
49 | /** |
||
50 | * @var \Magento\Framework\Stdlib\DateTime\DateTime |
||
51 | */ |
||
52 | private $dateTime; |
||
53 | |||
54 | /** |
||
55 | * @var \Magento\Cron\Model\ScheduleFactory |
||
56 | */ |
||
57 | private $cronScheduleFactory; |
||
58 | |||
59 | /** |
||
60 | * @param \Magento\Framework\App\State $state |
||
61 | * @param \Magento\Cron\Model\ConfigInterface $cronConfig |
||
62 | * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata |
||
63 | * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone |
||
64 | * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime |
||
65 | * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
||
66 | * @param \Magento\Cron\Model\ResourceModel\Schedule\Collection $cronScheduleCollection |
||
67 | * @param \Magento\Cron\Model\ScheduleFactory $cronSchedulFactory |
||
68 | */ |
||
69 | public function inject( |
||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | * @throws \Magento\Framework\Exception\CronException |
||
92 | */ |
||
93 | protected function getJobs() |
||
118 | |||
119 | /** |
||
120 | * @param string $jobCode |
||
121 | * @return array |
||
122 | */ |
||
123 | protected function getJobConfig($jobCode) |
||
135 | |||
136 | /** |
||
137 | * @param array $jobConfig |
||
138 | * @return array |
||
139 | * @throws \Magento\Framework\Exception\CronException |
||
140 | */ |
||
141 | protected function getSchedule(array $jobConfig) |
||
166 | |||
167 | /** |
||
168 | * Get cron expression of cron job. |
||
169 | * |
||
170 | * @param array $jobConfig |
||
171 | * @return null|string |
||
172 | */ |
||
173 | private function getCronExpression($jobConfig) |
||
187 | |||
188 | /** |
||
189 | * Get config of schedule. |
||
190 | * |
||
191 | * @param array $jobConfig |
||
192 | * @return mixed |
||
193 | */ |
||
194 | private function getConfigSchedule($jobConfig) |
||
201 | |||
202 | /** |
||
203 | * @param InputInterface $input |
||
204 | * @param OutputInterface $output |
||
205 | * @param array $jobs |
||
206 | * @return string |
||
207 | * @throws \InvalidArgumentException |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | protected function askJobCode(InputInterface $input, OutputInterface $output, $jobs) |
||
229 | |||
230 | /** |
||
231 | * @param InputInterface $input |
||
232 | * @param OutputInterface $output |
||
233 | * @return array |
||
234 | * @throws \Exception |
||
235 | */ |
||
236 | protected function getJobForExecuteMethod(InputInterface $input, OutputInterface $output) |
||
266 | |||
267 | /** |
||
268 | * Get timestamp used for time related database fields in the cron tables |
||
269 | * |
||
270 | * Note: The timestamp used will change from Magento 2.1.7 to 2.2.0 and |
||
271 | * these changes are branched by Magento version in this method. |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | protected function getCronTimestamp() |
||
286 | } |
||
287 |
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.