diff --git a/lib/Client.php b/lib/Client.php index 40d2cc9..ef54d0e 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -149,14 +149,17 @@ public function send(RequestInterface $request): ResponseInterface // If retry was still set to false, it means no event handler // dealt with the problem. In this case we just re-throw the // exception. + // @phpstan-ignore booleanNot.alwaysTrue if (!$retry) { throw $e; } } + // @phpstan-ignore if.alwaysFalse if ($retry) { ++$retryCount; } + // @phpstan-ignore booleanOr.leftAlwaysFalse } while ($retry || $doRedirect); $this->emit('afterRequest', [$request, $response]); @@ -228,6 +231,7 @@ public function poll(): bool $e = new ClientException($curlResult['curl_errmsg'], $curlResult['curl_errno']); $this->emit('exception', [$request, $e, &$retry, $retryCount]); + // @phpstan-ignore if.alwaysFalse if ($retry) { ++$retryCount; $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); @@ -236,6 +240,7 @@ public function poll(): bool $curlResult['request'] = $request; + // @phpstan-ignore function.alreadyNarrowedType if (is_callable($errorCallback)) { $errorCallback($curlResult); } @@ -243,6 +248,7 @@ public function poll(): bool $this->emit('error', [$request, $curlResult['response'], &$retry, $retryCount]); $this->emit('error:'.$curlResult['http_code'], [$request, $curlResult['response'], &$retry, $retryCount]); + // @phpstan-ignore if.alwaysFalse if ($retry) { ++$retryCount; $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); @@ -251,12 +257,14 @@ public function poll(): bool $curlResult['request'] = $request; + // @phpstan-ignore function.alreadyNarrowedType if (is_callable($errorCallback)) { $errorCallback($curlResult); } } else { $this->emit('afterRequest', [$request, $curlResult['response']]); + // @phpstan-ignore function.alreadyNarrowedType if (is_callable($successCallback)) { $successCallback($curlResult['response']); } @@ -299,7 +307,7 @@ public function setThrowExceptions(bool $throwExceptions): void * * These settings will be included in every HTTP request. */ - public function addCurlSetting(int $name, $value): void + public function addCurlSetting(int $name, mixed $value): void { $this->curlSettings[$name] = $value; } @@ -311,10 +319,10 @@ protected function doRequest(RequestInterface $request): ResponseInterface { $settings = $this->createCurlSettingsArray($request); - if (null === $this->curlHandle) { - $this->curlHandle = curl_init(); - } else { + if (isset($this->curlHandle)) { curl_reset($this->curlHandle); + } else { + $this->curlHandle = curl_init(); } curl_setopt_array($this->curlHandle, $settings); @@ -332,17 +340,15 @@ protected function doRequest(RequestInterface $request): ResponseInterface * * By keeping this resource around for the lifetime of this object, things * like persistent connections are possible. - * - * @var resource|null */ - private $curlHandle; + private \CurlHandle $curlHandle; /** * Handler for curl_multi requests. * * The first time sendAsync is used, this will be created. * - * @var resource|null + * @var \CurlMultiHandle|null */ private $curlMultiHandle; @@ -382,6 +388,7 @@ protected function createCurlSettingsArray(RequestInterface $request): array // reason. $settings[CURLOPT_PUT] = true; $settings[CURLOPT_INFILE] = $body; + // @phpstan-ignore function.alreadyNarrowedType if (false !== $bodyStat && array_key_exists('size', $bodyStat)) { $settings[CURLOPT_INFILESIZE] = $bodyStat['size']; } @@ -426,7 +433,7 @@ protected function createCurlSettingsArray(RequestInterface $request): array public const STATUS_HTTPERROR = 2; /** - * @param resource $curlHandle + * @param \CurlHandle $curlHandle * * @return mixed[] */ @@ -466,7 +473,7 @@ private function parseResponse(string $response, $curlHandle): array * status is STATUS_SUCCESS, or STATUS_HTTPERROR * * @param array $headerLines - * @param resource $curlHandle + * @param \CurlHandle $curlHandle * * @return array */ @@ -523,7 +530,7 @@ protected function parseCurlResponse(array $headerLines, string $body, $curlHand * * @deprecated Use parseCurlResponse instead * - * @param resource $curlHandle + * @param \CurlHandle $curlHandle * * @return array */ @@ -548,6 +555,7 @@ protected function parseCurlResult(string $response, $curlHandle): array // This will cause substr($response, $curlInfo['header_size']) return FALSE instead of NULL // An exception will be thrown when calling getBodyAsString then $responseBody = substr($response, $curlInfo['header_size']); + // @phpstan-ignore identical.alwaysFalse if (false === $responseBody) { $responseBody = ''; } @@ -603,7 +611,7 @@ protected function sendAsyncInternal(RequestInterface $request, callable $succes * * This method exists so that it can easily be overridden and mocked. * - * @param resource $curlHandle + * @param \CurlHandle $curlHandle */ protected function curlExec($curlHandle): string { @@ -622,7 +630,7 @@ protected function curlExec($curlHandle): string * * This method exists so that it can easily be overridden and mocked. * - * @param resource $curlHandle + * @param \CurlHandle $curlHandle * * @return array */ diff --git a/phpstan.neon b/phpstan.neon index 703af1c..afb3e74 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -17,7 +17,3 @@ parameters: message: "#^.* will always evaluate to true\\.$#" count: 4 path: tests/* - - - message: "#^Left side of || is always false.$#" - count: 23 - path: lib/Client.php diff --git a/tests/HTTP/ClientTest.php b/tests/HTTP/ClientTest.php index 817cb4c..8b2c8b0 100644 --- a/tests/HTTP/ClientTest.php +++ b/tests/HTTP/ClientTest.php @@ -560,7 +560,7 @@ public function doRequest(RequestInterface $request): ResponseInterface * * This method exists so that it can easily be overridden and mocked. * - * @param resource $curlHandle + * @param \CurlHandle $curlHandle */ protected function curlStuff($curlHandle): array { @@ -581,7 +581,7 @@ protected function curlStuff($curlHandle): array * * This method exists so that it can easily be overridden and mocked. * - * @param resource $curlHandle + * @param \CurlHandle $curlHandle */ protected function curlExec($curlHandle): string {