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 QueueController extends Controller |
||
14 | { |
||
15 | use ControllerTrait; |
||
16 | |||
17 | /** |
||
18 | * Summary stats. |
||
19 | * |
||
20 | * @Route("/") |
||
21 | * @Route("/status/") |
||
22 | * @Template("@DtcQueue/Queue/status.html.twig") |
||
23 | */ |
||
24 | 1 | public function statusAction() |
|
34 | |||
35 | /** |
||
36 | * List jobs in system by default. |
||
37 | * |
||
38 | * @Route("/jobs_all", name="dtc_queue_jobs_all") |
||
39 | */ |
||
40 | 1 | View Code Duplication | public function jobsAllAction() |
52 | |||
53 | /** |
||
54 | * @Route("/archive", name="dtc_queue_archive") |
||
55 | * @Method({"POST"}) |
||
56 | */ |
||
57 | 1 | public function archiveAction(Request $request) |
|
58 | { |
||
59 | 1 | $workerName = $request->get('workerName'); |
|
60 | 1 | $methodName = $request->get('method'); |
|
61 | |||
62 | 1 | $jobManager = $this->get('dtc_queue.manager.job'); |
|
63 | $callback = function ($count) { |
||
64 | echo json_encode(['count' => $count]); |
||
65 | echo "\n"; |
||
66 | flush(); |
||
67 | 1 | }; |
|
68 | |||
69 | 1 | $streamingResponse = new StreamedResponse(function () use ($jobManager, $callback, $workerName, $methodName) { |
|
70 | $total = $jobManager->countLiveJobs($workerName, $methodName); |
||
71 | echo json_encode(['total' => $total]); |
||
72 | echo "\n"; |
||
73 | flush(); |
||
74 | if ($total > 0) { |
||
75 | $jobManager->archiveAllJobs($workerName, $methodName, $callback); |
||
76 | } |
||
77 | 1 | }); |
|
78 | 1 | $streamingResponse->headers->set('Content-Type', 'application/x-ndjson'); |
|
79 | |||
80 | 1 | return $streamingResponse; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * List jobs in system by default. |
||
85 | * |
||
86 | * @Template("@DtcQueue/Queue/jobs.html.twig") |
||
87 | * @Route("/jobs", name="dtc_queue_jobs") |
||
88 | */ |
||
89 | 1 | public function jobsAction() |
|
104 | |||
105 | /** |
||
106 | * List jobs in system by default. |
||
107 | * |
||
108 | * @Template("@DtcQueue/Queue/jobs_running.html.twig") |
||
109 | * @Route("/jobs_running", name="dtc_queue_jobs_running") |
||
110 | */ |
||
111 | 1 | public function runningJobsAction() |
|
124 | |||
125 | /** |
||
126 | * @param string $class1 |
||
127 | * @param string $class2 |
||
128 | * @param string $label1 |
||
129 | * @param string $label2 |
||
130 | * |
||
131 | * @return \Symfony\Component\HttpFoundation\Response |
||
132 | * |
||
133 | * @throws \Exception |
||
134 | */ |
||
135 | 2 | protected function getDualGridParams($class1, $class2, $label1, $label2) |
|
156 | |||
157 | /** |
||
158 | * List jobs in system by default. |
||
159 | * |
||
160 | * @Route("/runs", name="dtc_queue_runs") |
||
161 | */ |
||
162 | 1 | View Code Duplication | public function runsAction() |
174 | |||
175 | /** |
||
176 | * List registered workers in the system. |
||
177 | * |
||
178 | * @Route("/workers", name="dtc_queue_workers") |
||
179 | * @Template("@DtcQueue/Queue/workers.html.twig") |
||
180 | */ |
||
181 | 1 | public function workersAction() |
|
196 | } |
||
197 |
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.