Skip to content

Commit 9857a1d

Browse files
authored
Issue #680: Fix ValueError for imap_ping(), when the connection is already closed
* Issue #680: Fix ValueError for `imap_ping()`, when the connection is already closed
1 parent 13bdfa9 commit 9857a1d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/PhpImap/Mailbox.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,17 @@ public function getImapStream(bool $forceConnection = true)
476476

477477
public function hasImapStream(): bool
478478
{
479-
return (\is_resource($this->imapStream) || $this->imapStream instanceof \IMAP\Connection) && \imap_ping($this->imapStream);
479+
try {
480+
return (\is_resource($this->imapStream) || $this->imapStream instanceof \IMAP\Connection) && \imap_ping($this->imapStream);
481+
} catch (\Error $exception) {
482+
// From PHP 8.1.10 imap_ping() on a closed stream throws a ValueError. See #680.
483+
$valueError = '\ValueError';
484+
if (class_exists($valueError) && $exception instanceof $valueError) {
485+
return false;
486+
}
487+
488+
throw $exception;
489+
}
480490
}
481491

482492
/**

0 commit comments

Comments
 (0)