From b6bf195ea8d18db87d67ce4892b6f219afd29980 Mon Sep 17 00:00:00 2001 From: prinsfrank Date: Fri, 8 Oct 2021 14:39:27 +0200 Subject: [PATCH 1/3] Add test for scenarios where either the page is null or there is no content, both failing on a return type for 'getDefaultFont' --- tests/Unit/PDFObjectTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/Unit/PDFObjectTest.php diff --git a/tests/Unit/PDFObjectTest.php b/tests/Unit/PDFObjectTest.php new file mode 100644 index 00000000..cd923a93 --- /dev/null +++ b/tests/Unit/PDFObjectTest.php @@ -0,0 +1,24 @@ +getText()); + } + + public function testGetTextOnPageWithoutContent(): void + { + $document = new Document(); + + static::assertSame(' ', (new PDFObject($document, null, null))->getText(new Page($document))); + } +} From 90d5eb8ded2debd7010df1bf7fe3607cbdbe1946 Mon Sep 17 00:00:00 2001 From: prinsfrank Date: Fri, 8 Oct 2021 14:39:56 +0200 Subject: [PATCH 2/3] Don't add the font to the set of available fonts when there is no font available --- src/Smalot/PdfParser/PDFObject.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index 7dd36d5a..d5f7cf6e 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -222,7 +222,10 @@ private function getDefaultFont(Page $page = null): Font $fonts = $page->getFonts(); } - $fonts[] = $this->document->getFirstFont(); + $firstFont = $this->document->getFirstFont(); + if ($firstFont !== null) { + $fonts[] = $firstFont; + } if (\count($fonts) > 0) { return reset($fonts); From b4ee2bc348c4a2f18a78cd5d2d82201f9b35c431 Mon Sep 17 00:00:00 2001 From: prinsfrank Date: Fri, 8 Oct 2021 14:43:02 +0200 Subject: [PATCH 3/3] Run csfixer --- src/Smalot/PdfParser/PDFObject.php | 2 +- tests/Unit/PDFObjectTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Smalot/PdfParser/PDFObject.php b/src/Smalot/PdfParser/PDFObject.php index d5f7cf6e..2bce9ce6 100644 --- a/src/Smalot/PdfParser/PDFObject.php +++ b/src/Smalot/PdfParser/PDFObject.php @@ -223,7 +223,7 @@ private function getDefaultFont(Page $page = null): Font } $firstFont = $this->document->getFirstFont(); - if ($firstFont !== null) { + if (null !== $firstFont) { $fonts[] = $firstFont; } diff --git a/tests/Unit/PDFObjectTest.php b/tests/Unit/PDFObjectTest.php index cd923a93..593f3ec9 100644 --- a/tests/Unit/PDFObjectTest.php +++ b/tests/Unit/PDFObjectTest.php @@ -1,4 +1,5 @@