From 468de469b4b0b3644305f57aa7f401a4da694e2d Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Tue, 27 Jun 2023 01:46:29 -0500 Subject: [PATCH 1/4] Fix html_entity_decode deprecation warning in PHP 8.1 Copied from https://github.com/mtibben/html2text/pull/113. Passing null as the second parameter is deprecated in PHP 8.1. When using the legacyConstruct function the htmlFuncFlags are never set and passing null into html_entity_decode. This move setting htmlFuncFlags above the legacy check to ensure they are always set to prevent passing null into html_entity_decode. --- src/Html2Text.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Html2Text.php b/src/Html2Text.php index 16a6572..c3e0109 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -236,6 +236,10 @@ private function legacyConstruct($html = '', $fromFile = false, array $options = */ public function __construct($html = '', $options = array()) { + $this->htmlFuncFlags = (PHP_VERSION_ID < 50400) + ? ENT_QUOTES + : ENT_QUOTES | ENT_HTML5; + // for backwards compatibility if (!is_array($options)) { return call_user_func_array(array($this, 'legacyConstruct'), func_get_args()); @@ -243,9 +247,6 @@ public function __construct($html = '', $options = array()) $this->html = $html; $this->options = array_merge($this->options, $options); - $this->htmlFuncFlags = (PHP_VERSION_ID < 50400) - ? ENT_QUOTES - : ENT_QUOTES | ENT_HTML5; } /** From 33f1bd3f716bffea23d7a227901822302dd32baa Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Tue, 27 Jun 2023 01:50:58 -0500 Subject: [PATCH 2/4] Ensure trim() always operates on a string, for PHP 8.2 compat Other instances of trim() operate on known strings, so they don't need to be null-coalesced --- src/Html2Text.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Html2Text.php b/src/Html2Text.php index c3e0109..d3b5462 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -352,7 +352,7 @@ protected function doConvert() { $this->linkList = array(); - $text = trim($this->html); + $text = trim($this->html ?? ''); $this->converter($text); @@ -390,7 +390,7 @@ protected function converter(&$text) $text = preg_replace("/[\n]{3,}/", "\n\n", $text); // remove leading empty lines (can be produced by eg. P tag on the beginning) - $text = ltrim($text, "\n"); + $text = ltrim($text ?? '', "\n"); if ($this->options['width'] > 0) { $text = wordwrap($text, $this->options['width']); From 134969577f7c710d0669a3143a4b66bdd0ac2675 Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Tue, 27 Jun 2023 01:53:59 -0500 Subject: [PATCH 3/4] Remove unnecessary dynamic property set to avoid deprecation warning in PHP 8.2 in test --- test/PrintTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/PrintTest.php b/test/PrintTest.php index 0f101df..9a9fd80 100644 --- a/test/PrintTest.php +++ b/test/PrintTest.php @@ -11,15 +11,15 @@ class PrintTest extends TestCase public function testP() { - $this->html = new Html2Text(self::TEST_HTML); - $this->html->p(); + $html = new Html2Text(self::TEST_HTML); + $html->p(); $this->expectOutputString(self::EXPECTED); } public function testPrint_text() { - $this->html = new Html2Text(self::TEST_HTML); - $this->html->print_text(); + $html = new Html2Text(self::TEST_HTML); + $html->print_text(); $this->expectOutputString(self::EXPECTED); } } From 030d1c644fbe39f2395922522d0d96f71ce1b7af Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Tue, 27 Jun 2023 01:54:21 -0500 Subject: [PATCH 4/4] Rename data provider to avoid attempting to run it as a test --- test/ImageTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ImageTest.php b/test/ImageTest.php index a981d03..afe439a 100644 --- a/test/ImageTest.php +++ b/test/ImageTest.php @@ -6,7 +6,7 @@ class ImageTest extends TestCase { - public function testImageDataProvider() { + public function imageDataProvider() { return array( 'Without alt tag' => array( 'html' => '', @@ -36,7 +36,7 @@ public function testImageDataProvider() { } /** - * @dataProvider testImageDataProvider + * @dataProvider imageDataProvider */ public function testImages($html, $expected) {