Skip to content

Commit

Permalink
[CLEANUP] Use Preg::matchAll to handle unexpected preg_match_all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeQZ committed Sep 20, 2024
1 parent 8ac1121 commit 6bdb616
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/HtmlProcessor/HtmlPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Support/Constraint/StringContainsCssCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function notAddsSecondHtmlTag(string $html): void

$result = $subject->render();

$htmlTagCount = \preg_match_all('%<html[\\s/>]%', $result);
$htmlTagCount = (new Preg())->matchAll('%<html[\\s/>]%', $result);
self::assertSame(1, $htmlTagCount);
}

Expand Down Expand Up @@ -556,7 +556,7 @@ public function addsMissingHeadTagExactlyOnce(string $html): void

$result = $subject->render();

$headTagCount = \preg_match_all('%<head[\\s/>]%', $result);
$headTagCount = (new Preg())->matchAll('%<head[\\s/>]%', $result);
self::assertSame(1, $headTagCount);
}

Expand Down Expand Up @@ -604,7 +604,7 @@ public function notAddsSecondHeadTag(string $html): void

$result = $subject->render();

$headTagCount = \preg_match_all('%<head[\\s/>]%', $result);
$headTagCount = (new Preg())->matchAll('%<head[\\s/>]%', $result);
self::assertSame(1, $headTagCount);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/HtmlProcessor/HtmlPrunerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -393,8 +394,7 @@ public function removeRedundantClassesMinifiesClassAttributes(string $html, arra

$subject->removeRedundantClasses($classesToKeep);

\preg_match_all('/class="([^"]*+)"/', $subject->renderBodyContent(), $classAttributeMatches);
/** @var array<int, array<int, string>> $classAttributeMatches */
(new Preg())->matchAll('/class="([^"]*+)"/', $subject->renderBodyContent(), $classAttributeMatches);
foreach ($classAttributeMatches[1] as $classAttributeValue) {
self::assertMinified($classAttributeValue);
}
Expand Down

0 comments on commit 6bdb616

Please sign in to comment.