Skip to content

Commit

Permalink
[CLEANUP] Use Preg::split to handle unexpected preg_split errors (#1314)
Browse files Browse the repository at this point in the history
This resolves three PHPStan errors.

Instances of calls to `preg_split` in the two (identical)
`parseCssDeclarationsBlock` methods are not updated - those are covered
separately by #1311.
  • Loading branch information
JakeQZ committed Sep 17, 2024
1 parent fe0bb4d commit 178d3ef
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 16 deletions.
12 changes: 1 addition & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:

-
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
count: 2
count: 1
path: src/CssInliner.php

-
Expand All @@ -25,11 +25,6 @@ parameters:
count: 2
path: src/CssInliner.php

-
message: "#^Parameter \\#2 \\$array of function array_map expects array, array\\<int, string\\>\\|false given\\.$#"
count: 1
path: src/CssInliner.php

-
message: "#^Only booleans are allowed in an if condition, int\\|false given\\.$#"
count: 1
Expand Down Expand Up @@ -85,11 +80,6 @@ parameters:
count: 1
path: src/HtmlProcessor/HtmlPruner.php

-
message: "#^Parameter \\#1 \\$array of method Pelago\\\\Emogrifier\\\\Utilities\\\\ArrayIntersector\\:\\:intersectWith\\(\\) expects array\\<\\(int\\|string\\)\\>, array\\<int, string\\>\\|false given\\.$#"
count: 1
path: src/HtmlProcessor/HtmlPruner.php

-
message: "#^Parameter \\#1 \\$elements of method Pelago\\\\Emogrifier\\\\HtmlProcessor\\\\HtmlPruner\\:\\:removeClassAttributeFromElements\\(\\) expects DOMNodeList, DOMNodeList\\<DOMNode\\>\\|false given\\.$#"
count: 1
Expand Down
5 changes: 3 additions & 2 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ private function hasUnsupportedPseudoClass(string $selector): bool
return false;
}

foreach (\preg_split('/' . self::COMBINATOR_MATCHER . '/', $selector) as $selectorPart) {
$preg = (new Preg())->throwExceptions($this->debug);
foreach ($preg->split('/' . self::COMBINATOR_MATCHER . '/', $selector) as $selectorPart) {
if ($this->selectorPartHasUnsupportedOfTypePseudoClass($selectorPart)) {
return true;
}
Expand Down Expand Up @@ -1053,7 +1054,7 @@ function (array $matches): string {
function (string $selectorPart): string {
return $this->removeUnsupportedOfTypePseudoClasses($selectorPart);
},
\preg_split(
(new Preg())->throwExceptions($this->debug)->split(
'/(' . self::COMBINATOR_MATCHER . ')/',
$selectorWithoutUnmatchablePseudoComponents,
-1,
Expand Down
3 changes: 1 addition & 2 deletions src/HtmlProcessor/CssToAttributeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ private function isTableOrImageNode(\DOMElement $node): bool
*/
private function parseCssShorthandValue(string $value): array
{
/** @var array<int, string> $values */
$values = \preg_split('/\\s+/', $value);
$values = (new Preg())->split('/\\s+/', $value);

$css = [];
$css['top'] = $values[0];
Expand Down
4 changes: 3 additions & 1 deletion src/HtmlProcessor/HtmlPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Pelago\Emogrifier\CssInliner;
use Pelago\Emogrifier\Utilities\ArrayIntersector;
use Pelago\Emogrifier\Utilities\Preg;

/**
* This class can remove things from HTML.
Expand Down Expand Up @@ -84,9 +85,10 @@ private function removeClassesFromElements(\DOMNodeList $elements, array $classe
{
$classesToKeepIntersector = new ArrayIntersector($classesToKeep);

$preg = new Preg();
/** @var \DOMElement $element */
foreach ($elements as $element) {
$elementClasses = \preg_split('/\\s++/', \trim($element->getAttribute('class')));
$elementClasses = $preg->split('/\\s++/', \trim($element->getAttribute('class')));
$elementClassesToKeep = $classesToKeepIntersector->intersectWith($elementClasses);
if ($elementClassesToKeep !== []) {
$element->setAttribute('class', \implode(' ', $elementClassesToKeep));
Expand Down

0 comments on commit 178d3ef

Please sign in to comment.