From 405f09f2f529f78f030f56f188ec5b22ac9f9005 Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Mon, 7 Sep 2026 12:35:07 +1000 Subject: [PATCH] Default $extgstates to an array so the writers can count it The property was declared without a default, so it is null on any Mpdf whose constructor has not run. ResourceWriter and BackgroundWriter both reach for it with count(), which is a TypeError on PHP 8. Mirrors mpdf/mpdf#2135, less its SvgTest rewrite: the deprecation that PR is named after comes from a null $gs subscript, not from $extgstates, and upstream has since fixed that cause in 2dc0381, which gravitypdf already carries. Co-Authored-By: Claude Opus 5 --- src/Mpdf.php | 2 +- tests/Mpdf/ConfigurationTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Mpdf.php b/src/Mpdf.php index e17d43005..ff56d450e 100644 --- a/src/Mpdf.php +++ b/src/Mpdf.php @@ -610,7 +610,7 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface var $directionality; - var $extgstates; // Used for alpha channel - Transparency (Watermark) + var $extgstates = []; // Used for alpha channel - Transparency (Watermark) var $mgl; var $mgt; var $mgr; diff --git a/tests/Mpdf/ConfigurationTest.php b/tests/Mpdf/ConfigurationTest.php index 78fb79d6e..fed02dd66 100644 --- a/tests/Mpdf/ConfigurationTest.php +++ b/tests/Mpdf/ConfigurationTest.php @@ -14,6 +14,17 @@ public function testDefaultSettings() $this->assertFalse($mpdf->autoPadding); } + /** + * The writers count() this before anything has drawn, so an instance that never ran the + * constructor - a partial mock, say - must still find an array here + */ + public function testExtgstatesDefaultsToAnArray() + { + $mpdf = (new \ReflectionClass(Mpdf::class))->newInstanceWithoutConstructor(); + + $this->assertSame([], $mpdf->extgstates); + } + public function testOverwrittenSettings() { $mpdf = new Mpdf([