From 7479b66e82657c110ecc963406ec9427da11dc46 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:09:51 +0200 Subject: [PATCH 1/6] Update setFolderPath to accept nullable folder path Refactor setFolderPath method to accept nullable string and handle default values. --- src/Message.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Message.php b/src/Message.php index 9d15a7b8..ef62877c 100755 --- a/src/Message.php +++ b/src/Message.php @@ -297,11 +297,11 @@ public static function make(int $uid, ?int $msglist, Client $client, string $raw "message" => $client->getDefaultEvents("message"), "flag" => $client->getDefaultEvents("flag"), ]); + $instance->setClient($client); $instance->setFolderPath($client->getFolderPath()); $instance->setSequence($sequence); $instance->setFetchOption($fetch_options); - $instance->setClient($client); $instance->setSequenceId($uid, $msglist); $instance->parseRawHeader($raw_header); @@ -1465,11 +1465,21 @@ public function getFolderPath(): string { /** * Set the message path aka folder path - * @param $folder_path + * @param ?string $folder_path * * @return Message */ - public function setFolderPath($folder_path): Message { + public function setFolderPath(?string $folder_path): Message { + if ($folder_path === null || $folder_path === '') { + $folder_path = $this->client?->getFolderPath(); + } + if ($folder_path === null || $folder_path === '') { + $folder_path = (string)$this->config->get('options.common_folders.inbox', 'INBOX'); + } + if ($folder_path === '') { + $folder_path = 'INBOX'; + } + $this->folder_path = $folder_path; return $this; From d5f234ef21b2e1d520026c85191d971845b8d50e Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:12:00 +0200 Subject: [PATCH 2/6] Handle active folder in checkConnection method --- src/Client.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Client.php b/src/Client.php index 5eb26ff2..4936e876 100755 --- a/src/Client.php +++ b/src/Client.php @@ -400,13 +400,20 @@ public function isConnected(): bool { * @throws ResponseException */ public function checkConnection(): bool { + $active_folder = $this->active_folder; try { if (!$this->isConnected()) { $this->connect(); + if ($active_folder !== null) { + $this->openFolder($active_folder, true); + } return true; } } catch (\Throwable) { $this->connect(); + if ($active_folder !== null) { + $this->openFolder($active_folder, true); + } } return false; } From 5ddd89af39b9bec72a8f1044f5fbade595b1c8e4 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:21:55 +0200 Subject: [PATCH 3/6] Added test to demonstrate fix --- tests/issues/PR619Test.php | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/issues/PR619Test.php diff --git a/tests/issues/PR619Test.php b/tests/issues/PR619Test.php new file mode 100644 index 00000000..b43b3bf1 --- /dev/null +++ b/tests/issues/PR619Test.php @@ -0,0 +1,63 @@ + [ + 'default' => [ + 'host' => 'localhost', + 'protocol' => 'imap', + 'encryption' => 'ssl', + 'username' => 'foo@example.com', + 'password' => 'secret', + ], + ], + ]); + + $client = new Client($config); + + $protocol = $this->createStub(ImapProtocol::class); + $protocol->method('connected')->willReturn(true); + $client->connection = $protocol; + + // Simulate the state after an implicit reconnect: + // Client::connect() always calls disconnect() first, + // and disconnect() resets active_folder to null. + $ref = new ReflectionClass($client); + $prop = $ref->getProperty('active_folder'); + $prop->setAccessible(true); + $prop->setValue($client, null); + + return $client; + } + + public function testMakeThrowsTypeErrorWhenActiveFolderIsNull(): void + { + $this->expectException(\TypeError::class); + + $client = $this->makeClientWithNullActiveFolder(); + Message::make(1, 0, $client, "Subject: Test\r\n\r\n", '', []); + } + + public function testMakeSucceedsAfterFix(): void + { + $client = $this->makeClientWithNullActiveFolder(); + + $message = Message::make(1, 0, $client, "Subject: Test\r\n\r\n", '', []); + + $this->assertInstanceOf(Message::class, $message); + $this->assertIsString($message->getFolderPath()); + $this->assertNotEmpty($message->getFolderPath()); + } +} From cbd04098ab45fa812111ea5fa6870f0f22c0e612 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:27:01 +0200 Subject: [PATCH 4/6] Renamed test file --- tests/issues/{PR619Test.php => PR621Test.php} | 3 +++ 1 file changed, 3 insertions(+) rename tests/issues/{PR619Test.php => PR621Test.php} (96%) diff --git a/tests/issues/PR619Test.php b/tests/issues/PR621Test.php similarity index 96% rename from tests/issues/PR619Test.php rename to tests/issues/PR621Test.php index b43b3bf1..4c2d965a 100644 --- a/tests/issues/PR619Test.php +++ b/tests/issues/PR621Test.php @@ -9,6 +9,9 @@ use Webklex\PHPIMAP\Message; use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol; +/** + * @see https://github.com/Webklex/php-imap/pull/621 + */ class PR619Test extends TestCase { private function makeClientWithNullActiveFolder(): Client From adae338f981fe1f603a9bc0e67b464d22bc664f9 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:29:44 +0200 Subject: [PATCH 5/6] Make folder_path nullable in Message class --- src/Message.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Message.php b/src/Message.php index ef62877c..7a651629 100755 --- a/src/Message.php +++ b/src/Message.php @@ -130,10 +130,8 @@ class Message { /** * The message folder path - * - * @var string $folder_path */ - protected string $folder_path; + protected ?string $folder_path; /** * Fetch body options From afb2157a13e118b6e32153631e66198b4d2ab665 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 5 Jun 2026 16:32:09 +0200 Subject: [PATCH 6/6] Initialize folder_path with an empty string --- src/Message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message.php b/src/Message.php index 7a651629..a6b249b8 100755 --- a/src/Message.php +++ b/src/Message.php @@ -131,7 +131,7 @@ class Message { /** * The message folder path */ - protected ?string $folder_path; + protected string $folder_path = ''; /** * Fetch body options