| Conditions | 16 |
| Paths | 114 |
| Total Lines | 90 |
| Code Lines | 43 |
| Lines | 15 |
| Ratio | 16.67 % |
| Changes | 14 | ||
| Bugs | 1 | 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 |
||
| 23 | try { |
||
| 24 | $result = $callback(); |
||
| 25 | } catch (\Exception $e) { |
||
| 26 | ob_end_clean(); |
||
| 27 | // Rethrow and keep stack trace. |
||
| 28 | throw $e; |
||
| 29 | } |
||
| 30 | $html = ob_get_clean(); |
||
| 31 | |||
| 32 | if (!empty($html) || $mode === self::MODE_WEAK) { |
||
| 33 | if ($mode === self::MODE_WEAK) { |
||
| 34 | $code = http_response_code(); |
||
| 35 | $headers = self::getResponseHeaders(); |
||
| 36 | |||
| 37 | // Suppress actual headers (re-add by PSR-7 Response) |
||
| 38 | // If you don't remove old headers, it's duplicated in HTTP Headers |
||
| 39 | foreach ($headers as $key => $head) { |
||
| 40 | header_remove($key); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($result !== null) { |
||
| 44 | // We might be in weak mode, it is not normal to have both an output and a response! |
||
| 45 | $html = '<h1>Output started in controller. It is not normal to have an output in the controller, and a response returned by the controller. Output detected:</h1>'.$html; |
||
| 46 | $code = 500; |
||
| 47 | } |
||
| 48 | |||
| 49 | return new HtmlResponse($html, $code, $headers); |
||
| 50 | } else { |
||
| 51 | if ($debug) { |
||
| 52 | $html = '<h1>Output started in controller. A controller should return an object implementing the ResponseInterface rather than outputting directly content. Output detected:</h1>'.$html; |
||
| 53 | return new HtmlResponse($html, 500); |
||
| 54 | } else { |
||
| 55 | throw new SplashException('Output started in Controller : '.$html); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (!$result instanceof ResponseInterface) { |
||
| 61 | if ($result === null) { |
||
| 62 | throw new SplashException('Your controller should return an instance of Psr\\Http\\Message\\ResponseInterface. Your controller did not return any value.'); |
||
| 63 | } else { |
||
| 64 | $class = (gettype($result) == 'object') ? get_class($result) : gettype($result); |
||
| 65 | throw new SplashException('Your controller should return an instance of Psr\\Http\\Message\\ResponseInterface. Type of value returned: '.$class); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | return $result; |
||
| 70 | |||
| 71 | // TODO: If Symfony Response convert to psr-7 |
||
| 72 | // if ($result instanceof Response) { |
||
| 73 | // if ($html !== "") { |
||
| 74 | // throw new SplashException("You cannot output text AND return Response object in the same action. Output already started :'$html"); |
||
| 75 | // } |
||
| 76 | // |
||
| 77 | // if (headers_sent()) { |
||
| 78 | // $headers = headers_list(); |
||
| 79 | // throw new SplashException("Headers already sent. Detected headers are : ".var_export($headers, true)); |
||
| 80 | // } |
||
| 81 | // |
||
| 82 | // return $result; |
||
| 83 | // } |
||
| 84 | // |
||
| 85 | // $code = http_response_code(); |
||
| 86 | // $headers = SplashUtils::greatResponseHeaders(); |
||
| 87 | // |
||
| 88 | // // Suppress actual headers (re-add by Symfony Response) |
||
| 89 | // // If you don't remove old headers, it's duplicated in HTTP Headers |
||
| 90 | // foreach ($headers as $key => $head) { |
||
| 91 | // header_remove($key); |
||
| 92 | // } |
||
| 93 | // |
||
| 94 | // return new Response($html, $code, $headers); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Same as apache_response_headers (for any server) |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | private static function getResponseHeaders() { |
||
| 102 | $arh = array(); |
||
| 103 | |||
| 104 | // headers_list don't return associative array |
||
| 105 | $headers = headers_list(); |
||
| 106 | foreach ($headers as $header) { |
||
| 107 | $header = explode(":", $header); |
||
| 108 | $arh[array_shift($header)] = trim(implode(":", $header)); |
||
| 109 | } |
||
| 110 | return $arh; |
||
| 111 | } |
||
| 112 | } |
||
| 113 |