From f610333040aaf2ef3ddac2d67f886b52ecb26679 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Fri, 11 Oct 2024 23:00:43 +0200 Subject: [PATCH] Add @var casts for XPath queries Because PHPStan does not currently analyze XPath expressions, we need to use a @var cast: https://phpstan.org/writing-php-code/phpdocs-basics#inline-%40var These are list of elements since asterisk wildcard only selects elements: https://www.w3.org/TR/1999/REC-xpath-19991116/#path-abbrev --- src/Readability.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Readability.php b/src/Readability.php index 0bc2ec8..9bf62c0 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -474,6 +474,7 @@ class Readability implements LoggerAwareInterface } // Remove service data-candidate attribute. + /** @var \DOMNodeList<\DOMElement> */ $elems = $xpath->query('.//*[@data-candidate]', $articleContent); foreach ($elems as $elem) { $elem->removeAttribute('data-candidate'); @@ -1159,6 +1160,7 @@ class Readability implements LoggerAwareInterface * 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) { @@ -1180,6 +1182,7 @@ class Readability implements LoggerAwareInterface $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);