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 |
||
13 | class PruneCommand extends ContainerAwareCommand |
||
14 | { |
||
15 | const OLDER_MESSAGE = '<int>[d|m|y|h|i|s] Specify how old the jobs should (defaults to timestamp unless a quantifier is specified [d_ays, m_onths, y_years, h_ours, i_minutes, s_econds'; |
||
16 | |||
17 | 5 | protected function configure() |
|
18 | { |
||
19 | 5 | $this |
|
20 | 5 | ->setName('dtc:queue:prune') |
|
21 | 5 | ->setDescription('Prune job with error status') |
|
22 | 5 | ->addArgument('type', InputArgument::REQUIRED, '<stalled|stalled_runs|error|expired|old|old_runs|old_job_timings> Prune stalled, erroneous, expired, or old jobs') |
|
23 | 5 | ->addOption('older', null, InputOption::VALUE_REQUIRED, self::OLDER_MESSAGE); |
|
24 | 5 | } |
|
25 | |||
26 | 5 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) |
|
|||
27 | { |
||
28 | 5 | $container = $this->getContainer(); |
|
29 | 5 | $jobManager = $container->get('dtc_queue.job_manager'); |
|
30 | 5 | $type = $input->getArgument('type'); |
|
31 | switch ($type) { |
||
32 | 5 | case 'error': |
|
33 | 1 | $count = $jobManager->pruneErroneousJobs(); |
|
34 | 1 | $output->writeln("$count Erroneous Job(s) pruned"); |
|
35 | 1 | break; |
|
36 | 4 | case 'expired': |
|
37 | 1 | $count = $jobManager->pruneExpiredJobs(); |
|
38 | 1 | $output->writeln("$count Expired Job(s) pruned"); |
|
39 | 1 | break; |
|
40 | 3 | default: |
|
41 | 3 | return $this->executeStalledOther($input, $output); |
|
42 | 3 | } |
|
43 | |||
44 | 2 | return 0; |
|
45 | } |
||
46 | |||
47 | 3 | View Code Duplication | public function executeStalledOther(InputInterface $input, OutputInterface $output) |
48 | { |
||
49 | 3 | $container = $this->getContainer(); |
|
50 | 3 | $jobManager = $container->get('dtc_queue.job_manager'); |
|
51 | 3 | $type = $input->getArgument('type'); |
|
52 | switch ($type) { |
||
53 | 3 | case 'stalled': |
|
54 | 1 | $count = $jobManager->pruneStalledJobs(); |
|
55 | 1 | $output->writeln("$count Stalled Job(s) pruned"); |
|
56 | 1 | break; |
|
57 | 2 | case 'stalled_runs': |
|
58 | 1 | $count = $container->get('dtc_queue.run_manager')->pruneStalledRuns(); |
|
59 | 1 | $output->writeln("$count Stalled Job(s) pruned"); |
|
60 | 1 | break; |
|
61 | 1 | default: |
|
62 | 1 | return $this->executeOlder($input, $output); |
|
63 | 1 | } |
|
64 | |||
65 | 2 | return 0; |
|
66 | } |
||
67 | |||
68 | 1 | public function executeOlder(InputInterface $input, OutputInterface $output) |
|
85 | |||
86 | /** |
||
87 | * @param string[] $matches |
||
88 | * @param string $type |
||
89 | * @param OutputInterface $output |
||
90 | * |
||
91 | * @return int |
||
92 | * |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 1 | protected function pruneOldJobs(array $matches, $type, OutputInterface $output) |
|
115 | |||
116 | /** |
||
117 | * @param string $type |
||
118 | * @param \DateTime $olderThan |
||
119 | * @param OutputInterface $output |
||
120 | * |
||
121 | * @return int |
||
122 | * |
||
123 | * @throws UnsupportedException |
||
124 | */ |
||
125 | 1 | protected function pruneOlderThan($type, \DateTime $olderThan, OutputInterface $output) |
|
149 | |||
150 | /** |
||
151 | * Returns the date interval based on the modifier and the duration. |
||
152 | * |
||
153 | * @param string $modifier |
||
154 | * @param int $duration |
||
155 | * |
||
156 | * @return \DateInterval |
||
157 | * |
||
158 | * @throws UnsupportedException |
||
159 | */ |
||
160 | 1 | protected function getInterval($modifier, $duration) |
|
178 | |||
179 | /** |
||
180 | * @param string $modifier |
||
181 | * @param int $duration |
||
182 | * |
||
183 | * @return \DateInterval |
||
184 | * |
||
185 | * @throws UnsupportedException |
||
186 | */ |
||
187 | protected function getIntervalTime($modifier, $duration) |
||
206 | } |
||
207 |
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.