diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 63bf993..bcefae6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,13 +10,11 @@ jobs: strategy: fail-fast: false matrix: - php: [7.3, 7.4, 8.0] + php: [7.3, 7.4, 8.0, 8.1] experimental: [false] include: - - php: 8.0 - analysis: true - php: 8.1 - experimental: true + analysis: true steps: - name: Checkout diff --git a/composer.json b/composer.json index 816775f..75a07bf 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "php-http/psr7-integration-tests": "dev-master", "phpspec/prophecy": "^1.14", "phpspec/prophecy-phpunit": "^2.0", - "phpstan/phpstan": "^0.12.99", + "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3.6" }, diff --git a/src/Cookies.php b/src/Cookies.php index 894bdb1..00448a2 100644 --- a/src/Cookies.php +++ b/src/Cookies.php @@ -193,7 +193,7 @@ protected function toHeader(string $name, array $properties): string public static function parseHeader($header): array { if (is_array($header)) { - $header = isset($header[0]) ? $header[0] : ''; + $header = $header[0] ?? ''; } if (!is_string($header)) { diff --git a/src/Factory/ServerRequestFactory.php b/src/Factory/ServerRequestFactory.php index 55ffd8f..b6e0471 100644 --- a/src/Factory/ServerRequestFactory.php +++ b/src/Factory/ServerRequestFactory.php @@ -92,7 +92,7 @@ public function createServerRequest(string $method, $uri, array $serverParams = */ public static function createFromGlobals(): Request { - $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; + $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; $uri = (new UriFactory())->createFromGlobals($_SERVER); $headers = Headers::createFromGlobals(); @@ -106,7 +106,7 @@ public static function createFromGlobals(): Request $uploadedFiles = UploadedFile::createFromGlobals($_SERVER); $request = new Request($method, $uri, $headers, $cookies, $_SERVER, $body, $uploadedFiles); - $contentTypes = $request->getHeader('Content-Type') ?? []; + $contentTypes = $request->getHeader('Content-Type'); $parsedContentType = ''; foreach ($contentTypes as $contentType) { diff --git a/src/Factory/UriFactory.php b/src/Factory/UriFactory.php index 7f0a216..11bc7c3 100644 --- a/src/Factory/UriFactory.php +++ b/src/Factory/UriFactory.php @@ -62,12 +62,12 @@ public function createUri(string $uri = ''): UriInterface public function createFromGlobals(array $globals): Uri { // Scheme - $https = isset($globals['HTTPS']) ? $globals['HTTPS'] : false; + $https = $globals['HTTPS'] ?? false; $scheme = !$https || $https === 'off' ? 'http' : 'https'; // Authority: Username and password - $username = isset($globals['PHP_AUTH_USER']) ? $globals['PHP_AUTH_USER'] : ''; - $password = isset($globals['PHP_AUTH_PW']) ? $globals['PHP_AUTH_PW'] : ''; + $username = $globals['PHP_AUTH_USER'] ?? ''; + $password = $globals['PHP_AUTH_PW'] ?? ''; // Authority: Host $host = ''; @@ -106,7 +106,7 @@ public function createFromGlobals(array $globals): Uri $requestUri = $uriFragments[0]; if ($queryString === '' && count($uriFragments) > 1) { - $queryString = parse_url('http://www.example.com' . $globals['REQUEST_URI'], PHP_URL_QUERY) ?? ''; + $queryString = parse_url('https://www.example.com' . $globals['REQUEST_URI'], PHP_URL_QUERY) ?? ''; } } diff --git a/src/Headers.php b/src/Headers.php index afde205..355c151 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -191,7 +191,7 @@ protected function parseAuthorizationHeader(array $headers): array if (isset($this->globals['REDIRECT_HTTP_AUTHORIZATION'])) { $headers['Authorization'] = $this->globals['REDIRECT_HTTP_AUTHORIZATION']; } elseif (isset($this->globals['PHP_AUTH_USER'])) { - $pw = isset($this->globals['PHP_AUTH_PW']) ? $this->globals['PHP_AUTH_PW'] : ''; + $pw = $this->globals['PHP_AUTH_PW'] ?? ''; $headers['Authorization'] = 'Basic ' . base64_encode($this->globals['PHP_AUTH_USER'] . ':' . $pw); } elseif (isset($this->globals['PHP_AUTH_DIGEST'])) { $headers['Authorization'] = $this->globals['PHP_AUTH_DIGEST']; diff --git a/src/Request.php b/src/Request.php index a80329e..3dd3cd8 100644 --- a/src/Request.php +++ b/src/Request.php @@ -201,7 +201,7 @@ public function getRequestTarget(): string */ public function withRequestTarget($requestTarget) { - if (preg_match('#\s#', $requestTarget)) { + if (!is_string($requestTarget) || preg_match('#\s#', $requestTarget)) { throw new InvalidArgumentException( 'Invalid request target provided; must be a string and cannot contain whitespace' ); @@ -331,7 +331,7 @@ public function getAttributes(): array */ public function getAttribute($name, $default = null) { - return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; + return $this->attributes[$name] ?? $default; } /** diff --git a/src/Response.php b/src/Response.php index 3f5b943..f4688f3 100644 --- a/src/Response.php +++ b/src/Response.php @@ -124,8 +124,8 @@ public function __construct( ?StreamInterface $body = null ) { $this->status = $this->filterStatus($status); - $this->headers = $headers ? $headers : new Headers([], []); - $this->body = $body ? $body : (new StreamFactory())->createStream(); + $this->headers = $headers ?: new Headers([], []); + $this->body = $body ?: (new StreamFactory())->createStream(); } /** diff --git a/src/Stream.php b/src/Stream.php index 8616989..8f8b318 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -119,7 +119,7 @@ public function getMetadata($key = null) return $this->meta; } - return isset($this->meta[$key]) ? $this->meta[$key] : null; + return $this->meta[$key] ?? null; } /** @@ -209,7 +209,7 @@ public function getSize(): ?int $stats = fstat($this->stream); if ($stats) { - $this->size = isset($stats['size']) && !$this->isPipe() ? $stats['size'] : null; + $this->size = !$this->isPipe() ? $stats['size'] : null; } } @@ -239,7 +239,7 @@ public function tell(): int */ public function eof(): bool { - return $this->stream ? feof($this->stream) : true; + return !$this->stream || feof($this->stream); } /** @@ -256,7 +256,7 @@ public function isReadable(): bool if ($this->stream) { $mode = $this->getMetadata('mode'); - if (strstr($mode, 'r') !== false || strstr($mode, '+') !== false) { + if (is_string($mode) && (strstr($mode, 'r') !== false || strstr($mode, '+') !== false)) { $this->readable = true; } } @@ -277,7 +277,7 @@ public function isWritable(): bool if ($this->stream) { $mode = $this->getMetadata('mode'); - if (strstr($mode, 'w') !== false || strstr($mode, '+') !== false) { + if (is_string($mode) && (strstr($mode, 'w') !== false || strstr($mode, '+') !== false)) { $this->writable = true; } } @@ -329,7 +329,7 @@ public function read($length): string { $data = false; - if ($this->isReadable() && $this->stream) { + if ($this->isReadable() && $this->stream && $length >= 0) { $data = fread($this->stream, $length); } @@ -410,7 +410,7 @@ public function isPipe(): bool $stats = fstat($this->stream); if (is_array($stats)) { - $this->isPipe = isset($stats['mode']) && ($stats['mode'] & self::FSTAT_MODE_S_IFIFO) !== 0; + $this->isPipe = ($stats['mode'] & self::FSTAT_MODE_S_IFIFO) !== 0; } } } diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 568e342..b34109c 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -237,7 +237,7 @@ public static function createFromGlobals(array $globals): array } if (!empty($_FILES)) { - return static::parseUploadedFiles($_FILES); + return self::parseUploadedFiles($_FILES); } return []; @@ -258,7 +258,7 @@ private static function parseUploadedFiles(array $uploadedFiles): array foreach ($uploadedFiles as $field => $uploadedFile) { if (!isset($uploadedFile['error'])) { if (is_array($uploadedFile)) { - $parsed[$field] = static::parseUploadedFiles($uploadedFile); + $parsed[$field] = self::parseUploadedFiles($uploadedFile); } continue; } @@ -267,9 +267,9 @@ private static function parseUploadedFiles(array $uploadedFiles): array if (!is_array($uploadedFile['error'])) { $parsed[$field] = new static( $uploadedFile['tmp_name'], - isset($uploadedFile['name']) ? $uploadedFile['name'] : null, - isset($uploadedFile['type']) ? $uploadedFile['type'] : null, - isset($uploadedFile['size']) ? $uploadedFile['size'] : null, + $uploadedFile['name'] ?? null, + $uploadedFile['type'] ?? null, + $uploadedFile['size'] ?? null, $uploadedFile['error'], true ); @@ -283,7 +283,7 @@ private static function parseUploadedFiles(array $uploadedFiles): array $subArray[$fileIdx]['error'] = $uploadedFile['error'][$fileIdx]; $subArray[$fileIdx]['size'] = $uploadedFile['size'][$fileIdx]; - $parsed[$field] = static::parseUploadedFiles($subArray); + $parsed[$field] = self::parseUploadedFiles($subArray); } } } diff --git a/src/Uri.php b/src/Uri.php index a22f38e..4b499cc 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -177,7 +177,7 @@ public function getUserInfo(): string { $info = $this->user; - if (isset($this->password) && $this->password !== '') { + if ($this->password !== '') { $info .= ':' . $this->password; }