From b5a868080f221bfc42048c3a4221429b55a83bf0 Mon Sep 17 00:00:00 2001 From: Sebbo94BY Date: Sun, 10 May 2026 20:20:02 +0200 Subject: [PATCH] Fix boolean received error in getFileInfo() The change is in `src/PhpImap/IncomingMailAttachment.php:132: getFileInfo()` now routes the finfo lookup through a small helper and returns `''` if detection fails instead of propagating false into a string return type. I also added `tests/unit/IncomingMailAttachmentTest.php:1` with one regression test for the failure branch and one sanity check for the normal branch. --- src/PhpImap/IncomingMailAttachment.php | 16 +++++++- tests/unit/IncomingMailAttachmentTest.php | 46 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/unit/IncomingMailAttachmentTest.php diff --git a/src/PhpImap/IncomingMailAttachment.php b/src/PhpImap/IncomingMailAttachment.php index f8bbc50c..5f871b07 100644 --- a/src/PhpImap/IncomingMailAttachment.php +++ b/src/PhpImap/IncomingMailAttachment.php @@ -130,10 +130,24 @@ public function addDataPartInfo(DataPartInfo $dataInfo): void * @psalm-param fileinfoconst $fileinfo_const */ public function getFileInfo(int $fileinfo_const = FILEINFO_NONE): string + { + $fileInfo = $this->detectFileInfo($fileinfo_const, $this->getContents()); + + if (false === $fileInfo) { + return ''; + } + + return $fileInfo; + } + + /** + * @return false|string + */ + protected function detectFileInfo(int $fileinfo_const, string $contents) { $finfo = new finfo($fileinfo_const); - return $finfo->buffer($this->getContents()); + return $finfo->buffer($contents); } /** diff --git a/tests/unit/IncomingMailAttachmentTest.php b/tests/unit/IncomingMailAttachmentTest.php new file mode 100644 index 00000000..620c8e4d --- /dev/null +++ b/tests/unit/IncomingMailAttachmentTest.php @@ -0,0 +1,46 @@ +assertSame('', $attachment->getFileInfo(FILEINFO_MIME_TYPE)); + } + + public function testGetFileInfoReturnsDetectedStringWhenFinfoBufferSucceeds(): void + { + $attachment = new class() extends IncomingMailAttachment { + public function getContents(): string + { + return 'png-contents'; + } + + protected function detectFileInfo(int $fileinfo_const, string $contents) + { + return 'image/png'; + } + }; + + $this->assertSame('image/png', $attachment->getFileInfo(FILEINFO_MIME_TYPE)); + } +}