diff --git a/lib/Auth/AWS.php b/lib/Auth/AWS.php index 1e985e8..ffda3cf 100644 --- a/lib/Auth/AWS.php +++ b/lib/Auth/AWS.php @@ -55,14 +55,14 @@ public function init(): bool { $authHeader = $this->request->getHeader('Authorization'); - if ($authHeader === null) { + if (null === $authHeader) { $this->errorCode = self::ERR_NOAWSHEADER; return false; } $authHeader = explode(' ', $authHeader); - if ('AWS' != $authHeader[0] || !isset($authHeader[1])) { + if ('AWS' !== $authHeader[0] || !isset($authHeader[1])) { $this->errorCode = self::ERR_NOAWSHEADER; return false; @@ -93,7 +93,7 @@ public function validate(string $secretKey): bool $body = $this->request->getBody(); $this->request->setBody($body); - if ($contentMD5 != base64_encode(md5((string) $body, true))) { + if ($contentMD5 !== base64_encode(md5((string) $body, true))) { // content-md5 header did not match md5 signature of body $this->errorCode = self::ERR_MD5CHECKSUMWRONG; @@ -122,7 +122,7 @@ public function validate(string $secretKey): bool ) ); - if ($this->signature != $signature) { + if ($this->signature !== $signature) { $this->errorCode = self::ERR_INVALIDSIGNATURE; return false; diff --git a/lib/Auth/Digest.php b/lib/Auth/Digest.php index 20785b8..23c28f8 100644 --- a/lib/Auth/Digest.php +++ b/lib/Auth/Digest.php @@ -123,7 +123,7 @@ protected function validate(): bool { $A2 = $this->request->getMethod().':'.$this->digestParts['uri']; - if ('auth-int' == $this->digestParts['qop']) { + if ('auth-int' === $this->digestParts['qop']) { // Making sure we support this qop value if (!($this->qop & self::QOP_AUTHINT)) { return false; @@ -132,18 +132,15 @@ protected function validate(): bool $body = $this->request->getBody($asString = true); $this->request->setBody($body); $A2 .= ':'.md5($body); - } else { - // We need to make sure we support this qop value - if (!($this->qop & self::QOP_AUTH)) { - return false; - } + } elseif (!($this->qop & self::QOP_AUTH)) { + return false; } $A2 = md5($A2); $validResponse = md5("{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}"); - return $this->digestParts['response'] == $validResponse; + return $this->digestParts['response'] === $validResponse; } /** @@ -200,7 +197,7 @@ protected function parseDigest(string $digest) preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER); foreach ($matches as $m) { - $data[$m[1]] = $m[2] ? $m[2] : $m[3]; + $data[$m[1]] = $m[2] ?: $m[3]; unset($needed_parts[$m[1]]); } diff --git a/lib/Client.php b/lib/Client.php index ebc86dd..5940662 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -96,14 +96,14 @@ public function send(RequestInterface $request): ResponseInterface try { $response = $this->doRequest($request); - $code = (int) $response->getStatus(); + $code = $response->getStatus(); // We are doing in-PHP redirects, because curl's // FOLLOW_LOCATION throws errors when PHP is configured with // open_basedir. // // https://github.com/fruux/sabre-http/issues/12 - if (in_array($code, [301, 302, 307, 308]) && $redirects < $this->maxRedirects) { + if ($redirects < $this->maxRedirects && in_array($code, [301, 302, 307, 308])) { $oldLocation = $request->getUrl(); // Creating a new instance of the request object. @@ -196,7 +196,7 @@ public function poll(): bool ); if ($status && CURLMSG_DONE === $status['msg']) { - $resourceId = (int)$status['handle']; + $resourceId = (int) $status['handle']; list( $request, $successCallback, @@ -454,14 +454,14 @@ protected function parseCurlResult(string $response, $curlHandle): array foreach ($headerBlob as $header) { $parts = explode(':', $header, 2); - if (2 == count($parts)) { + if (2 === count($parts)) { $response->addHeader(trim($parts[0]), trim($parts[1])); } } $response->setBody($responseBody); - $httpCode = (int)$response->getStatus(); + $httpCode = $response->getStatus(); return [ 'status' => $httpCode >= 400 ? self::STATUS_HTTPERROR : self::STATUS_SUCCESS, @@ -487,7 +487,7 @@ protected function sendAsyncInternal(RequestInterface $request, callable $succes $this->createCurlSettingsArray($request) ); curl_multi_add_handle($this->curlMultiHandle, $curl); - $this->curlMultiMap[(int)$curl] = [ + $this->curlMultiMap[(int) $curl] = [ $request, $success, $error, diff --git a/lib/Message.php b/lib/Message.php index dff242c..fc34f8d 100644 --- a/lib/Message.php +++ b/lib/Message.php @@ -52,7 +52,7 @@ public function getBodyAsStream() if (is_callable($this->body)) { $body = $this->getBodyAsString(); } - if (is_string($body) || $body === null) { + if (is_string($body) || null === $body) { $stream = fopen('php://temp', 'r+'); fwrite($stream, (string) $body); rewind($stream); @@ -75,7 +75,7 @@ public function getBodyAsString(): string if (is_string($body)) { return $body; } - if ($body === null) { + if (null === $body) { return ''; } if (is_callable($body)) { @@ -84,12 +84,15 @@ public function getBodyAsString(): string return ob_get_clean(); } + /** + * @var string|int|null + */ $contentLength = $this->getHeader('Content-Length'); if (is_int($contentLength) || ctype_digit($contentLength)) { return stream_get_contents($body, (int) $contentLength); - } else { - return stream_get_contents($body); } + + return stream_get_contents($body); } /** diff --git a/lib/MessageDecoratorTrait.php b/lib/MessageDecoratorTrait.php index 2f440bb..d5504ac 100644 --- a/lib/MessageDecoratorTrait.php +++ b/lib/MessageDecoratorTrait.php @@ -157,7 +157,7 @@ public function setHeaders(array $headers) * another value. Individual values can be retrieved with * getHeadersAsArray. * - * @param scalar $value + * @param string|string[] $value */ public function addHeader(string $name, $value) { diff --git a/lib/MessageInterface.php b/lib/MessageInterface.php index f8181a5..8070845 100644 --- a/lib/MessageInterface.php +++ b/lib/MessageInterface.php @@ -119,7 +119,7 @@ public function setHeaders(array $headers); * another value. Individual values can be retrieved with * getHeadersAsArray. * - * @param scalar $value + * @param string|string[] $value */ public function addHeader(string $name, $value); diff --git a/lib/Request.php b/lib/Request.php index 907d75a..496629a 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -88,11 +88,11 @@ public function getQueryParameters(): array $url = $this->getUrl(); if (false === ($index = strpos($url, '?'))) { return []; - } else { - parse_str(substr($url, $index + 1), $queryParams); - - return $queryParams; } + + parse_str(substr($url, $index + 1), $queryParams); + + return $queryParams; } protected $absoluteUrl; @@ -174,11 +174,12 @@ public function getPath(): string return trim(decodePath(substr($uri, strlen($baseUri))), '/'); } - // A special case, if the baseUri was accessed without a trailing - // slash, we'll accept it as well. - elseif ($uri.'/' === $baseUri) { + + if ($uri.'/' === $baseUri) { return ''; } + // A special case, if the baseUri was accessed without a trailing + // slash, we'll accept it as well. throw new \LogicException('Requested uri ('.$this->getUrl().') is out of base uri ('.$this->getBaseUrl().')'); } @@ -247,7 +248,7 @@ public function setRawServerData(array $data) */ public function __toString(): string { - $out = $this->getMethod().' '.$this->getUrl().' HTTP/'.$this->getHTTPVersion()."\r\n"; + $out = $this->getMethod().' '.$this->getUrl().' HTTP/'.$this->getHttpVersion()."\r\n"; foreach ($this->getHeaders() as $key => $value) { foreach ($value as $v) { diff --git a/lib/Response.php b/lib/Response.php index d7bf2c2..64dfbc0 100644 --- a/lib/Response.php +++ b/lib/Response.php @@ -105,13 +105,13 @@ class Response extends Message implements ResponseInterface */ public function __construct($status = 500, array $headers = null, $body = null) { - if ($status !== null) { + if (null !== $status) { $this->setStatus($status); } - if ($headers !== null) { + if (null !== $headers) { $this->setHeaders($headers); } - if ($body !== null) { + if (null !== $body) { $this->setBody($body); } } @@ -151,18 +151,19 @@ public function setStatus($status) { if (ctype_digit($status) || is_int($status)) { $statusCode = $status; - $statusText = isset(self::$statusCodes[$status]) ? self::$statusCodes[$status] : 'Unknown'; + $statusText = self::$statusCodes[$status] ?? 'Unknown'; } else { list( $statusCode, $statusText ) = explode(' ', $status, 2); + $statusCode = (int) $statusCode; } if ($statusCode < 100 || $statusCode > 999) { throw new \InvalidArgumentException('The HTTP status code must be exactly 3 digits'); } - $this->status = (int) $statusCode; + $this->status = $statusCode; $this->statusText = $statusText; } diff --git a/lib/Sapi.php b/lib/Sapi.php index bf3daae..19e787f 100644 --- a/lib/Sapi.php +++ b/lib/Sapi.php @@ -42,7 +42,7 @@ public static function getRequest(): Request { $serverArr = $_SERVER; - if ('cli' === php_sapi_name()) { + if ('cli' === PHP_SAPI) { // If we're running off the CLI, we're going to set some default // settings. $serverArr['REQUEST_URI'] = $_SERVER['REQUEST_URI'] ?? '/'; @@ -75,7 +75,7 @@ public static function sendResponse(ResponseInterface $response) } $body = $response->getBody(); - if ($body === null) { + if (null === $body) { return; } @@ -199,11 +199,11 @@ public static function createFromServerArray(array $serverArray): Request } } - if ($url === null) { + if (null === $url) { throw new InvalidArgumentException('The _SERVER array must have a REQUEST_URI key'); } - if ($method === null) { + if (null === $method) { throw new InvalidArgumentException('The _SERVER array must have a REQUEST_METHOD key'); } $r = new Request($method, $url, $headers); diff --git a/lib/functions.php b/lib/functions.php index 161d261..1404c7d 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -138,7 +138,7 @@ function negotiateContentType($acceptHeaderValue, array $availableOptions) foreach ($proposals as $proposal) { // Ignoring broken values. - if ($proposal === null) { + if (null === $proposal) { continue; }