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 |
||
21 | class ApiPresenter extends Presenter |
||
22 | { |
||
23 | /** |
||
24 | * @var ApiDecider @inject |
||
25 | */ |
||
26 | public $apiDecider; |
||
27 | |||
28 | /** |
||
29 | * @var IpDetectorInterface @inject |
||
30 | */ |
||
31 | public $ipDetector; |
||
32 | |||
33 | /** |
||
34 | * CORS header settings |
||
35 | * |
||
36 | * Available values: |
||
37 | * 'auto' - send back header Access-Control-Allow-Origin with domain that made request |
||
38 | * '*' - send header with '*' - this will workf fine if you dont need to send cookies via ajax calls to api |
||
39 | * with jquery $.ajax with xhrFields: { withCredentials: true } settings |
||
40 | * 'off' - will not send any CORS header |
||
41 | * other - any other value will be send in Access-Control-Allow-Origin header |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $corsHeader = '*'; |
||
46 | |||
47 | /** |
||
48 | * Presenter startup method |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | 6 | public function startup() |
|
57 | |||
58 | /** |
||
59 | * Set cors header |
||
60 | * |
||
61 | * See description to property $corsHeader for valid inputs |
||
62 | * |
||
63 | * @param string $corsHeader |
||
64 | */ |
||
65 | public function setCorsHeader($corsHeader) |
||
69 | |||
70 | /** |
||
71 | * Nette render default method |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | 6 | public function renderDefault() |
|
76 | { |
||
77 | 6 | $start = microtime(true); |
|
78 | |||
79 | 6 | $this->sendCorsHeaders(); |
|
80 | |||
81 | 6 | $hand = $this->getHandler(); |
|
82 | 6 | $handler = $hand['handler']; |
|
83 | 6 | $authorization = $hand['authorization']; |
|
84 | |||
85 | 6 | if ($this->checkAuth($authorization) === false) { |
|
86 | return; |
||
87 | } |
||
88 | |||
89 | 3 | $params = $this->processParams($handler); |
|
90 | if ($params === false) { |
||
91 | return; |
||
92 | } |
||
93 | |||
94 | try { |
||
95 | $response = $handler->handle($params); |
||
96 | $code = $response->getCode(); |
||
97 | } catch (Exception $exception) { |
||
98 | $response = new JsonApiResponse(500, ['status' => 'error', 'message' => 'Internal server error']); |
||
99 | $code = $response->getCode(); |
||
100 | Debugger::log($exception, Debugger::EXCEPTION); |
||
101 | } |
||
102 | |||
103 | $end = microtime(true); |
||
104 | |||
105 | if ($this->context->hasService('apiLogger')) { |
||
106 | $this->logRequest($this->context->getService('apiLogger'), $code, $end - $start); |
||
107 | } |
||
108 | |||
109 | // output to nette |
||
110 | $this->getHttpResponse()->setCode($code); |
||
111 | $this->sendResponse($response); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get handler information triplet (endpoint, handler, authorization) |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | 6 | private function getHandler() |
|
128 | |||
129 | /** |
||
130 | * Check authorization |
||
131 | * |
||
132 | * @param ApiAuthorizationInterface $authorization |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 6 | private function checkAuth(ApiAuthorizationInterface $authorization) |
|
145 | |||
146 | /** |
||
147 | * Process input parameters |
||
148 | * |
||
149 | * @param ApiHandlerInterface $handler |
||
150 | * |
||
151 | * @return array|bool |
||
152 | */ |
||
153 | 3 | private function processParams(ApiHandlerInterface $handler) |
|
154 | { |
||
155 | 3 | $paramsProcessor = new ParamsProcessor($handler->params()); |
|
156 | 3 | View Code Duplication | if ($paramsProcessor->isError()) { |
157 | 3 | $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
|
158 | 3 | $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
|
159 | return false; |
||
160 | } |
||
161 | return $paramsProcessor->getValues(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Log request |
||
166 | * |
||
167 | * @param ApiLoggerInterface $logger |
||
168 | * @param integer $code |
||
169 | * @param double $elapsed |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | private function logRequest(ApiLoggerInterface $logger, $code, $elapsed) |
||
195 | |||
196 | 6 | protected function sendCorsHeaders() |
|
216 | |||
217 | private function getRequestDomain() |
||
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.