Skip to content

Commit 46bd7a6

Browse files
committed
Raise PHPStan version to 5
Converting `hasSingleTagInsideElement` into a type-safe getter will allow PHPStan to know the type of `newNode` is `DOMElement`.
1 parent 95a0978 commit 46bd7a6

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 4
2+
level: 5
33
paths:
44
- src
55
- tests

src/Readability.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,8 @@ protected function grabArticle(?JSLikeHTMLElement $page = null): ?JSLikeHTMLElem
10351035
}
10361036
}
10371037

1038-
if ($this->hasSingleTagInsideElement($node, 'p') && $this->getLinkDensity($node) < 0.25) {
1039-
$newNode = $node->childNodes->item(0);
1038+
$newNode = $this->getSingleTagInsideElement($node, 'p');
1039+
if (null !== $newNode && $this->getLinkDensity($node) < 0.25) {
10401040
$node->parentNode->replaceChild($newNode, $node);
10411041
$nodesToScore[] = $newNode;
10421042
}
@@ -1538,10 +1538,10 @@ private function isPhrasingContent($node): bool
15381538

15391539
/**
15401540
* Checks if `$node` has only whitespace and a single element with `$tag` for the tag name.
1541-
* Returns false if `$node` contains non-empty text nodes
1541+
* Returns the matched element, or `null` if `$node` contains non-empty text nodes
15421542
* or if it contains no element with given tag or more than 1 element.
15431543
*/
1544-
private function hasSingleTagInsideElement(JSLikeHTMLElement $node, string $tag): bool
1544+
private function getSingleTagInsideElement(JSLikeHTMLElement $node, string $tag): ?JSLikeHTMLElement
15451545
{
15461546
$childNodes = iterator_to_array($node->childNodes);
15471547
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
@@ -1554,7 +1554,7 @@ private function hasSingleTagInsideElement(JSLikeHTMLElement $node, string $tag)
15541554
// And there should be no text nodes with real content
15551555
$a = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMText && preg_match($this->regexps['hasContent'], $this->getInnerText($childNode)));
15561556

1557-
return 0 === \count($a);
1557+
return 0 === \count($a) ? $children[0] : null;
15581558
}
15591559

15601560
/**

0 commit comments

Comments
 (0)