Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan v1.0 #209

Merged
merged 3 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/Factory/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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) ?? '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
4 changes: 2 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static function createFromGlobals(array $globals): array
}

if (!empty($_FILES)) {
return static::parseUploadedFiles($_FILES);
return self::parseUploadedFiles($_FILES);
}

return [];
Expand All @@ -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;
}
Expand All @@ -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
);
Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function getUserInfo(): string
{
$info = $this->user;

if (isset($this->password) && $this->password !== '') {
if ($this->password !== '') {
$info .= ':' . $this->password;
}

Expand Down