From f8504aac7aa088b7c4ed6f284bfbd8f7b36f080c Mon Sep 17 00:00:00 2001
From: JakeQZ
Date: Fri, 20 Sep 2024 18:24:12 +0100
Subject: [PATCH] [CLEANUP] Use `Preg::matchAll` to handle unexpected
`preg_match_all` errors (#1331)
---
phpstan-baseline.neon | 5 -----
src/HtmlProcessor/HtmlPruner.php | 4 +++-
tests/Support/Constraint/StringContainsCssCount.php | 4 +++-
tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php | 6 +++---
tests/Unit/HtmlProcessor/HtmlPrunerTest.php | 4 ++--
5 files changed, 11 insertions(+), 12 deletions(-)
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 27391a7d..7a98e082 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -84,8 +84,3 @@ parameters:
message: "#^Parameter \\#2 \\$actual of static method Pelago\\\\Emogrifier\\\\Tests\\\\Unit\\\\HtmlProcessor\\\\AbstractHtmlProcessorTest\\:\\:assertEqualsHtml\\(\\) expects string, string\\|false given\\.$#"
count: 1
path: tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
-
- -
- message: "#^Variable \\$classAttributeMatches in PHPDoc tag @var does not match any variable in the foreach loop\\: \\$classAttributeValue$#"
- count: 1
- path: tests/Unit/HtmlProcessor/HtmlPrunerTest.php
diff --git a/src/HtmlProcessor/HtmlPruner.php b/src/HtmlProcessor/HtmlPruner.php
index c5a64d82..2b00afc4 100644
--- a/src/HtmlProcessor/HtmlPruner.php
+++ b/src/HtmlProcessor/HtmlPruner.php
@@ -126,9 +126,11 @@ private function removeClassAttributeFromElements(\DOMNodeList $elements): void
*/
public function removeRedundantClassesAfterCssInlined(CssInliner $cssInliner): self
{
+ $preg = new Preg();
+
$classesToKeepAsKeys = [];
foreach ($cssInliner->getMatchingUninlinableSelectors() as $selector) {
- \preg_match_all('/\\.(-?+[_a-zA-Z][\\w\\-]*+)/', $selector, $matches);
+ $preg->matchAll('/\\.(-?+[_a-zA-Z][\\w\\-]*+)/', $selector, $matches);
$classesToKeepAsKeys += \array_fill_keys($matches[1], true);
}
diff --git a/tests/Support/Constraint/StringContainsCssCount.php b/tests/Support/Constraint/StringContainsCssCount.php
index d7b2bbe1..6d0f6ea2 100644
--- a/tests/Support/Constraint/StringContainsCssCount.php
+++ b/tests/Support/Constraint/StringContainsCssCount.php
@@ -4,6 +4,8 @@
namespace Pelago\Emogrifier\Tests\Support\Constraint;
+use Pelago\Emogrifier\Utilities\Preg;
+
/**
* This constraint asserts that the string it is evaluated for contains some specific CSS a specific number of times,
* allowing for cosmetic whitespace differences.
@@ -61,6 +63,6 @@ protected function matches($other): bool
return false;
}
- return \preg_match_all($this->cssPattern, $other) === $this->count;
+ return (new Preg())->matchAll($this->cssPattern, $other) === $this->count;
}
}
diff --git a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
index cfe7ff4e..1c891ad5 100644
--- a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
+++ b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
@@ -487,7 +487,7 @@ public function notAddsSecondHtmlTag(string $html): void
$result = $subject->render();
- $htmlTagCount = \preg_match_all('%]%', $result);
+ $htmlTagCount = (new Preg())->matchAll('%
]%', $result);
self::assertSame(1, $htmlTagCount);
}
@@ -556,7 +556,7 @@ public function addsMissingHeadTagExactlyOnce(string $html): void
$result = $subject->render();
- $headTagCount = \preg_match_all('%]%', $result);
+ $headTagCount = (new Preg())->matchAll('%
]%', $result);
self::assertSame(1, $headTagCount);
}
@@ -604,7 +604,7 @@ public function notAddsSecondHeadTag(string $html): void
$result = $subject->render();
- $headTagCount = \preg_match_all('%
]%', $result);
+ $headTagCount = (new Preg())->matchAll('%
]%', $result);
self::assertSame(1, $headTagCount);
}
diff --git a/tests/Unit/HtmlProcessor/HtmlPrunerTest.php b/tests/Unit/HtmlProcessor/HtmlPrunerTest.php
index 4bc648f5..79d9f8ec 100644
--- a/tests/Unit/HtmlProcessor/HtmlPrunerTest.php
+++ b/tests/Unit/HtmlProcessor/HtmlPrunerTest.php
@@ -7,6 +7,7 @@
use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\HtmlProcessor\AbstractHtmlProcessor;
use Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
+use Pelago\Emogrifier\Utilities\Preg;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
@@ -393,8 +394,7 @@ public function removeRedundantClassesMinifiesClassAttributes(string $html, arra
$subject->removeRedundantClasses($classesToKeep);
- \preg_match_all('/class="([^"]*+)"/', $subject->renderBodyContent(), $classAttributeMatches);
- /** @var array> $classAttributeMatches */
+ (new Preg())->matchAll('/class="([^"]*+)"/', $subject->renderBodyContent(), $classAttributeMatches);
foreach ($classAttributeMatches[1] as $classAttributeValue) {
self::assertMinified($classAttributeValue);
}