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 | * CORS header settings |
||
| 30 | * |
||
| 31 | * Available values: |
||
| 32 | * 'auto' - send back header Access-Control-Allow-Origin with domain that made request |
||
| 33 | * '*' - send header with '*' - this will workf fine if you dont need to send cookies via ajax calls to api |
||
| 34 | * with jquery $.ajax with xhrFields: { withCredentials: true } settings |
||
| 35 | * 'off' - will not send any CORS header |
||
| 36 | * other - any other value will be send in Access-Control-Allow-Origin header |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $corsHeader = '*'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Presenter startup method |
||
| 44 | * |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | 9 | public function startup() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Set cors header |
||
| 55 | * |
||
| 56 | * See description to property $corsHeader for valid inputs |
||
| 57 | * |
||
| 58 | * @param string $corsHeader |
||
| 59 | */ |
||
| 60 | public function setCorsHeader($corsHeader) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Nette render default method |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | 9 | public function renderDefault() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Get handler information triplet (endpoint, handler, authorization) |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 9 | private function getHandler() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Check authorization |
||
| 126 | * |
||
| 127 | * @param ApiAuthorizationInterface $authorization |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 9 | private function checkAuth(ApiAuthorizationInterface $authorization) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Process input parameters |
||
| 143 | * |
||
| 144 | * @param ApiHandlerInterface $handler |
||
| 145 | * |
||
| 146 | * @return array|bool |
||
| 147 | */ |
||
| 148 | 6 | private function processParams(ApiHandlerInterface $handler) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Log request |
||
| 161 | * |
||
| 162 | * @param ApiLoggerInterface $logger |
||
| 163 | * @param integer $code |
||
| 164 | * @param double $elapsed |
||
| 165 | * |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | private function logRequest(ApiLoggerInterface $logger, $code, $elapsed) |
||
| 169 | { |
||
| 170 | $headers = []; |
||
| 171 | if (function_exists('getallheaders')) { |
||
| 172 | $headers = getallheaders(); |
||
| 173 | } |
||
| 174 | |||
| 175 | $requestHeaders = ''; |
||
| 176 | foreach ($headers as $key => $value) { |
||
| 177 | $requestHeaders .= "$key: $value\n"; |
||
| 178 | } |
||
| 179 | |||
| 180 | $ipDetector = $this->context->getByType('Tomaj\NetteApi\Misc\IpDetectorInterface'); |
||
| 181 | $logger->log( |
||
| 182 | $code, |
||
| 183 | $this->getRequest()->getMethod(), |
||
| 184 | $requestHeaders, |
||
| 185 | filter_input(INPUT_SERVER, 'REQUEST_URI'), |
||
| 186 | $ipDetector->getRequestIp(), |
||
| 187 | filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
||
| 188 | ($elapsed) * 1000 |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | 9 | protected function sendCorsHeaders() |
|
| 212 | |||
| 213 | private function getRequestDomain() |
||
| 228 | } |
||
| 229 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: