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 |
||
| 20 | class ApiPresenter extends Presenter |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var ApiDecider @inject |
||
| 24 | */ |
||
| 25 | public $apiDecider; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var IpDetectorInterface @inject |
||
| 29 | */ |
||
| 30 | public $ipDetector; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * CORS header settings |
||
| 34 | * |
||
| 35 | * Available values: |
||
| 36 | * 'auto' - send back header Access-Control-Allow-Origin with domain that made request |
||
| 37 | * '*' - send header with '*' - this will workf fine if you dont need to send cookies via ajax calls to api |
||
| 38 | * with jquery $.ajax with xhrFields: { withCredentials: true } settings |
||
| 39 | * 'off' - will not send any CORS header |
||
| 40 | * other - any other value will be send in Access-Control-Allow-Origin header |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $corsHeader = '*'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Presenter startup method |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function startup() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set cors header |
||
| 59 | * |
||
| 60 | * See description to property $corsHeader for valid inputs |
||
| 61 | * |
||
| 62 | * @param string $corsHeader |
||
| 63 | */ |
||
| 64 | public function setCorsHeader($corsHeader) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Nette render default method |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function renderDefault() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get handler information triplet (endpoint, handler, authorization) |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | private function getHandler() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Check authorization |
||
| 129 | * |
||
| 130 | * @param ApiAuthorizationInterface $authorization |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function checkAuth(ApiAuthorizationInterface $authorization) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Process input parameters |
||
| 146 | * |
||
| 147 | * @param ApiHandlerInterface $handler |
||
| 148 | * |
||
| 149 | * @return array|bool |
||
| 150 | */ |
||
| 151 | private function processParams(ApiHandlerInterface $handler) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Log request |
||
| 164 | * |
||
| 165 | * @param ApiLoggerInterface $logger |
||
| 166 | * @param integer $code |
||
| 167 | * @param double $elapsed |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | private function logRequest(ApiLoggerInterface $logger, $code, $elapsed) |
||
| 193 | |||
| 194 | protected function sendCorsHeaders() |
||
| 214 | |||
| 215 | private function getRequestDomain() |
||
| 225 | } |
||
| 226 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.