Skip to content

Commit 8e9a005

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 8e9a005

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

phpstan.neon

+1-1
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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,7 @@ 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+
if (($newNode = $this->getSingleTagInsideElement($node, 'p')) !== null && $this->getLinkDensity($node) < 0.25) {
10401039
$node->parentNode->replaceChild($newNode, $node);
10411040
$nodesToScore[] = $newNode;
10421041
}
@@ -1538,10 +1537,10 @@ private function isPhrasingContent($node): bool
15381537

15391538
/**
15401539
* 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
1540+
* Returns the matched element, or `null` if `$node` contains non-empty text nodes
15421541
* or if it contains no element with given tag or more than 1 element.
15431542
*/
1544-
private function hasSingleTagInsideElement(JSLikeHTMLElement $node, string $tag): bool
1543+
private function getSingleTagInsideElement(JSLikeHTMLElement $node, string $tag): ?JSLikeHTMLElement
15451544
{
15461545
$childNodes = iterator_to_array($node->childNodes);
15471546
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
@@ -1554,7 +1553,7 @@ private function hasSingleTagInsideElement(JSLikeHTMLElement $node, string $tag)
15541553
// And there should be no text nodes with real content
15551554
$a = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMText && preg_match($this->regexps['hasContent'], $this->getInnerText($childNode)));
15561555

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

15601559
/**

0 commit comments

Comments
 (0)