| Conditions | 10 |
| Paths | 100 |
| Total Lines | 81 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 90 | public function sendRequest(RequestInterface $request) |
||
| 91 | { |
||
| 92 | $body = (string) $request->getBody(); |
||
| 93 | |||
| 94 | $headers = []; |
||
| 95 | foreach (array_keys($request->getHeaders()) as $headerName) { |
||
| 96 | if (strtolower($headerName) === 'content-length') { |
||
| 97 | $values = array(strlen($body)); |
||
| 98 | } else { |
||
| 99 | $values = $request->getHeader($headerName); |
||
| 100 | } |
||
| 101 | foreach ($values as $value) { |
||
| 102 | $headers[] = $headerName . ': ' . $value; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $streamContextOptions = array( |
||
| 107 | 'protocol_version' => $request->getProtocolVersion(), |
||
| 108 | 'method' => $request->getMethod(), |
||
| 109 | 'header' => implode("\r\n", $headers), |
||
| 110 | 'timeout' => $this->timeout, |
||
| 111 | 'ignore_errors' => true, |
||
| 112 | 'follow_location' => $this->followRedirects ? 1 : 0, |
||
| 113 | 'max_redirects' => 100 |
||
| 114 | ); |
||
| 115 | |||
| 116 | if (strlen($body) > 0) { |
||
| 117 | $streamContextOptions['content'] = $body; |
||
| 118 | } |
||
| 119 | |||
| 120 | $context = stream_context_create(array( |
||
| 121 | 'http' => $streamContextOptions, |
||
| 122 | 'https' => $streamContextOptions |
||
| 123 | )); |
||
| 124 | |||
| 125 | $httpHeadersOffset = 0; |
||
| 126 | $finalUrl = (string) $request->getUri(); |
||
|
|
|||
| 127 | |||
| 128 | stream_context_set_params( |
||
| 129 | $context, |
||
| 130 | array('notification' => |
||
| 131 | function ( |
||
| 132 | $code, |
||
| 133 | $severity, |
||
| 134 | $msg, |
||
| 135 | $msgCode, |
||
| 136 | $bytesTx, |
||
| 137 | $bytesMax |
||
| 138 | ) use ( |
||
| 139 | &$remoteDocument, |
||
| 140 | &$http_response_header, |
||
| 141 | &$httpHeadersOffset |
||
| 142 | ) { |
||
| 143 | if ($code === STREAM_NOTIFY_REDIRECTED) { |
||
| 144 | $finalUrl = $msg; |
||
| 145 | $httpHeadersOffset = count($http_response_header); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | ) |
||
| 149 | ); |
||
| 150 | |||
| 151 | |||
| 152 | $response = $this->messageFactory->createResponse(); |
||
| 153 | if (false === ($responseBody = @file_get_contents((string) $request->getUri(), false, $context))) { |
||
| 154 | if (!isset($http_response_header)) { |
||
| 155 | throw new NetworkException( |
||
| 156 | 'Unable to execute request', |
||
| 157 | $request |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | } else { |
||
| 161 | $response = $response->withBody($this->streamFactory->createStream($responseBody)); |
||
| 162 | } |
||
| 163 | |||
| 164 | $parser = new HeaderParser(); |
||
| 165 | try { |
||
| 166 | return $parser->parseArray(array_slice($http_response_header, $httpHeadersOffset), $response); |
||
| 167 | } catch (\Exception $e) { |
||
| 168 | throw new RequestException($e->getMessage(), $request, $e); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.