From 3c6a5bb3850416b0f686de05cd10e8447b9a74c5 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Fri, 3 Feb 2023 15:04:24 +0100 Subject: [PATCH] Fix strict types errors --- src/Controllers/Admin/NewsController.php | 2 +- src/Controllers/Metrics/MetricsEngine.php | 2 +- src/Helpers/ConfigureEnvironmentServiceProvider.php | 2 +- src/Http/MessageTrait.php | 4 ++-- src/Http/Request.php | 2 +- src/Http/Validation/Rules/StringInputLength.php | 2 +- src/Middleware/ErrorHandler.php | 4 ++-- src/Middleware/LegacyMiddleware.php | 4 ++-- tests/Unit/Middleware/LegacyMiddlewareTest.php | 2 +- tests/Unit/Models/Stub/BaseModelImplementation.php | 2 +- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Controllers/Admin/NewsController.php b/src/Controllers/Admin/NewsController.php index c68cae32..0a93264d 100644 --- a/src/Controllers/Admin/NewsController.php +++ b/src/Controllers/Admin/NewsController.php @@ -34,7 +34,7 @@ class NewsController extends BaseController $newsId = $request->getAttribute('news_id'); // optional $news = $this->news->find($newsId); - $isMeeting = $request->get('meeting', false); + $isMeeting = (bool) $request->get('meeting', false); return $this->showEdit($news, $isMeeting); } diff --git a/src/Controllers/Metrics/MetricsEngine.php b/src/Controllers/Metrics/MetricsEngine.php index ecedeae1..156fe299 100644 --- a/src/Controllers/Metrics/MetricsEngine.php +++ b/src/Controllers/Metrics/MetricsEngine.php @@ -169,7 +169,7 @@ class MetricsEngine implements EngineInterface return str_replace( array_keys($replace), array_values($replace), - $value + (string) $value ); } diff --git a/src/Helpers/ConfigureEnvironmentServiceProvider.php b/src/Helpers/ConfigureEnvironmentServiceProvider.php index 0af82c6e..717b7a29 100644 --- a/src/Helpers/ConfigureEnvironmentServiceProvider.php +++ b/src/Helpers/ConfigureEnvironmentServiceProvider.php @@ -33,7 +33,7 @@ class ConfigureEnvironmentServiceProvider extends ServiceProvider protected function setTimeZone(CarbonTimeZone $timeZone): void { ini_set('date.timezone', (string) $timeZone); - date_default_timezone_set($timeZone); + date_default_timezone_set((string) $timeZone); } /** diff --git a/src/Http/MessageTrait.php b/src/Http/MessageTrait.php index 1d6bef4c..20a709e0 100644 --- a/src/Http/MessageTrait.php +++ b/src/Http/MessageTrait.php @@ -213,7 +213,7 @@ trait MessageTrait */ public function getBody(): StreamInterface { - $stream = Stream::create($this->getContent()); + $stream = Stream::create((string) $this->getContent()); $stream->rewind(); return $stream; @@ -237,7 +237,7 @@ trait MessageTrait $new = clone $this; if (method_exists($new, 'setContent')) { - $new->setContent($body); + $new->setContent((string) $body); } else { $new->content = $body; } diff --git a/src/Http/Request.php b/src/Http/Request.php index 049fbcd5..352ac0a1 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -164,7 +164,7 @@ class Request extends SymfonyRequest implements ServerRequestInterface */ public function withUri(UriInterface $uri, mixed $preserveHost = false): static { - $new = $this->create($uri); + $new = $this->create((string) $uri); if ($preserveHost) { $new->headers->set('HOST', $this->getHost()); } diff --git a/src/Http/Validation/Rules/StringInputLength.php b/src/Http/Validation/Rules/StringInputLength.php index 23695f34..b3dc0862 100644 --- a/src/Http/Validation/Rules/StringInputLength.php +++ b/src/Http/Validation/Rules/StringInputLength.php @@ -29,7 +29,7 @@ trait StringInputLength try { $input = new DateTime($input); // Min 1s diff to exclude any not auto-detected dates / times like ... - return abs((new DateTime())->diff($input)->format('%s')) > 1; + return abs((int) (new DateTime())->diff($input)->format('%s')) > 1; } catch (Exception $e) { // Ignore it } diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php index 811f21cf..ffe9aa58 100644 --- a/src/Middleware/ErrorHandler.php +++ b/src/Middleware/ErrorHandler.php @@ -65,7 +65,7 @@ class ErrorHandler implements MiddlewareInterface $statusCode = $response->getStatusCode(); $contentType = $response->getHeader('content-type'); $contentType = array_shift($contentType); - if (!$contentType && strpos($response->getBody() ?? '', 'getBody()?->getContents() ?? '', 'loader->exists($this->viewPrefix . $view)) { - return $view; + return (string) $view; } } diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index 72a990c7..1af2a8c2 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -152,10 +152,10 @@ class LegacyMiddleware implements MiddlewareInterface * * @codeCoverageIgnore */ - protected function renderPage(string $page, string $title, string $content): ResponseInterface + protected function renderPage(string | int $page, string $title, string $content): ResponseInterface { if (!empty($page) && is_int($page)) { - return response($content, (int) $page); + return response($content, $page); } if (strpos((string) $content, 'method('renderPage') ->withConsecutive( ['users', 'title', 'content'], - ['404', 'Page not found', 'It\'s not available!'] + [404, 'Page not found', 'It\'s not available!'] ) ->willReturn($response); diff --git a/tests/Unit/Models/Stub/BaseModelImplementation.php b/tests/Unit/Models/Stub/BaseModelImplementation.php index 076d29dd..89f17e5a 100644 --- a/tests/Unit/Models/Stub/BaseModelImplementation.php +++ b/tests/Unit/Models/Stub/BaseModelImplementation.php @@ -15,7 +15,7 @@ class BaseModelImplementation extends BaseModel public int $saveCount = 0; - public static QueryBuilder $queryBuilder = null; + public static ?QueryBuilder $queryBuilder = null; public function save(array $options = []): bool {