Skip to content

Commit 1bfa56b

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 67ace8c commit 1bfa56b

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
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
@@ -423,6 +423,7 @@ public function prepArticle(\DOMNode $articleContent): void
423423
}
424424

425425
// Remove service data-candidate attribute.
426+
/** @var \DOMNodeList<\DOMElement> */
426427
$elems = $xpath->query('.//*[@data-candidate]', $articleContent);
427428
for ($i = $elems->length - 1; $i >= 0; --$i) {
428429
$elems->item($i)->removeAttribute('data-candidate');
@@ -1102,12 +1103,13 @@ protected function grabArticle(?\DOMElement $page = null)
11021103
* This is faster to do before scoring but safer after.
11031104
*/
11041105
if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS) && $xpath) {
1106+
/** @var \DOMNodeList<\DOMElement> */
11051107
$candidates = $xpath->query('.//*[(self::footer and count(//footer)<2) or (self::aside and count(//aside)<2)]', $page->documentElement);
11061108

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

@@ -1154,6 +1157,7 @@ protected function grabArticle(?\DOMElement $page = null)
11541157
}
11551158
}
11561159

1160+
/** @var \DOMNodeList<\DOMElement> */
11571161
$topCandidates = array_filter(
11581162
$topCandidates,
11591163
fn ($v, $idx) => 0 === $idx || null !== $v,
@@ -1276,19 +1280,19 @@ protected function grabArticle(?\DOMElement $page = null)
12761280
$siblingNode = $siblingNodes->item($s);
12771281
$siblingNodeName = $siblingNode->nodeName;
12781282
$append = false;
1279-
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
1283+
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . (($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
12801284

12811285
if ($siblingNode->isSameNode($topCandidate)) {
12821286
$append = true;
12831287
} else {
12841288
$contentBonus = 0;
12851289

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

1291-
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
1295+
if ($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
12921296
$append = true;
12931297
} elseif (0 === strcasecmp($siblingNodeName, 'p')) {
12941298
$linkDensity = (int) $this->getLinkDensity($siblingNode);
@@ -1518,7 +1522,7 @@ private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
15181522

15191523
private function isPhrasingContent($node): bool
15201524
{
1521-
return \XML_TEXT_NODE === $node->nodeType
1525+
return $node instanceof \DOMText
15221526
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
15231527
|| (
15241528
\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true)

tests/ReadabilityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function testAutoClosingIframeNotThrowingException(): void
286286
$oldErrorReporting = error_reporting(\E_ALL | \E_STRICT);
287287
$oldDisplayErrors = ini_set('display_errors', '1');
288288
// dummy function to be used to the next test
289-
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext): bool {
289+
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool {
290290
throw new \Exception($errstr, $errno);
291291
}, \E_ALL | \E_STRICT);
292292

0 commit comments

Comments
 (0)