| Conditions | 10 |
| Paths | 18 |
| Total Lines | 50 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 4 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public function handle(Request $request, Closure $next) |
||
| 22 | { |
||
| 23 | // Next unless method is PATCH and If-Match header is set |
||
| 24 | if (! ($request->isMethod('PATCH') && $request->hasHeader('If-Match'))) { |
||
| 25 | return $next($request); |
||
| 26 | } |
||
| 27 | |||
| 28 | // Create new GET request to same endpoint, |
||
| 29 | // copy headers and add header that allows you to ignore this request in middlewares |
||
| 30 | $getRequest = Request::create($request->getRequestUri(), 'GET'); |
||
| 31 | $getRequest->headers = $request->headers; |
||
| 32 | $getRequest->headers->set('X-From-Middleware', 'IfMatch'); |
||
| 33 | $getResponse = app()->handle($getRequest); |
||
|
|
|||
| 34 | |||
| 35 | // Get content from response object and get hashes from content and etag |
||
| 36 | $getEtag = EtagConditionals::getEtag($request, $getResponse); |
||
| 37 | $ifMatch = $request->header('If-Match'); |
||
| 38 | |||
| 39 | if ($ifMatch === null) { |
||
| 40 | return response(null, 412); |
||
| 41 | } |
||
| 42 | |||
| 43 | $ifMatchArray = (is_string($ifMatch)) ? |
||
| 44 | explode(',', $ifMatch) : |
||
| 45 | $ifMatch; |
||
| 46 | |||
| 47 | // Strip W/ if weak comparison algorithm can be used |
||
| 48 | if (config('etagconditionals.if_match_weak')) { |
||
| 49 | foreach ($ifMatchArray as &$match) { |
||
| 50 | $match = str_replace('W/', '', $match); |
||
| 51 | } |
||
| 52 | unset($match); |
||
| 53 | } |
||
| 54 | |||
| 55 | foreach ($ifMatchArray as &$match) { |
||
| 56 | $match = trim($match); |
||
| 57 | } |
||
| 58 | unset($match); |
||
| 59 | |||
| 60 | // Compare current and request hashes |
||
| 61 | // Also allow wildcard (*) values |
||
| 62 | if (! (in_array($getEtag, $ifMatchArray) || in_array('"*"', $ifMatchArray))) { |
||
| 63 | return response(null, 412); |
||
| 64 | } |
||
| 65 | |||
| 66 | // Before continuing, prepare the application to receive our request |
||
| 67 | // This is for Laravel Octane support |
||
| 68 | app()->instance('request', $request); |
||
| 69 | |||
| 70 | return $next($request); |
||
| 71 | } |
||
| 73 |