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 |
||
| 19 | class AllowCorsRequests |
||
| 20 | { |
||
| 21 | protected $logger; |
||
| 22 | |||
| 23 | 5 | public function __construct(LoggerInterface $logger) |
|
| 27 | |||
| 28 | 5 | public function handle(Request $request, Closure $next) |
|
| 29 | { |
||
| 30 | 5 | if (! $request->headers->has('Origin')) { |
|
| 31 | 1 | return $next($request); |
|
| 32 | } |
||
| 33 | |||
| 34 | 4 | $origin = $request->headers->get('Origin', ''); |
|
| 35 | 4 | $host = $this->parseUrl($origin); |
|
|
|
|||
| 36 | 4 | View Code Duplication | if (empty($host)) { |
| 37 | 1 | $this->logRequest('Origin is invalid', [ |
|
| 38 | 1 | 'origin' => $origin, |
|
| 39 | 1 | 'parsed' => $host, |
|
| 40 | ]); |
||
| 41 | |||
| 42 | 1 | return $this->response($request, 'Origin is invalid', Response::HTTP_BAD_REQUEST); |
|
| 43 | } |
||
| 44 | |||
| 45 | 4 | $allowed_origins = config('lodash.cors.allow_origins', []); |
|
| 46 | 4 | $current_app = $this->parseUrl(config('app.url', '')); |
|
| 47 | 4 | if (! empty($host)) { |
|
| 48 | 4 | $allowed_origins[] = $current_app; |
|
| 49 | } |
||
| 50 | |||
| 51 | 4 | $found = false; |
|
| 52 | 4 | foreach ($allowed_origins as $allowed_origin) { |
|
| 53 | 4 | if ($host === $allowed_origin || ends_with($host, '.' . $allowed_origin)) { |
|
| 54 | 3 | $found = true; |
|
| 55 | 3 | break; |
|
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | 4 | View Code Duplication | if (! $found) { |
| 60 | 1 | $this->logRequest('Origin is not allowed', [ |
|
| 61 | 1 | 'origin' => $origin, |
|
| 62 | 1 | 'parsed' => $host, |
|
| 63 | ]); |
||
| 64 | |||
| 65 | 1 | return $this->response($request, 'Origin is not allowed', Response::HTTP_METHOD_NOT_ALLOWED); |
|
| 66 | } |
||
| 67 | |||
| 68 | 3 | if ($request->method() === Request::METHOD_OPTIONS) { |
|
| 69 | 1 | $allowed_headers = config('lodash.cors.allow_headers', []); |
|
| 70 | 1 | $allowed_methods = config('lodash.cors.allow_methods', []); |
|
| 71 | |||
| 72 | 1 | $response = $this->response($request, 'Allowed', Response::HTTP_OK); |
|
| 73 | |||
| 74 | 1 | $response->headers->set('Access-Control-Allow-Origin', $origin); |
|
| 75 | 1 | $response->headers->set('Access-Control-Allow-Credentials', 'true'); |
|
| 76 | |||
| 77 | 1 | $response->headers->set('Access-Control-Allow-Methods', implode(',', $allowed_methods)); |
|
| 78 | 1 | $response->headers->set('Access-Control-Allow-Headers', implode(',', $allowed_headers)); |
|
| 79 | 1 | $response->headers->set('Access-Control-Max-Age', '1728000'); |
|
| 80 | |||
| 81 | 1 | return $response; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** @var \Illuminate\Http\Response $response */ |
||
| 85 | 2 | $response = $next($request); |
|
| 86 | |||
| 87 | 2 | $response->headers->set('Access-Control-Allow-Origin', $origin); |
|
| 88 | 2 | $response->headers->set('Access-Control-Allow-Credentials', 'true'); |
|
| 89 | |||
| 90 | 2 | return $response; |
|
| 91 | } |
||
| 92 | |||
| 93 | 1 | protected function logRequest(string $message, array $context = []): void |
|
| 97 | |||
| 98 | 4 | protected function parseUrl(string $url): string |
|
| 112 | |||
| 113 | 2 | protected function response(Request $request, string $message, int $code): Response |
|
| 121 | } |
||
| 122 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.