| Conditions | 15 |
| Paths | 577 |
| Total Lines | 93 |
| Code Lines | 53 |
| 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 |
||
| 26 | public function request($method = null) |
||
| 27 | { |
||
| 28 | $uri = $this->getUri(); |
||
| 29 | if (!$uri) { |
||
| 30 | throw new Exception('Set URI before calling Http2Client->request()'); |
||
| 31 | } |
||
| 32 | |||
| 33 | if ($method) { |
||
| 34 | $this->setMethod($method); |
||
| 35 | } |
||
| 36 | |||
| 37 | $this->redirectCounter = 0; |
||
| 38 | $maxRedirects = $this->getConfigValue('maxredirects') ?? 5; |
||
| 39 | |||
| 40 | // Build the full URL with GET parameters |
||
| 41 | $url = $uri; |
||
| 42 | $params = $this->getParametersGet(); |
||
| 43 | if (!empty($params)) { |
||
| 44 | $query = http_build_query($params, '', '&'); |
||
| 45 | $url .= (strpos($url, '?') === false ? '?' : '&') . $query; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Initialize cURL |
||
| 49 | $ch = curl_init($url); |
||
| 50 | |||
| 51 | // Force HTTP/2 |
||
| 52 | if (defined('CURL_HTTP_VERSION_2_0')) { |
||
| 53 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
||
| 54 | } elseif (defined('CURL_HTTP_VERSION_2')) { |
||
| 55 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2); |
||
| 56 | } |
||
| 57 | |||
| 58 | // Basic settings |
||
| 59 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 60 | curl_setopt($ch, CURLOPT_HEADER, true); |
||
| 61 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
| 62 | curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirects); |
||
| 63 | |||
| 64 | $timeout = $this->getConfigValue('timeout') ?? 10; |
||
| 65 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
||
| 66 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
||
| 67 | |||
| 68 | // Set user agent |
||
| 69 | $useragent = $this->getConfigValue('useragent') ?? 'Skosmos HTTP/2 Client'; |
||
| 70 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent); |
||
| 71 | |||
| 72 | // Set HTTP method |
||
| 73 | $httpMethod = strtoupper($this->getMethod()); |
||
| 74 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); |
||
| 75 | |||
| 76 | // Set headers using reflection to access private property |
||
| 77 | $headers = []; |
||
| 78 | $reflection = new \ReflectionClass(parent::class); |
||
| 79 | $property = $reflection->getProperty('headers'); |
||
| 80 | $property->setAccessible(true); |
||
| 81 | $headersList = $property->getValue($this); |
||
| 82 | |||
| 83 | foreach ($headersList as $header) { |
||
| 84 | if (is_array($header) && count($header) >= 2) { |
||
| 85 | list($name, $value) = $header; |
||
| 86 | if (is_array($value)) { |
||
| 87 | $value = implode(', ', $value); |
||
| 88 | } |
||
| 89 | $headers[] = "$name: $value"; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | if (!empty($headers)) { |
||
| 93 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Set POST data if present |
||
| 97 | $rawData = $this->getRawData(); |
||
| 98 | if ($rawData !== null && in_array($httpMethod, ['POST', 'PUT', 'PATCH'])) { |
||
| 99 | curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Execute request |
||
| 103 | $response = curl_exec($ch); |
||
| 104 | |||
| 105 | if ($response === false) { |
||
| 106 | $error = curl_error($ch); |
||
| 107 | $errno = curl_errno($ch); |
||
| 108 | curl_close($ch); |
||
| 109 | throw new Exception("cURL request failed: $error (errno: $errno)"); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Get redirect count |
||
| 113 | $this->redirectCounter = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT); |
||
| 114 | |||
| 115 | curl_close($ch); |
||
| 116 | |||
| 117 | // Parse the response |
||
| 118 | return Response::fromString($response); |
||
| 119 | } |
||
| 137 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: