Skip to content

Commit 2065c44

Browse files
Merge pull request #225 from Setasign/development
Development
2 parents 94c45fb + 866a255 commit 2065c44

File tree

12 files changed

+103
-71
lines changed

12 files changed

+103
-71
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
vendor
1+
/.idea
2+
/**/vendor
23
/phpstan.phar
34
/composer.phar
45
/tests/visual/**/compare/

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FPDI - Free PDF Document Importer
33

44
[![Latest Stable Version](https://poser.pugx.org/setasign/fpdi/v/stable.svg)](https://packagist.org/packages/setasign/fpdi)
55
[![Total Downloads](https://poser.pugx.org/setasign/fpdi/downloads.svg)](https://packagist.org/packages/setasign/fpdi)
6-
[![Latest Unstable Version](https://poser.pugx.org/setasign/fpdi/v/unstable.svg)](https://packagist.org/packages/setasign/fpdi)
76
[![License](https://poser.pugx.org/setasign/fpdi/license.svg)](https://packagist.org/packages/setasign/fpdi)
87

98
:heavy_exclamation_mark: This document refers to FPDI 2. Version 1 is deprecated and development is discontinued. :heavy_exclamation_mark:

composer.lock

Lines changed: 35 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FpdfTplTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null,
109109
unset($x['tpl']);
110110
\extract($x, EXTR_IF_EXISTS);
111111
/** @noinspection NotOptimalIfConditionsInspection */
112-
/** @noinspection PhpConditionAlreadyCheckedInspection */
112+
/** @phpstan-ignore function.alreadyNarrowedType */
113113
if (\is_array($x)) {
114114
$x = 0;
115115
}

src/FpdiTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height
444444
unset($x['pageId']);
445445
\extract($x, EXTR_IF_EXISTS);
446446
/** @noinspection NotOptimalIfConditionsInspection */
447+
/** @phpstan-ignore function.alreadyNarrowedType */
447448
if (\is_array($x)) {
448449
$x = 0;
449450
}

src/PdfParser/PdfParser.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -281,33 +281,34 @@ public function readValue($token = null, $expectedType = null)
281281

282282
default:
283283
if (\is_numeric($token)) {
284-
if (($token2 = $this->tokenizer->getNextToken()) !== false) {
285-
if (\is_numeric($token2) && ($token3 = $this->tokenizer->getNextToken()) !== false) {
286-
switch ($token3) {
287-
case 'obj':
288-
if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) {
289-
throw new Type\PdfTypeException(
290-
'Got unexpected token type.',
291-
Type\PdfTypeException::INVALID_DATA_TYPE
292-
);
293-
}
294-
295-
return $this->parsePdfIndirectObject((int)$token, (int)$token2);
296-
case 'R':
297-
if (
298-
$expectedType !== null &&
299-
$expectedType !== PdfIndirectObjectReference::class
300-
) {
301-
throw new Type\PdfTypeException(
302-
'Got unexpected token type.',
303-
Type\PdfTypeException::INVALID_DATA_TYPE
304-
);
305-
}
306-
307-
return PdfIndirectObjectReference::create((int)$token, (int)$token2);
284+
$token2 = $this->tokenizer->getNextToken();
285+
if ($token2 !== false) {
286+
if (\is_numeric($token2)) {
287+
$token3 = $this->tokenizer->getNextToken();
288+
if ($token3 === 'obj') {
289+
if ($expectedType !== null && $expectedType !== PdfIndirectObject::class) {
290+
throw new Type\PdfTypeException(
291+
'Got unexpected token type.',
292+
Type\PdfTypeException::INVALID_DATA_TYPE
293+
);
294+
}
295+
296+
return $this->parsePdfIndirectObject((int) $token, (int) $token2);
297+
} elseif ($token3 === 'R') {
298+
if (
299+
$expectedType !== null &&
300+
$expectedType !== PdfIndirectObjectReference::class
301+
) {
302+
throw new Type\PdfTypeException(
303+
'Got unexpected token type.',
304+
Type\PdfTypeException::INVALID_DATA_TYPE
305+
);
306+
}
307+
308+
return PdfIndirectObjectReference::create((int) $token, (int) $token2);
309+
} elseif ($token3 !== false) {
310+
$this->tokenizer->pushStack($token3);
308311
}
309-
310-
$this->tokenizer->pushStack($token3);
311312
}
312313

313314
$this->tokenizer->pushStack($token2);

src/PdfParser/StreamReader.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,20 @@ public function reset($pos = 0, $length = 200)
418418
\fseek($this->stream, $pos);
419419

420420
$this->position = $pos;
421-
$this->buffer = $length > 0 ? \fread($this->stream, $length) : '';
422-
$this->bufferLength = \strlen($this->buffer);
423421
$this->offset = 0;
422+
if ($length > 0) {
423+
$this->buffer = (string) \fread($this->stream, $length);
424+
} else {
425+
$this->buffer = '';
426+
}
427+
$this->bufferLength = \strlen($this->buffer);
424428

425429
// If a stream wrapper is in use it is possible that
426430
// length values > 8096 will be ignored, so use the
427431
// increaseLength()-method to correct that behavior
428432
if ($this->bufferLength < $length && $this->increaseLength($length - $this->bufferLength)) {
429433
// increaseLength parameter is $minLength, so cut to have only the required bytes in the buffer
430-
$this->buffer = \substr($this->buffer, 0, $length);
434+
$this->buffer = (string) \substr($this->buffer, 0, $length);
431435
$this->bufferLength = \strlen($this->buffer);
432436
}
433437
}

src/PdfParser/Tokenizer.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function pushStack($token)
6868
/**
6969
* Get next token.
7070
*
71-
* @return bool|string
71+
* @return false|string
7272
*/
7373
public function getNextToken()
7474
{
@@ -117,11 +117,8 @@ public function getNextToken()
117117
} while (
118118
// Break the loop if a delimiter or white space char is matched
119119
// in the current buffer or increase the buffers length
120-
$lastBuffer !== false &&
121-
(
122-
$bufferOffset + $pos === \strlen($lastBuffer) &&
123-
$this->streamReader->increaseLength()
124-
)
120+
$bufferOffset + $pos === \strlen($lastBuffer)
121+
&& $this->streamReader->increaseLength()
125122
);
126123

127124
$result = \substr($lastBuffer, $bufferOffset - 1, $pos + 1);

src/PdfParser/Type/PdfDictionary.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ public static function parse(Tokenizer $tokenizer, StreamReader $streamReader, P
4848
if (!($key instanceof PdfName)) {
4949
$lastToken = null;
5050
// ignore all other entries and search for the closing brackets
51-
while (($token = $tokenizer->getNextToken()) !== '>' && $token !== false && $lastToken !== '>') {
51+
while (($token = $tokenizer->getNextToken()) !== '>' || $lastToken !== '>') {
52+
if ($token === false) {
53+
return false;
54+
}
5255
$lastToken = $token;
5356
}
5457

55-
if ($token === false) {
56-
return false;
57-
}
58-
5958
break;
6059
}
6160

src/PdfReader/PdfReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function getPageCount()
109109
/**
110110
* Get a page instance.
111111
*
112-
* @param int $pageNumber
112+
* @param int|numeric-string $pageNumber
113113
* @return Page
114114
* @throws PdfTypeException
115115
* @throws CrossReferenceException

0 commit comments

Comments
 (0)