Skip to content

Commit

Permalink
First pass at fixing things reported by phpstan-strict-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-davis committed Feb 13, 2023
1 parent 01ce5c0 commit cc7f6f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ function resolve(string $basePath, string $newPath): string

// If the new path defines a scheme, it's absolute and we can just return
// that.
if ($delta['scheme']) {
if (null !== $delta['scheme']) {
return build($delta);
}

$base = parse($basePath);
$pick = function ($part) use ($base, $delta) {
if ($delta[$part]) {
if (null !== $delta[$part]) {
return $delta[$part];
} elseif ($base[$part]) {
} elseif (null !== $base[$part]) {
return $base[$part];
}

Expand Down Expand Up @@ -85,13 +85,13 @@ function resolve(string $basePath, string $newPath): string

// If the source url ended with a /, we want to preserve that.
$newParts['path'] = 0 === strpos($path, '/') ? $path : '/'.$path;
if ($delta['query']) {
if (null !== $delta['query']) {
$newParts['query'] = $delta['query'];
} elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) {
// Keep the old query if host and path didn't change
$newParts['query'] = $base['query'];
}
if ($delta['fragment']) {
if (null !== $delta['fragment']) {
$newParts['fragment'] = $delta['fragment'];
}

Expand Down Expand Up @@ -134,7 +134,7 @@ function normalize(string $uri): string
$parts['path'] = '/'.implode('/', $newPathParts);
}

if ($parts['scheme']) {
if (null !== $parts['scheme']) {
$parts['scheme'] = strtolower($parts['scheme']);
$defaultPorts = [
'http' => '80',
Expand All @@ -157,7 +157,7 @@ function normalize(string $uri): string
}
}

if ($parts['host']) {
if (null !== $parts['host']) {
$parts['host'] = strtolower($parts['host']);
}

Expand Down Expand Up @@ -201,7 +201,7 @@ function ($matches) {
}

$result = parse_url($uri);
if (!$result) {
if (false === $result) {
$result = _parse_fallback($uri);
} else {
// Add empty host and leading slash to Windows file paths
Expand All @@ -212,7 +212,7 @@ function ($matches) {
// that is used as the regex. The 2 backslash are then the way to get 1 backslash
// character into the character set "a forward slash or a backslash"
if (isset($result['scheme']) && 'file' === $result['scheme'] && isset($result['path']) &&
preg_match('/^(?<windows_path> [a-zA-Z]:([\/\\\\].*)?)$/x', $result['path'])) {
1 === preg_match('/^(?<windows_path> [a-zA-Z]:([\/\\\\].*)?)$/x', $result['path'])) {
$result['path'] = '/'.$result['path'];
$result['host'] = '';
}
Expand Down Expand Up @@ -265,7 +265,7 @@ function build(array $parts): string
// If there's a scheme, there's also a host.
$uri = $parts['scheme'].':';
}
if ($authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) {
if ('' !== $authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) {
// No scheme, but there is a host.
$uri .= '//'.$authority;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ function build(array $parts): string
function split(string $path): array
{
$matches = [];
if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) {
if (1 === preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) {
return [$matches[1], $matches[2]];
}

Expand Down Expand Up @@ -353,7 +353,7 @@ function ($matches) {
'query' => null,
];

if (preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) {
if (1 === preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) {
$result['scheme'] = $matches[1];
// Take what's left.
$uri = substr($uri, strlen($result['scheme']) + 1);
Expand Down Expand Up @@ -382,10 +382,10 @@ function ($matches) {
(?: : (?<port> [0-9]+))?
(?<path> / .*)?
$%x';
if (!preg_match($regex, $uri, $matches)) {
if (1 !== preg_match($regex, $uri, $matches)) {
throw new InvalidUriException('Invalid, or could not parse URI');
}
if ($matches['host']) {
if (isset($matches['host']) && '' !== $matches['host']) {
$result['host'] = $matches['host'];
}
if (isset($matches['port'])) {
Expand All @@ -394,10 +394,10 @@ function ($matches) {
if (isset($matches['path'])) {
$result['path'] = $matches['path'];
}
if ($matches['user']) {
if (isset($matches['user']) && '' !== $matches['user']) {
$result['user'] = $matches['user'];
}
if ($matches['pass']) {
if (isset($matches['pass']) && '' !== $matches['pass']) {
$result['pass'] = $matches['pass'];
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
parameters:
level: 9
ignoreErrors:
-
message: "#^.* will always evaluate to true\\.$#"
path: tests/*

0 comments on commit cc7f6f2

Please sign in to comment.