Skip to content

Commit

Permalink
Fix potential null values passed to non nullable internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Jun 15, 2024
1 parent 2ef1495 commit 855981d
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion formwork/src/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Http/HeadersData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 5 additions & 5 deletions formwork/src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)]);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions formwork/src/Interpolator/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '\'"')));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Pages/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
}
}
}
4 changes: 2 additions & 2 deletions formwork/src/Panel/Controllers/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Panel/Controllers/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 3 additions & 3 deletions formwork/src/Utils/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/**
Expand Down

0 comments on commit 855981d

Please sign in to comment.