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 |
||
| 15 | class HealthCheckController |
||
| 16 | { |
||
| 17 | protected $runnerManager; |
||
| 18 | protected $pathHelper; |
||
| 19 | protected $template; |
||
| 20 | protected $failureStatusCode; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param $template |
||
| 24 | * @param $failureStatusCode |
||
| 25 | */ |
||
| 26 | public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, $failureStatusCode) |
||
| 27 | { |
||
| 28 | $this->runnerManager = $runnerManager; |
||
| 29 | $this->pathHelper = $pathHelper; |
||
| 30 | $this->template = $template; |
||
| 31 | $this->failureStatusCode = $failureStatusCode; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return Response |
||
| 36 | */ |
||
| 37 | public function indexAction(Request $request) |
||
| 38 | { |
||
| 39 | $group = $this->getGroup($request); |
||
| 40 | |||
| 41 | $urls = $this->pathHelper->getRoutesJs([ |
||
| 42 | 'liip_monitor_run_all_checks' => ['group' => $group], |
||
| 43 | 'liip_monitor_run_single_check' => ['checkId' => 'replaceme', 'group' => $group], |
||
| 44 | ]); |
||
| 45 | |||
| 46 | $css = $this->pathHelper->getStyleTags([ |
||
| 47 | 'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css', |
||
| 48 | 'bundles/liipmonitor/css/style.css', |
||
| 49 | ]); |
||
| 50 | |||
| 51 | $javascript = $this->pathHelper->getScriptTags([ |
||
| 52 | 'bundles/liipmonitor/javascript/jquery-1.7.1.min.js', |
||
| 53 | 'bundles/liipmonitor/javascript/ember-0.9.5.min.js', |
||
| 54 | 'bundles/liipmonitor/javascript/app.js', |
||
| 55 | ]); |
||
| 56 | |||
| 57 | // this is a hack to make the bundle template agnostic. |
||
| 58 | // URL generation for Assets and Routes is still handled by the framework. |
||
| 59 | ob_start(); |
||
| 60 | include $this->template; |
||
| 61 | $content = ob_get_clean(); |
||
| 62 | |||
| 63 | return new Response($content, 200, ['Content-Type' => 'text/html']); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return Response |
||
| 68 | */ |
||
| 69 | public function listAction(Request $request) |
||
| 70 | { |
||
| 71 | $ret = []; |
||
| 72 | |||
| 73 | $runner = $this->getRunner($request); |
||
| 74 | |||
| 75 | foreach ($runner->getChecks() as $alias => $check) { |
||
| 76 | $ret[] = $alias; |
||
| 77 | } |
||
| 78 | |||
| 79 | return new JsonResponse($ret); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return JsonResponse |
||
| 84 | */ |
||
| 85 | public function listAllAction() |
||
| 86 | { |
||
| 87 | $allChecks = []; |
||
| 88 | |||
| 89 | foreach ($this->runnerManager->getRunners() as $group => $runner) { |
||
| 90 | foreach ($runner->getChecks() as $alias => $check) { |
||
| 91 | $allChecks[$group][] = $alias; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return new JsonResponse($allChecks); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return JsonResponse |
||
| 100 | */ |
||
| 101 | public function listGroupsAction() |
||
| 102 | { |
||
| 103 | $groups = $this->runnerManager->getGroups(); |
||
| 104 | |||
| 105 | return new JsonResponse($groups); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return Response |
||
| 110 | */ |
||
| 111 | public function runAllChecksAction(Request $request) |
||
| 112 | { |
||
| 113 | $report = $this->runTests($request); |
||
| 114 | |||
| 115 | return new JsonResponse([ |
||
| 116 | 'checks' => $report->getResults(), |
||
| 117 | 'globalStatus' => $report->getGlobalStatus(), |
||
| 118 | ]); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function streamAllChecksAction(Request $request) |
||
| 122 | { |
||
| 123 | return new StreamedResponse( |
||
| 124 | function () use ($request) { |
||
| 125 | $reporter = new StreamedReporter(); |
||
| 126 | $runner = $this->getRunner($request); |
||
| 127 | |||
| 128 | $runner->addReporter($reporter); |
||
| 129 | $runner->run(); |
||
| 130 | } |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return Response |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function runAllChecksHttpStatusAction(Request $request) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $checkId |
||
| 149 | * |
||
| 150 | * @return Response |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function runSingleCheckHttpStatusAction($checkId, Request $request) |
|
| 153 | { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $checkId |
||
| 164 | * |
||
| 165 | * @return Response |
||
| 166 | */ |
||
| 167 | public function runSingleCheckAction($checkId, Request $request) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string|null $checkId |
||
| 176 | * |
||
| 177 | * @return ArrayReporter |
||
| 178 | */ |
||
| 179 | protected function runTests(Request $request, $checkId = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Runner |
||
| 200 | * |
||
| 201 | * @throws \Exception |
||
| 202 | */ |
||
| 203 | private function getRunner(Request $request) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | private function getGroup(Request $request) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return Response |
||
| 226 | */ |
||
| 227 | public function listReportersAction() |
||
| 231 | } |
||
| 232 |
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.