From 855981d20a4c53a95ccaf5bf5c27a89c0e7fa91c Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:56:45 +0200 Subject: [PATCH] Fix potential null values passed to non nullable internal functions --- formwork/src/Commands/ServeCommand.php | 2 +- formwork/src/Controllers/PageController.php | 4 ++-- formwork/src/Http/Client.php | 2 +- formwork/src/Http/HeadersData.php | 2 +- formwork/src/Http/Request.php | 10 +++++----- formwork/src/Interpolator/Parser.php | 3 +-- formwork/src/Pages/Site.php | 2 +- formwork/src/Panel/Controllers/BackupController.php | 4 ++-- formwork/src/Panel/Controllers/PagesController.php | 2 +- formwork/src/Router/Router.php | 2 +- formwork/src/Utils/Uri.php | 6 +++--- 11 files changed, 19 insertions(+), 20 deletions(-) diff --git a/formwork/src/Commands/ServeCommand.php b/formwork/src/Commands/ServeCommand.php index 76c2689f9..30cde0443 100644 --- a/formwork/src/Commands/ServeCommand.php +++ b/formwork/src/Commands/ServeCommand.php @@ -106,7 +106,7 @@ protected function handleOutput(array $lines): void $this->climate->br(); $input = $this->climate->input('Enter another port:'); - $input->accept(fn ($response) => ctype_digit($response)); + $input->accept(fn (string $response) => ctype_digit($response)); $this->port = (int) $input->prompt(); diff --git a/formwork/src/Controllers/PageController.php b/formwork/src/Controllers/PageController.php index 64734622e..af01fce7e 100644 --- a/formwork/src/Controllers/PageController.php +++ b/formwork/src/Controllers/PageController.php @@ -72,8 +72,8 @@ public function load(RouteParams $routeParams, ViewFactory $viewFactory): Respon return $this->getPageResponse($page); } } else { - $filename = basename($route); - $upperLevel = dirname($route); + $filename = basename((string) $route); + $upperLevel = dirname((string) $route); if ($upperLevel === '.') { $upperLevel = $this->config->get('system.pages.index'); diff --git a/formwork/src/Http/Client.php b/formwork/src/Http/Client.php index 89ea35a93..2fdb15c24 100644 --- a/formwork/src/Http/Client.php +++ b/formwork/src/Http/Client.php @@ -184,7 +184,7 @@ protected function connect(string $uri, array $options = []): array $length = $currentResponse['headers']['Content-Length'] ?? null; - if (strtoupper($options['method']) === 'HEAD') { + if (strtoupper((string) $options['method']) === 'HEAD') { $length = 0; } diff --git a/formwork/src/Http/HeadersData.php b/formwork/src/Http/HeadersData.php index dbdf53a61..3b1bef016 100644 --- a/formwork/src/Http/HeadersData.php +++ b/formwork/src/Http/HeadersData.php @@ -19,7 +19,7 @@ public function __construct(array $data) */ protected function initialize(array $headers): void { - $this->data = Arr::mapKeys($headers, fn ($key) => str_replace('_', '-', ucwords(strtolower($key), '_'))); + $this->data = Arr::mapKeys($headers, fn (string $key) => str_replace('_', '-', ucwords(strtolower($key), '_'))); ksort($this->data); } } diff --git a/formwork/src/Http/Request.php b/formwork/src/Http/Request.php index 98478bb53..0569f821a 100644 --- a/formwork/src/Http/Request.php +++ b/formwork/src/Http/Request.php @@ -87,7 +87,7 @@ public function baseUri(): string { $scheme = $this->isSecure() ? 'https' : 'http'; - $host = strtolower($this->server->get('SERVER_NAME')); + $host = strtolower((string) $this->server->get('SERVER_NAME')); $port = (int) $this->server->get('SERVER_PORT', 80); @@ -105,7 +105,7 @@ public function baseUri(): string public function uri(): string { - $uri = urldecode($this->server->get('REQUEST_URI')); + $uri = urldecode((string) $this->server->get('REQUEST_URI')); $root = $this->root(); if (Str::startsWith($uri, $root)) { return Path::join(['/', Str::removeStart($uri, $root)]); @@ -227,7 +227,7 @@ public function content(): ?string */ public function isSecure(): bool { - $https = $this->server->has('HTTPS') && strtolower($this->server->get('HTTPS')) !== 'off'; + $https = $this->server->has('HTTPS') && strtolower((string) $this->server->get('HTTPS')) !== 'off'; if ($this->isFromTrustedProxy() && ($proto = $this->getForwardedDirective('proto')) !== []) { return in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true); @@ -246,7 +246,7 @@ public function isLocalhost(): bool public function isXmlHttpRequest(): bool { - return strtolower($this->headers->get('X-Requested-With', '')) === 'xmlhttprequest'; + return strtolower((string) $this->headers->get('X-Requested-With')) === 'xmlhttprequest'; } public function type(): RequestType @@ -348,7 +348,7 @@ protected function getForwardedDirectives(): array $directives = []; if (($forwardedHeader = $this->headers->get('Forwarded')) !== null) { - $directives = array_map(Header::combine(...), Header::split(strtolower($forwardedHeader), ',;=')); + $directives = array_map(Header::combine(...), Header::split(strtolower((string) $forwardedHeader), ',;=')); } else { foreach (self::FORWARDED_DIRECTIVES as $name) { if (($xForwarededHeader = $this->headers->get('X-Forwarded-' . ucfirst($name))) !== null) { diff --git a/formwork/src/Interpolator/Parser.php b/formwork/src/Interpolator/Parser.php index 97a902a33..bc5d273f8 100644 --- a/formwork/src/Interpolator/Parser.php +++ b/formwork/src/Interpolator/Parser.php @@ -80,8 +80,7 @@ protected function parseNumberToken(): NumberNode protected function parseStringToken(): StringNode { $token = $this->tokenStream->expect(Token::TYPE_STRING); - // @phpstan-ignore-next-line - return new StringNode(stripcslashes(trim($token->value(), '\'"'))); + return new StringNode(stripcslashes(trim((string) $token->value(), '\'"'))); } /** diff --git a/formwork/src/Pages/Site.php b/formwork/src/Pages/Site.php index 27c79ad2d..f1643815e 100644 --- a/formwork/src/Pages/Site.php +++ b/formwork/src/Pages/Site.php @@ -473,7 +473,7 @@ protected function loadRouteAliases(): void { $this->routeAliases = []; foreach ($this->data['routeAliases'] as $from => $to) { - $this->routeAliases[trim($from, '/')] = trim($to, '/'); + $this->routeAliases[trim((string) $from, '/')] = trim((string) $to, '/'); } } } diff --git a/formwork/src/Panel/Controllers/BackupController.php b/formwork/src/Panel/Controllers/BackupController.php index 8a780915d..fabc211b2 100644 --- a/formwork/src/Panel/Controllers/BackupController.php +++ b/formwork/src/Panel/Controllers/BackupController.php @@ -46,7 +46,7 @@ public function make(Config $config): JsonResponse public function download(RouteParams $routeParams): Response { $this->ensurePermission('backup.download'); - $file = FileSystem::joinPaths($this->config->get('system.backup.path'), basename(base64_decode($routeParams->get('backup')))); + $file = FileSystem::joinPaths($this->config->get('system.backup.path'), basename(base64_decode((string) $routeParams->get('backup')))); try { if (FileSystem::isFile($file, assertExists: false)) { return new FileResponse($file, download: true); @@ -64,7 +64,7 @@ public function download(RouteParams $routeParams): Response public function delete(RouteParams $routeParams): Response { $this->ensurePermission('backup.download'); - $file = FileSystem::joinPaths($this->config->get('system.backup.path'), basename(base64_decode($routeParams->get('backup')))); + $file = FileSystem::joinPaths($this->config->get('system.backup.path'), basename(base64_decode((string) $routeParams->get('backup')))); try { if (FileSystem::isFile($file, assertExists: false)) { FileSystem::delete($file); diff --git a/formwork/src/Panel/Controllers/PagesController.php b/formwork/src/Panel/Controllers/PagesController.php index 55b5cfc93..ff59561d9 100644 --- a/formwork/src/Panel/Controllers/PagesController.php +++ b/formwork/src/Panel/Controllers/PagesController.php @@ -256,7 +256,7 @@ public function reorder(): JsonResponse $pageCollection->moveItem($from, $to); foreach ($pageCollection->values() as $i => $page) { - $name = basename($page->relativePath()); + $name = basename((string) $page->relativePath()); $newName = preg_replace(Page::NUM_REGEX, $i + 1 . '-', $name) ?? throw new RuntimeException(sprintf('Replacement failed with error: %s', preg_last_error_msg())); diff --git a/formwork/src/Router/Router.php b/formwork/src/Router/Router.php index d69990cca..fb7292251 100644 --- a/formwork/src/Router/Router.php +++ b/formwork/src/Router/Router.php @@ -328,7 +328,7 @@ protected function generateRoute(Route $route, array $params): string $pattern = $this->resolvePatternShortcut($pattern); - if (!(bool) preg_match('~^' . trim($pattern, '^$') . '$~', $params[$param])) { + if (!(bool) preg_match('~^' . trim($pattern, '^$') . '$~', (string) $params[$param])) { throw new InvalidArgumentException(sprintf('Invalid value for param "%s"', $param)); } diff --git a/formwork/src/Utils/Uri.php b/formwork/src/Utils/Uri.php index 8792a9cdb..8929d3861 100644 --- a/formwork/src/Utils/Uri.php +++ b/formwork/src/Utils/Uri.php @@ -40,7 +40,7 @@ public static function scheme(?string $uri = null): ?string return App::instance()->request()->isSecure() ? 'https' : 'http'; } $scheme = static::parseComponent($uri, PHP_URL_SCHEME); - return $scheme !== null ? strtolower($scheme) : null; + return $scheme !== null ? strtolower((string) $scheme) : null; } /** @@ -49,10 +49,10 @@ public static function scheme(?string $uri = null): ?string public static function host(?string $uri = null): ?string { if ($uri === null) { - return strtolower($_SERVER['SERVER_NAME']); + return strtolower((string) $_SERVER['SERVER_NAME']); } $host = static::parseComponent($uri, PHP_URL_HOST); - return $host !== null ? strtolower($host) : null; + return $host !== null ? strtolower((string) $host) : null; } /**