From 352fcccdcb46030d648f82ac587d90740c608b42 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:53:04 +0100 Subject: [PATCH] Fixed some small type inspections --- .gitignore | 3 +- README.md | 1 - src/FpdfTplTrait.php | 2 +- src/FpdiTrait.php | 1 + src/PdfParser/PdfParser.php | 53 ++++++++++--------- src/PdfParser/StreamReader.php | 10 ++-- src/PdfParser/Tokenizer.php | 9 ++-- src/PdfReader/PdfReader.php | 3 +- .../PdfParser/Type/PdfArrayTest.php | 8 +++ 9 files changed, 51 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 91f5a8a..0f483d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -vendor +/.idea +/**/vendor /phpstan.phar /composer.phar /tests/visual/**/compare/ diff --git a/README.md b/README.md index e27d205..18998fb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ FPDI - Free PDF Document Importer [![Latest Stable Version](https://poser.pugx.org/setasign/fpdi/v/stable.svg)](https://packagist.org/packages/setasign/fpdi) [![Total Downloads](https://poser.pugx.org/setasign/fpdi/downloads.svg)](https://packagist.org/packages/setasign/fpdi) -[![Latest Unstable Version](https://poser.pugx.org/setasign/fpdi/v/unstable.svg)](https://packagist.org/packages/setasign/fpdi) [![License](https://poser.pugx.org/setasign/fpdi/license.svg)](https://packagist.org/packages/setasign/fpdi) :heavy_exclamation_mark: This document refers to FPDI 2. Version 1 is deprecated and development is discontinued. :heavy_exclamation_mark: diff --git a/src/FpdfTplTrait.php b/src/FpdfTplTrait.php index f1c457f..aae2571 100644 --- a/src/FpdfTplTrait.php +++ b/src/FpdfTplTrait.php @@ -109,7 +109,7 @@ public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, unset($x['tpl']); \extract($x, EXTR_IF_EXISTS); /** @noinspection NotOptimalIfConditionsInspection */ - /** @noinspection PhpConditionAlreadyCheckedInspection */ + /** @phpstan-ignore function.alreadyNarrowedType */ if (\is_array($x)) { $x = 0; } diff --git a/src/FpdiTrait.php b/src/FpdiTrait.php index 458796b..a15cafe 100644 --- a/src/FpdiTrait.php +++ b/src/FpdiTrait.php @@ -444,6 +444,7 @@ public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height unset($x['pageId']); \extract($x, EXTR_IF_EXISTS); /** @noinspection NotOptimalIfConditionsInspection */ + /** @phpstan-ignore function.alreadyNarrowedType */ if (\is_array($x)) { $x = 0; } diff --git a/src/PdfParser/PdfParser.php b/src/PdfParser/PdfParser.php index 918cf49..02af9ca 100644 --- a/src/PdfParser/PdfParser.php +++ b/src/PdfParser/PdfParser.php @@ -281,33 +281,34 @@ public function readValue($token = null, $expectedType = null) default: if (\is_numeric($token)) { - if (($token2 = $this->tokenizer->getNextToken()) !== false) { - if (\is_numeric($token2) && ($token3 = $this->tokenizer->getNextToken()) !== false) { - switch ($token3) { - case 'obj': - if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) { - throw new Type\PdfTypeException( - 'Got unexpected token type.', - Type\PdfTypeException::INVALID_DATA_TYPE - ); - } - - return $this->parsePdfIndirectObject((int)$token, (int)$token2); - case 'R': - if ( - $expectedType !== null && - $expectedType !== PdfIndirectObjectReference::class - ) { - throw new Type\PdfTypeException( - 'Got unexpected token type.', - Type\PdfTypeException::INVALID_DATA_TYPE - ); - } - - return PdfIndirectObjectReference::create((int)$token, (int)$token2); + $token2 = $this->tokenizer->getNextToken(); + if ($token2 !== false) { + if (\is_numeric($token2)) { + $token3 = $this->tokenizer->getNextToken(); + if ($token3 === 'obj') { + if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) { + throw new Type\PdfTypeException( + 'Got unexpected token type.', + Type\PdfTypeException::INVALID_DATA_TYPE + ); + } + + return $this->parsePdfIndirectObject((int) $token, (int) $token2); + } elseif ($token3 === 'R') { + if ( + $expectedType !== null && + $expectedType !== PdfIndirectObjectReference::class + ) { + throw new Type\PdfTypeException( + 'Got unexpected token type.', + Type\PdfTypeException::INVALID_DATA_TYPE + ); + } + + return PdfIndirectObjectReference::create((int) $token, (int) $token2); + } elseif ($token3 !== false) { + $this->tokenizer->pushStack($token3); } - - $this->tokenizer->pushStack($token3); } $this->tokenizer->pushStack($token2); diff --git a/src/PdfParser/StreamReader.php b/src/PdfParser/StreamReader.php index 16c6af9..3018418 100644 --- a/src/PdfParser/StreamReader.php +++ b/src/PdfParser/StreamReader.php @@ -418,16 +418,20 @@ public function reset($pos = 0, $length = 200) \fseek($this->stream, $pos); $this->position = $pos; - $this->buffer = $length > 0 ? \fread($this->stream, $length) : ''; - $this->bufferLength = \strlen($this->buffer); $this->offset = 0; + if ($length > 0) { + $this->buffer = (string) \fread($this->stream, $length); + } else { + $this->buffer = ''; + } + $this->bufferLength = \strlen($this->buffer); // If a stream wrapper is in use it is possible that // length values > 8096 will be ignored, so use the // increaseLength()-method to correct that behavior if ($this->bufferLength < $length && $this->increaseLength($length - $this->bufferLength)) { // increaseLength parameter is $minLength, so cut to have only the required bytes in the buffer - $this->buffer = \substr($this->buffer, 0, $length); + $this->buffer = (string) \substr($this->buffer, 0, $length); $this->bufferLength = \strlen($this->buffer); } } diff --git a/src/PdfParser/Tokenizer.php b/src/PdfParser/Tokenizer.php index 90f2aa9..889d036 100644 --- a/src/PdfParser/Tokenizer.php +++ b/src/PdfParser/Tokenizer.php @@ -68,7 +68,7 @@ public function pushStack($token) /** * Get next token. * - * @return bool|string + * @return false|string */ public function getNextToken() { @@ -117,11 +117,8 @@ public function getNextToken() } while ( // Break the loop if a delimiter or white space char is matched // in the current buffer or increase the buffers length - $lastBuffer !== false && - ( - $bufferOffset + $pos === \strlen($lastBuffer) && - $this->streamReader->increaseLength() - ) + $bufferOffset + $pos === \strlen($lastBuffer) + && $this->streamReader->increaseLength() ); $result = \substr($lastBuffer, $bufferOffset - 1, $pos + 1); diff --git a/src/PdfReader/PdfReader.php b/src/PdfReader/PdfReader.php index 9e022cb..95c4b7e 100644 --- a/src/PdfReader/PdfReader.php +++ b/src/PdfReader/PdfReader.php @@ -109,7 +109,7 @@ public function getPageCount() /** * Get a page instance. * - * @param int $pageNumber + * @param int|numeric-string $pageNumber * @return Page * @throws PdfTypeException * @throws CrossReferenceException @@ -118,6 +118,7 @@ public function getPageCount() */ public function getPage($pageNumber) { + /** @phpstan-ignore function.alreadyNarrowedType */ if (!\is_numeric($pageNumber)) { throw new \InvalidArgumentException( 'Page number needs to be a number.' diff --git a/tests/functional/PdfParser/Type/PdfArrayTest.php b/tests/functional/PdfParser/Type/PdfArrayTest.php index 1740d53..8e3690a 100644 --- a/tests/functional/PdfParser/Type/PdfArrayTest.php +++ b/tests/functional/PdfParser/Type/PdfArrayTest.php @@ -67,6 +67,14 @@ public function parseProvider() PdfNumeric::create(123), PdfNumeric::create(456) ] + ], + [ + "123 456 789]", + [ + PdfNumeric::create(123), + PdfNumeric::create(456), + PdfNumeric::create(789) + ] ] ];