| Conditions | 9 |
| Paths | 138 |
| Total Lines | 71 |
| Code Lines | 50 |
| 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 |
||
| 141 | throw new ConnectionException('Failed to connect to the host via proxy'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return mixed |
||
| 146 | * @throws \InvalidArgumentException |
||
| 147 | */ |
||
| 148 | private function getStreamContext(): mixed |
||
| 149 | { |
||
| 150 | if ($this->config->getContext() !== null) { |
||
| 151 | // Suppress the error since we'll catch it below |
||
| 152 | if (@get_resource_type($this->config->getContext()) === 'stream-context') { |
||
| 153 | return $this->config->getContext(); |
||
| 154 | } |
||
| 155 | |||
| 156 | throw new \InvalidArgumentException( |
||
| 157 | 'Stream context is invalid', |
||
| 158 | CommonsContract::CLIENT_INVALID_STREAM_CONTEXT |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | return stream_context_create($this->config->getContextOptions()); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param mixed $urlParts |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | private function getPathWithQuery(mixed $urlParts): string |
||
| 171 | { |
||
| 172 | $path = $urlParts['path'] ?? '/'; |
||
| 173 | $query = $urlParts['query'] ?? ''; |
||
| 174 | $fragment = $urlParts['fragment'] ?? ''; |
||
| 175 | $pathWithQuery = $path; |
||
| 176 | |||
| 177 | if (!empty($query)) { |
||
| 178 | $pathWithQuery .= '?' . $query; |
||
| 179 | } |
||
| 180 | |||
| 181 | if (!empty($fragment)) { |
||
| 182 | $pathWithQuery .= '#' . $fragment; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $pathWithQuery; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $pathWithQuery |
||
| 190 | * @param array $headers |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | private function getHeaders(string $pathWithQuery, array $headers): string |
||
| 195 | { |
||
| 196 | return 'GET ' . $pathWithQuery . " HTTP/1.1\r\n" |
||
| 197 | . implode( |
||
| 198 | "\r\n", |
||
| 199 | array_map( |
||
| 200 | function ($key, $value) { |
||
| 201 | return "$key: $value"; |
||
| 202 | }, |
||
| 203 | array_keys($headers), |
||
| 204 | $headers |
||
| 205 | ) |
||
| 206 | ) |
||
| 207 | . "\r\n\r\n"; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param int $timeout |
||
| 212 | * @param null $microSecs |
||
| 260 | } |