Skip to content

Commit

Permalink
Raise PHPStan to level 4
Browse files Browse the repository at this point in the history
Nothing affecting correctness, just stuff making it easier for PHPStan to reason about the code.

Remove `$errcontext` argument in `set_error_handler` since it is removed in PHP 8.
  • Loading branch information
jtojnar committed Oct 11, 2024
1 parent d6cd134 commit 55c9999
Show file tree
Hide file tree
Showing 2 changed files with 10 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: 1
level: 4
paths:
- src
- tests
Expand Down
14 changes: 9 additions & 5 deletions src/Readability.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public function prepArticle(\DOMNode $articleContent): void
}

// Remove service data-candidate attribute.
/** @var \DOMNodeList<\DOMElement> */
$elems = $xpath->query('.//*[@data-candidate]', $articleContent);
foreach ($elems as $elem) {
$elem->removeAttribute('data-candidate');
Expand Down Expand Up @@ -1159,12 +1160,13 @@ protected function grabArticle(?\DOMElement $page = null)
* This is faster to do before scoring but safer after.
*/
if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS) && $xpath) {
/** @var \DOMNodeList<\DOMElement> */
$candidates = $xpath->query('.//*[(self::footer and count(//footer)<2) or (self::aside and count(//aside)<2)]', $page->documentElement);

for ($c = $candidates->length - 1; $c >= 0; --$c) {
$node = $candidates->item($c);
// node should be readable but not inside of an article otherwise it's probably non-readable block
if ($node->hasAttribute('readability') && (int) $node->getAttributeNode('readability')->value < 40 && ($node->parentNode ? 0 !== strcasecmp($node->parentNode->tagName, 'article') : true)) {
if ($node->hasAttribute('readability') && (int) $node->getAttributeNode('readability')->value < 40 && ($node->parentNode instanceof \DOMElement ? 0 !== strcasecmp($node->parentNode->tagName, 'article') : true)) {
$this->logger->debug('Removing unlikely candidate (using note) ' . $node->getNodePath() . ' by "' . $node->tagName . '" with readability ' . self::getContentScore($node));
$node->parentNode->removeChild($node);
}
Expand All @@ -1180,6 +1182,7 @@ protected function grabArticle(?\DOMElement $page = null)
$topCandidates = array_fill(0, 5, null);
if ($xpath) {
// Using array of DOMElements after deletion is a path to DOOMElement.
/** @var \DOMNodeList<\DOMElement> */
$candidates = $xpath->query('.//*[@data-candidate]', $page->documentElement);
$this->logger->debug('Candidates: ' . $candidates->length);

Expand All @@ -1206,6 +1209,7 @@ protected function grabArticle(?\DOMElement $page = null)
}
}

/** @var \DOMNodeList<\DOMElement> */
$topCandidates = array_filter(
$topCandidates,
fn ($v, $idx) => 0 === $idx || null !== $v,
Expand Down Expand Up @@ -1323,19 +1327,19 @@ protected function grabArticle(?\DOMElement $page = null)
$siblingNode = $siblingNodes->item($s);
$siblingNodeName = $siblingNode->nodeName;
$append = false;
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . (($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));

if ($siblingNode->isSameNode($topCandidate)) {
$append = true;
} else {
$contentBonus = 0;

// Give a bonus if sibling nodes and top candidates have the same classname.
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
if ($siblingNode instanceof \DOMElement && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
$contentBonus += ((int) $topCandidate->getAttribute('readability')) * 0.2;
}

if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
if ($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
$append = true;
} elseif (0 === strcasecmp($siblingNodeName, 'p')) {
$linkDensity = (int) $this->getLinkDensity($siblingNode);
Expand Down Expand Up @@ -1565,7 +1569,7 @@ private function getAncestors(\DOMElement $node, int $maxDepth = 0): array

private function isPhrasingContent($node): bool
{
return \XML_TEXT_NODE === $node->nodeType
return $node instanceof \DOMText
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
|| (
\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true)
Expand Down

0 comments on commit 55c9999

Please sign in to comment.