Skip to content

Commit 55c9999

Browse files
committed
Raise PHPStan to level 4
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.
1 parent d6cd134 commit 55c9999

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-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: 1
2+
level: 4
33
paths:
44
- src
55
- tests

src/Readability.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ public function prepArticle(\DOMNode $articleContent): void
474474
}
475475

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

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

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

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

13281332
if ($siblingNode->isSameNode($topCandidate)) {
13291333
$append = true;
13301334
} else {
13311335
$contentBonus = 0;
13321336

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

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

15661570
private function isPhrasingContent($node): bool
15671571
{
1568-
return \XML_TEXT_NODE === $node->nodeType
1572+
return $node instanceof \DOMText
15691573
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
15701574
|| (
15711575
\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true)

0 commit comments

Comments
 (0)