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 HealthCheckController |
||
14 | { |
||
15 | protected $runnerManager; |
||
16 | protected $pathHelper; |
||
17 | protected $template; |
||
18 | protected $failureStatusCode; |
||
19 | |||
20 | /** |
||
21 | * @param $template |
||
22 | * @param $failureStatusCode |
||
23 | */ |
||
24 | public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, $failureStatusCode) |
||
25 | { |
||
26 | $this->runnerManager = $runnerManager; |
||
27 | $this->pathHelper = $pathHelper; |
||
28 | $this->template = $template; |
||
29 | $this->failureStatusCode = $failureStatusCode; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @return Response |
||
34 | */ |
||
35 | public function indexAction(Request $request) |
||
36 | { |
||
37 | $group = $this->getGroup($request); |
||
38 | |||
39 | $urls = $this->pathHelper->getRoutesJs([ |
||
40 | 'liip_monitor_run_all_checks' => ['group' => $group], |
||
41 | 'liip_monitor_run_single_check' => ['checkId' => 'replaceme', 'group' => $group], |
||
42 | ]); |
||
43 | |||
44 | $css = $this->pathHelper->getStyleTags([ |
||
45 | 'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css', |
||
46 | 'bundles/liipmonitor/css/style.css', |
||
47 | ]); |
||
48 | |||
49 | $javascript = $this->pathHelper->getScriptTags([ |
||
50 | 'bundles/liipmonitor/javascript/jquery-1.7.1.min.js', |
||
51 | 'bundles/liipmonitor/javascript/ember-0.9.5.min.js', |
||
52 | 'bundles/liipmonitor/javascript/app.js', |
||
53 | ]); |
||
54 | |||
55 | // this is a hack to make the bundle template agnostic. |
||
56 | // URL generation for Assets and Routes is still handled by the framework. |
||
57 | ob_start(); |
||
58 | include $this->template; |
||
59 | $content = ob_get_clean(); |
||
60 | |||
61 | return new Response($content, 200, ['Content-Type' => 'text/html']); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return Response |
||
66 | */ |
||
67 | public function listAction(Request $request) |
||
79 | |||
80 | /** |
||
81 | * @return JsonResponse |
||
82 | */ |
||
83 | public function listAllAction() |
||
95 | |||
96 | /** |
||
97 | * @return JsonResponse |
||
98 | */ |
||
99 | public function listGroupsAction() |
||
105 | |||
106 | /** |
||
107 | * @return Response |
||
108 | */ |
||
109 | public function runAllChecksAction(Request $request) |
||
110 | { |
||
111 | $report = $this->runTests($request); |
||
112 | |||
113 | return new JsonResponse([ |
||
114 | 'checks' => $report->getResults(), |
||
115 | 'globalStatus' => $report->getGlobalStatus(), |
||
116 | ]); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return Response |
||
121 | */ |
||
122 | View Code Duplication | public function runAllChecksHttpStatusAction(Request $request) |
|
|
|||
123 | { |
||
124 | $report = $this->runTests($request); |
||
125 | |||
126 | return new Response( |
||
127 | '', |
||
128 | (ArrayReporter::STATUS_OK === $report->getGlobalStatus() ? 200 : $this->failureStatusCode) |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $checkId |
||
134 | * |
||
135 | * @return Response |
||
136 | */ |
||
137 | View Code Duplication | public function runSingleCheckHttpStatusAction($checkId, Request $request) |
|
146 | |||
147 | /** |
||
148 | * @param string $checkId |
||
149 | * |
||
150 | * @return Response |
||
151 | */ |
||
152 | public function runSingleCheckAction($checkId, Request $request) |
||
153 | { |
||
154 | $results = $this->runTests($request, $checkId)->getResults(); |
||
155 | |||
156 | return new JsonResponse($results[0]); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string|null $checkId |
||
161 | * |
||
162 | * @return ArrayReporter |
||
163 | */ |
||
164 | protected function runTests(Request $request, $checkId = null) |
||
165 | { |
||
166 | $reporters = $request->query->get('reporters') ?? []; |
||
167 | |||
168 | if (!is_array($reporters)) { |
||
169 | $reporters = [$reporters]; |
||
170 | } |
||
171 | |||
172 | $reporter = new ArrayReporter(); |
||
173 | |||
174 | $runner = $this->getRunner($request); |
||
175 | |||
176 | $runner->addReporter($reporter); |
||
177 | $runner->useAdditionalReporters($reporters); |
||
178 | $runner->run($checkId); |
||
179 | |||
180 | return $reporter; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return Runner |
||
185 | * |
||
186 | * @throws \Exception |
||
187 | */ |
||
188 | private function getRunner(Request $request) |
||
189 | { |
||
190 | $group = $this->getGroup($request); |
||
191 | |||
192 | $runner = $this->runnerManager->getRunner($group); |
||
193 | |||
194 | if ($runner) { |
||
195 | return $runner; |
||
196 | } |
||
197 | |||
198 | throw new \RuntimeException(sprintf('Unknown check group "%s"', $group)); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | private function getGroup(Request $request) |
||
205 | { |
||
206 | return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup(); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return Response |
||
211 | */ |
||
212 | public function listReportersAction() |
||
216 | } |
||
217 |
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.