Skip to content

Commit

Permalink
Merge pull request #203 from phil-davis/phpstan-phpunit
Browse files Browse the repository at this point in the history
Add phpstan phpunit strict-rules
  • Loading branch information
phil-davis authored Jun 27, 2023
2 parents fcc83fa + dbc86e4 commit 5220b2b
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 46 deletions.
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"require-dev" : {
"friendsofphp/php-cs-fixer": "^3.14",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.4",
"phpstan/extension-installer": "^1.2",
"phpunit/phpunit" : "^9.6"
},
"suggest" : {
Expand Down Expand Up @@ -60,5 +63,10 @@
"composer cs-fixer",
"composer phpunit"
]
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
6 changes: 4 additions & 2 deletions lib/Auth/AWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function validate(string $secretKey): bool
{
$contentMD5 = $this->request->getHeader('Content-MD5');

if ($contentMD5) {
if (null !== $contentMD5) {
// We need to validate the integrity of the request
$body = $this->request->getBody();
$this->request->setBody($body);
Expand All @@ -97,7 +97,9 @@ public function validate(string $secretKey): bool
}
}

if (!$requestDate = $this->request->getHeader('x-amz-date')) {
$requestDate = $this->request->getHeader('x-amz-date');

if (null === $requestDate) {
$requestDate = $this->request->getHeader('Date');
}

Expand Down
10 changes: 8 additions & 2 deletions lib/Auth/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ public function getCredentials(): ?array
{
$auth = $this->request->getHeader('Authorization');

if (!$auth) {
if (null === $auth) {
return null;
}

if ('basic ' !== strtolower(substr($auth, 0, 6))) {
return null;
}

$credentials = explode(':', base64_decode(substr($auth, 6)), 2);
$decodedAuth = base64_decode(substr($auth, 6), true);

if (false === $decodedAuth) {
return null;
}

$credentials = explode(':', $decodedAuth, 2);

if (2 !== count($credentials)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/Auth/Bearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getToken(): ?string
{
$auth = $this->request->getHeader('Authorization');

if (!$auth) {
if (null === $auth) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Auth/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ protected function validate(): bool

if ('auth-int' === $this->digestParts['qop']) {
// Making sure we support this qop value
if (!($this->qop & self::QOP_AUTHINT)) {
if (0 === ($this->qop & self::QOP_AUTHINT)) {
return false;
}
// We need to add an MD5 of the entire request body to the A2 part of the hash
$body = $this->request->getBody();
$this->request->setBody($body);
$A2 .= ':'.md5($body);
} elseif (!($this->qop & self::QOP_AUTH)) {
} elseif (0 === ($this->qop & self::QOP_AUTH)) {
return false;
}

Expand Down Expand Up @@ -200,10 +200,10 @@ 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[3];
$data[$m[1]] = ('' !== $m[2]) ? $m[2] : $m[3];
unset($needed_parts[$m[1]]);
}

return $needed_parts ? false : $data;
return (count($needed_parts) > 0) ? false : $data;
}
}
28 changes: 16 additions & 12 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function send(RequestInterface $request): ResponseInterface
// open_basedir.
//
// https://github.com/fruux/sabre-http/issues/12
if ($redirects < $this->maxRedirects && in_array($code, [301, 302, 307, 308])) {
if ($redirects < $this->maxRedirects && in_array($code, [301, 302, 307, 308], true)) {
$oldLocation = $request->getUrl();

// Creating a new instance of the request object.
Expand Down Expand Up @@ -194,7 +194,7 @@ public function sendAsync(RequestInterface $request, callable $success = null, c
public function poll(): bool
{
// nothing to do?
if (!$this->curlMultiMap) {
if (0 === count($this->curlMultiMap)) {
return false;
}

Expand All @@ -216,6 +216,7 @@ public function poll(): bool

if ($status && CURLMSG_DONE === $status['msg']) {
$resourceId = (int) $status['handle'];

list(
$request,
$successCallback,
Expand All @@ -239,7 +240,7 @@ public function poll(): bool

$curlResult['request'] = $request;

if ($errorCallback) {
if (is_callable($errorCallback)) {
$errorCallback($curlResult);
}
} elseif (self::STATUS_HTTPERROR === $curlResult['status']) {
Expand All @@ -254,13 +255,13 @@ public function poll(): bool

$curlResult['request'] = $request;

if ($errorCallback) {
if (is_callable($errorCallback)) {
$errorCallback($curlResult);
}
} else {
$this->emit('afterRequest', [$request, $curlResult['response']]);

if ($successCallback) {
if (is_callable($successCallback)) {
$successCallback($curlResult['response']);
}
}
Expand Down Expand Up @@ -314,7 +315,7 @@ protected function doRequest(RequestInterface $request): ResponseInterface
{
$settings = $this->createCurlSettingsArray($request);

if (!$this->curlHandle) {
if (null === $this->curlHandle) {
$this->curlHandle = curl_init();
} else {
curl_reset($this->curlHandle);
Expand Down Expand Up @@ -353,7 +354,7 @@ protected function doRequest(RequestInterface $request): ResponseInterface
* Has a list of curl handles, as well as their associated success and
* error callbacks.
*
* @var array<int, mixed>
* @var array<int, array{0: RequestInterface, 1: callable, 2: callable, 3: int}>
*/
private array $curlMultiMap = [];

Expand Down Expand Up @@ -475,7 +476,7 @@ protected function parseCurlResponse(array $headerLines, string $body, $curlHand
$curlErrMsg
) = $this->curlStuff($curlHandle);

if ($curlErrNo) {
if (0 !== $curlErrNo) {
return [
'status' => self::STATUS_CURLERROR,
'curl_errno' => $curlErrNo,
Expand Down Expand Up @@ -532,7 +533,7 @@ protected function parseCurlResult(string $response, $curlHandle): array
$curlErrMsg
) = $this->curlStuff($curlHandle);

if ($curlErrNo) {
if (0 !== $curlErrNo) {
return [
'status' => self::STATUS_CURLERROR,
'curl_errno' => $curlErrNo,
Expand All @@ -541,10 +542,13 @@ protected function parseCurlResult(string $response, $curlHandle): array
}

$headerBlob = substr($response, 0, $curlInfo['header_size']);
// In the case of 204 No Content, strlen($response) == $curlInfo['header_size].
// In the case of 204 No Content, strlen($response) == $curlInfo['header_size'].
// 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']) ?: '';
$responseBody = substr($response, $curlInfo['header_size']);
if (false === $responseBody) {
$responseBody = '';
}

unset($response);

Expand All @@ -570,7 +574,7 @@ protected function parseCurlResult(string $response, $curlHandle): array
*/
protected function sendAsyncInternal(RequestInterface $request, callable $success, callable $error, int $retryCount = 0): void
{
if (!$this->curlMultiHandle) {
if (null === $this->curlMultiHandle) {
$this->curlMultiHandle = curl_multi_init();
}
$curl = curl_init();
Expand Down
2 changes: 1 addition & 1 deletion lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function setAbsoluteUrl(string $url): void
*/
public function getAbsoluteUrl(): string
{
if (!$this->absoluteUrl) {
if ((null === $this->absoluteUrl) || ('' === $this->absoluteUrl)) {
// Guessing we're a http endpoint.
$this->absoluteUrl = 'http://'.
($this->getHeader('Host') ?? 'localhost').
Expand Down
2 changes: 1 addition & 1 deletion lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Response extends Message implements ResponseInterface
/**
* Creates the response object.
*
* @param string|int $status
* @param string|int|null $status
* @param array<string, mixed>|null $headers
* @param resource|string|callable|null $body
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static function createFromServerArray(array $serverArray): Request
break;

case 'HTTPS':
if (!empty($value) && 'off' !== $value) {
if ('' !== $value && 'off' !== $value) {
$protocol = 'https';
}
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function toDate(\DateTime $dateTime): string
*/
function negotiateContentType(?string $acceptHeaderValue, array $availableOptions): ?string
{
if (!$acceptHeaderValue) {
if ((null === $acceptHeaderValue) || ('' === $acceptHeaderValue)) {
// Grabbing the first in the list.
return reset($availableOptions);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ function parsePrefer($input): array
} else {
$value = true;
}
$output[strtolower($matches['name'])] = empty($value) ? true : $value;
$output[strtolower($matches['name'])] = ('' === $value) ? true : $value;
break;
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ function parsePrefer($input): array
function getHeaderValues($values, $values2 = null): array
{
$values = (array) $values;
if ($values2) {
if (null !== $values2) {
$values = array_merge($values, (array) $values2);
}

Expand Down
10 changes: 9 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ parameters:
path: lib/Client.php
-
message: "#^Left side of || is always false.$#"
count: 2
count: 6
path: lib/Client.php
-
message: "#^Casting to string something that's already string.$#"
count: 1
path: lib/Sapi.php
-
message: "#^Strict comparison using === between null and array<string, mixed> will always evaluate to false.$#"
count: 1
path: lib/functions.php
-
message: "#^.* will always evaluate to true\\.$#"
count: 4
path: tests/*
8 changes: 4 additions & 4 deletions tests/HTTP/Auth/AWSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testFutureDate(): void
$contentMD5 = base64_encode(md5($content, true));

$date = new \DateTime('@'.(time() + (60 * 20)));
$date->setTimeZone(new \DateTimeZone('GMT'));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');

$this->request->setMethod('POST');
Expand All @@ -117,7 +117,7 @@ public function testPastDate(): void
$contentMD5 = base64_encode(md5($content, true));

$date = new \DateTime('@'.(time() - (60 * 20)));
$date->setTimeZone(new \DateTimeZone('GMT'));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');

$this->request->setMethod('POST');
Expand Down Expand Up @@ -145,7 +145,7 @@ public function testIncorrectSignature(): void
$contentMD5 = base64_encode(md5($content, true));

$date = new \DateTime('now');
$date->setTimeZone(new \DateTimeZone('GMT'));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');

$this->request->setUrl('/');
Expand All @@ -172,7 +172,7 @@ public function testValidRequest(): void
$contentMD5 = base64_encode(md5($content, true));

$date = new \DateTime('now');
$date->setTimeZone(new \DateTimeZone('GMT'));
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');

$sig = base64_encode($this->hmacsha1($secretKey,
Expand Down
Loading

0 comments on commit 5220b2b

Please sign in to comment.