Skip to content

Commit

Permalink
Fixed some small type inspections
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianKresse committed Nov 27, 2024
1 parent aa57315 commit 352fccc
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
/.idea
/**/vendor
/phpstan.phar
/composer.phar
/tests/visual/**/compare/
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/FpdfTplTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/FpdiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
53 changes: 27 additions & 26 deletions src/PdfParser/PdfParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/PdfParser/StreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/PdfParser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function pushStack($token)
/**
* Get next token.
*
* @return bool|string
* @return false|string
*/
public function getNextToken()
{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/PdfReader/PdfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.'
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/PdfParser/Type/PdfArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
]
];

Expand Down

0 comments on commit 352fccc

Please sign in to comment.