diff --git a/lib/functions.php b/lib/functions.php index 772723a..a41864c 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -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]; } @@ -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']; } @@ -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', @@ -157,7 +157,7 @@ function normalize(string $uri): string } } - if ($parts['host']) { + if (null !== $parts['host']) { $parts['host'] = strtolower($parts['host']); } @@ -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 @@ -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('/^(? [a-zA-Z]:([\/\\\\].*)?)$/x', $result['path'])) { + 1 === preg_match('/^(? [a-zA-Z]:([\/\\\\].*)?)$/x', $result['path'])) { $result['path'] = '/'.$result['path']; $result['host'] = ''; } @@ -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; } @@ -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]]; } @@ -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); @@ -382,10 +382,10 @@ function ($matches) { (?: : (? [0-9]+))? (? / .*)? $%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'])) { @@ -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 { diff --git a/phpstan.neon b/phpstan.neon index 07873f2..5c291a6 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +1,6 @@ parameters: level: 9 + ignoreErrors: + - + message: "#^.* will always evaluate to true\\.$#" + path: tests/*