Skip to content

Commit

Permalink
Raise PHPStan version to 5
Browse files Browse the repository at this point in the history
Converting `hasSingleTagInsideElement` into a type-safe getter
will allow PHPStan to know the type of `newNode` is `DOMElement`.
  • Loading branch information
jtojnar committed Mar 16, 2024
1 parent 95a0978 commit 8e9a005
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 4
level: 5
paths:
- src
- tests
Expand Down
9 changes: 4 additions & 5 deletions src/Readability.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,8 +1035,7 @@ protected function grabArticle(?JSLikeHTMLElement $page = null): ?JSLikeHTMLElem
}
}

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

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

return 0 === \count($a);
return 0 === \count($a) ? $children[0] : null;
}

/**
Expand Down

0 comments on commit 8e9a005

Please sign in to comment.