We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 7 |
| Paths | 7 |
| Total Lines | 60 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | 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 |
||
| 85 | public function main(ServerRequestInterface $request) |
||
| 86 | { |
||
| 87 | $url = (string) $request->getQueryParams()['url']; |
||
| 88 | |||
| 89 | if (!GeneralUtility::isValidUrl($url)) { |
||
| 90 | return $this->sendJson(400, [ |
||
| 91 | "message" => "Invalid URL.", |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Originally the header parameter for `GeneralUtility::getUrl()` |
||
| 96 | // Allowed values: |
||
| 97 | // - 0 Don't fetch headers. We'll ignore that. |
||
| 98 | // - 1 Fetch headers and content. We'll follow suit. |
||
| 99 | // - 2 Fetch headers only. We'll follow suit. |
||
| 100 | $header = (int) $request->getQueryParams()['header']; |
||
| 101 | $headerOnly = MathUtility::forceIntegerInRange($header, 0, 2, 0) === 2; |
||
| 102 | |||
| 103 | $httpMethod = $headerOnly ? 'HEAD' : 'GET'; |
||
| 104 | try { |
||
| 105 | $response = $this->requestFactory->request($url, $httpMethod, [ |
||
| 106 | 'headers' => $this->getRequestHeaders(), |
||
| 107 | |||
| 108 | // For performance, don't download content up-front. Rather, we'll |
||
| 109 | // download and upload simultaneously. |
||
| 110 | // https://docs.guzzlephp.org/en/6.5/request-options.html#stream |
||
| 111 | 'stream' => true, |
||
| 112 | |||
| 113 | // Don't throw exceptions when a non-success status code is |
||
| 114 | // received. We handle these manually. |
||
| 115 | 'http_errors' => false, |
||
| 116 | ]); |
||
| 117 | } catch (\Exception $e) { |
||
| 118 | return $this->sendJson(500, [ |
||
| 119 | "message" => "Could not fetch resource of given URL.", |
||
| 120 | ]); |
||
| 121 | } |
||
| 122 | |||
| 123 | http_response_code($response->getStatusCode()); |
||
| 124 | |||
| 125 | $this->overwriteHeaders([ |
||
| 126 | "Access-Control-Allow-Methods" => "GET", |
||
| 127 | "Access-Control-Allow-Origin" => $request->getHeaderLine('Origin') ? : '*', |
||
| 128 | "Access-Control-Max-Age" => "86400", |
||
| 129 | "Content-Type" => $response->getHeader("Content-Type")[0], |
||
| 130 | "Last-Modified" => $response->getHeader("Last-Modified")[0], |
||
| 131 | ]); |
||
| 132 | |||
| 133 | if (!$headerOnly) { |
||
| 134 | // Disable output buffering |
||
| 135 | ob_end_flush(); |
||
| 136 | |||
| 137 | // Stream proxied content in chunks of 8KB |
||
| 138 | $outStream = $response->getBody(); |
||
| 139 | while (!$outStream->eof()) { |
||
| 140 | echo $outStream->read(8 * 1024); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | exit; |
||
| 145 | } |
||
| 147 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.