From 6738ac998f0eb02ee5a3cbf2c97ef1eb1964bd64 Mon Sep 17 00:00:00 2001 From: Markus Bauer Date: Thu, 25 Jul 2024 17:41:49 +0200 Subject: [PATCH 1/5] Remove implicitly nullable parameter declarations --- src/GraphicsState.php | 2 +- src/PdfParser/Type/PdfDictionary.php | 2 +- src/PdfParser/Type/PdfStream.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GraphicsState.php b/src/GraphicsState.php index 27feafb..01cbe12 100644 --- a/src/GraphicsState.php +++ b/src/GraphicsState.php @@ -26,7 +26,7 @@ class GraphicsState /** * @param Matrix|null $ctm */ - public function __construct(Matrix $ctm = null) + public function __construct($ctm = null) { if ($ctm === null) { $ctm = new Matrix(); diff --git a/src/PdfParser/Type/PdfDictionary.php b/src/PdfParser/Type/PdfDictionary.php index 8991322..4f22153 100644 --- a/src/PdfParser/Type/PdfDictionary.php +++ b/src/PdfParser/Type/PdfDictionary.php @@ -107,7 +107,7 @@ public static function create(array $entries = []) * @return PdfNull|PdfType * @throws PdfTypeException */ - public static function get($dictionary, $key, PdfType $default = null) + public static function get($dictionary, $key, $default = null) { $dictionary = self::ensure($dictionary); diff --git a/src/PdfParser/Type/PdfStream.php b/src/PdfParser/Type/PdfStream.php index cfa2cdb..31681b4 100644 --- a/src/PdfParser/Type/PdfStream.php +++ b/src/PdfParser/Type/PdfStream.php @@ -31,11 +31,11 @@ class PdfStream extends PdfType * * @param PdfDictionary $dictionary * @param StreamReader $reader - * @param PdfParser $parser Optional to keep backwards compatibility + * @param PdfParser|null $parser Optional to keep backwards compatibility * @return self * @throws PdfTypeException */ - public static function parse(PdfDictionary $dictionary, StreamReader $reader, PdfParser $parser = null) + public static function parse(PdfDictionary $dictionary, StreamReader $reader, $parser = null) { $v = new self(); $v->value = $dictionary; From d0a413abd2bbadf3cd5eeda45a73d1b730614c3a Mon Sep 17 00:00:00 2001 From: Markus Bauer Date: Mon, 5 Aug 2024 11:42:24 +0200 Subject: [PATCH 2/5] Add explicit type checks where old PHP's type system cannot do it --- src/GraphicsState.php | 4 +++- src/PdfParser/Type/PdfDictionary.php | 3 +++ src/PdfParser/Type/PdfStream.php | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/GraphicsState.php b/src/GraphicsState.php index 01cbe12..6d253a6 100644 --- a/src/GraphicsState.php +++ b/src/GraphicsState.php @@ -30,7 +30,9 @@ public function __construct($ctm = null) { if ($ctm === null) { $ctm = new Matrix(); - } + } elseif (!($ctm instanceof Matrix)) { + throw new \InvalidArgumentException('$ctm must be an instance of Fpdi\\Matrix or null'); + } $this->ctm = $ctm; } diff --git a/src/PdfParser/Type/PdfDictionary.php b/src/PdfParser/Type/PdfDictionary.php index 4f22153..fcf4c7f 100644 --- a/src/PdfParser/Type/PdfDictionary.php +++ b/src/PdfParser/Type/PdfDictionary.php @@ -109,6 +109,9 @@ public static function create(array $entries = []) */ public static function get($dictionary, $key, $default = null) { + if ($default !== null && !($default instanceof PdfType)) { + throw new \InvalidArgumentException('Default value must be an instance of PdfType or null'); + } $dictionary = self::ensure($dictionary); if (isset($dictionary->value[$key])) { diff --git a/src/PdfParser/Type/PdfStream.php b/src/PdfParser/Type/PdfStream.php index 31681b4..b37c272 100644 --- a/src/PdfParser/Type/PdfStream.php +++ b/src/PdfParser/Type/PdfStream.php @@ -37,6 +37,9 @@ class PdfStream extends PdfType */ public static function parse(PdfDictionary $dictionary, StreamReader $reader, $parser = null) { + if ($parser !== null && !($parser instanceof PdfParser)) { + throw new \InvalidArgumentException('$parser must be an instance of PdfParser or null'); + } $v = new self(); $v->value = $dictionary; $v->reader = $reader; From 9ec6de6900cf1e1a2f4cfc18c2bc612a8bdca8a7 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:52:13 +0200 Subject: [PATCH 3/5] Changed phpstan.neon to treatPhpDocTypesAsCertain: false --- phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/phpstan.neon b/phpstan.neon index 48e664e..e602b7c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,6 @@ parameters: level: 5 + treatPhpDocTypesAsCertain: false paths: - src #- tests From 14860813394c978881c54bfec84d9b418ed8de70 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:55:43 +0200 Subject: [PATCH 4/5] Fixed code styling --- src/GraphicsState.php | 4 ++-- src/PdfParser/Type/PdfDictionary.php | 6 +++--- src/PdfParser/Type/PdfStream.php | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/GraphicsState.php b/src/GraphicsState.php index 6d253a6..f8da2b1 100644 --- a/src/GraphicsState.php +++ b/src/GraphicsState.php @@ -31,8 +31,8 @@ public function __construct($ctm = null) if ($ctm === null) { $ctm = new Matrix(); } elseif (!($ctm instanceof Matrix)) { - throw new \InvalidArgumentException('$ctm must be an instance of Fpdi\\Matrix or null'); - } + throw new \InvalidArgumentException('$ctm must be an instance of Fpdi\\Matrix or null'); + } $this->ctm = $ctm; } diff --git a/src/PdfParser/Type/PdfDictionary.php b/src/PdfParser/Type/PdfDictionary.php index fcf4c7f..9859afc 100644 --- a/src/PdfParser/Type/PdfDictionary.php +++ b/src/PdfParser/Type/PdfDictionary.php @@ -109,9 +109,9 @@ public static function create(array $entries = []) */ public static function get($dictionary, $key, $default = null) { - if ($default !== null && !($default instanceof PdfType)) { - throw new \InvalidArgumentException('Default value must be an instance of PdfType or null'); - } + if ($default !== null && !($default instanceof PdfType)) { + throw new \InvalidArgumentException('Default value must be an instance of PdfType or null'); + } $dictionary = self::ensure($dictionary); if (isset($dictionary->value[$key])) { diff --git a/src/PdfParser/Type/PdfStream.php b/src/PdfParser/Type/PdfStream.php index b37c272..a23fd0d 100644 --- a/src/PdfParser/Type/PdfStream.php +++ b/src/PdfParser/Type/PdfStream.php @@ -37,9 +37,9 @@ class PdfStream extends PdfType */ public static function parse(PdfDictionary $dictionary, StreamReader $reader, $parser = null) { - if ($parser !== null && !($parser instanceof PdfParser)) { - throw new \InvalidArgumentException('$parser must be an instance of PdfParser or null'); - } + if ($parser !== null && !($parser instanceof PdfParser)) { + throw new \InvalidArgumentException('$parser must be an instance of PdfParser or null'); + } $v = new self(); $v->value = $dictionary; $v->reader = $reader; From 79ba2a2dbf8e3bc330e3b2d5c30d153654f07c35 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:55:55 +0200 Subject: [PATCH 5/5] Added php8.4 to tests --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b7333e2..96b6c71 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -31,7 +31,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - php: ['7.4', '8.0', '8.1', '8.2', '8.3'] + php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] steps: - name: Checkout uses: actions/checkout@v2