From 8ef7776fb85905cbc620eb7c15abd1aea08fd211 Mon Sep 17 00:00:00 2001 From: christopher Date: Mon, 22 Sep 2025 15:03:44 +0200 Subject: [PATCH] Implement PHPStan up to level 2 --- phpstan.neon | 2 +- src/Collections/PaginatedCollection.php | 2 +- src/HasParsedMessage.php | 6 ++++-- src/Idle.php | 5 ++++- src/MessageQuery.php | 9 ++++++++- src/Pagination/LengthAwarePaginator.php | 2 +- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index ebf7637..0f0181e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 1 + level: 2 ignoreErrors: - identifier: new.static diff --git a/src/Collections/PaginatedCollection.php b/src/Collections/PaginatedCollection.php index c70dfcb..f9d21bd 100644 --- a/src/Collections/PaginatedCollection.php +++ b/src/Collections/PaginatedCollection.php @@ -8,7 +8,7 @@ /** * @template TKey of array-key * - * @template-covariant TValue + * @template TValue * * @template-extends Collection */ diff --git a/src/HasParsedMessage.php b/src/HasParsedMessage.php index b253df1..a4a0cad 100644 --- a/src/HasParsedMessage.php +++ b/src/HasParsedMessage.php @@ -6,6 +6,7 @@ use Carbon\CarbonInterface; use DirectoryTree\ImapEngine\Exceptions\RuntimeException; use GuzzleHttp\Psr7\Utils; +use ZBateson\MailMimeParser\Header\DateHeader; use ZBateson\MailMimeParser\Header\HeaderConsts; use ZBateson\MailMimeParser\Header\IHeader; use ZBateson\MailMimeParser\Header\IHeaderPart; @@ -27,8 +28,9 @@ trait HasParsedMessage */ public function date(): ?CarbonInterface { - if ($date = $this->header(HeaderConsts::DATE)?->getDateTime()) { - return Carbon::instance($date); + $dateHeader = $this->header(HeaderConsts::DATE); + if ($dateHeader instanceof DateHeader) { + return Carbon::instance($dateHeader->getDateTime()); } return null; diff --git a/src/Idle.php b/src/Idle.php index 60f8a9c..5204e45 100644 --- a/src/Idle.php +++ b/src/Idle.php @@ -6,6 +6,7 @@ use Carbon\CarbonInterface; use Closure; use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse; +use DirectoryTree\ImapEngine\Connection\Tokens\Atom; use DirectoryTree\ImapEngine\Exceptions\Exception; use DirectoryTree\ImapEngine\Exceptions\ImapConnectionClosedException; use DirectoryTree\ImapEngine\Exceptions\ImapConnectionTimedOutException; @@ -59,7 +60,9 @@ protected function listen(callable $callback, CarbonInterface $ttl): void continue; } - if ($response->tokenAt(2)?->is('EXISTS')) { + $possibleExistsToken = $response->tokenAt(2); + + if ($possibleExistsToken instanceof Atom && $possibleExistsToken->is('EXISTS')) { $msgn = (int) $response->tokenAt(1)->value; $callback($msgn); diff --git a/src/MessageQuery.php b/src/MessageQuery.php index 204a769..dbc291b 100644 --- a/src/MessageQuery.php +++ b/src/MessageQuery.php @@ -6,11 +6,13 @@ use DirectoryTree\ImapEngine\Collections\ResponseCollection; use DirectoryTree\ImapEngine\Connection\ConnectionInterface; use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder; +use DirectoryTree\ImapEngine\Connection\Responses\Data\ListData; use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse; use DirectoryTree\ImapEngine\Connection\Tokens\Token; use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; use DirectoryTree\ImapEngine\Enums\ImapFlag; use DirectoryTree\ImapEngine\Exceptions\ImapCommandException; +use DirectoryTree\ImapEngine\Exceptions\RuntimeException; use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator; use DirectoryTree\ImapEngine\Support\Str; use Illuminate\Support\Collection; @@ -189,7 +191,8 @@ public function findOrFail(int $id, ImapFetchIdentifier $identifier = ImapFetchI public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentifier::Uid): ?MessageInterface { /** @var UntaggedResponse $response */ - if (! $response = $this->id($id, $identifier)->first()) { + $response = $this->id($id, $identifier)->first(); + if (! $response) { return null; } @@ -294,6 +297,10 @@ protected function fetch(Collection $messages): array return $this->connection()->fetch($fetch, $uids->all())->mapWithKeys(function (UntaggedResponse $response) { $data = $response->tokenAt(3); + if (!$data instanceof ListData) { + throw new RuntimeException("Invalid data type at index 3"); + } + $uid = $data->lookup('UID')->value; return [ diff --git a/src/Pagination/LengthAwarePaginator.php b/src/Pagination/LengthAwarePaginator.php index 9788c4b..3dbf3dd 100644 --- a/src/Pagination/LengthAwarePaginator.php +++ b/src/Pagination/LengthAwarePaginator.php @@ -10,7 +10,7 @@ /** * @template TKey of array-key * - * @template-covariant TValue + * @template TValue * * @template-implements Arrayable */