From 6a7f008f72c5ecbc90f3cb83076aa8a657b9f6aa Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 12 Oct 2024 13:41:38 +0200 Subject: [PATCH] Replace isset with clearer null check `isset` does two things: it checks if the entity resulting from the chain of accessors is defined, and if it is not `null`. We know that the properties are always defined so we can just replace those `isset`s with `null` checks. This will allow us to reason about the code more clearly. --- src/Readability.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Readability.php b/src/Readability.php index 6dea5fd..1432d90 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -308,7 +308,7 @@ class Readability implements LoggerAwareInterface { $this->loadHtml(); - if (!isset($this->dom->documentElement)) { + if (null === $this->dom || null === $this->dom->documentElement) { return false; } @@ -400,7 +400,7 @@ class Readability implements LoggerAwareInterface $refLink = $this->dom->createElement('a'); $footnote = $this->dom->createElement('li'); $linkDomain = @parse_url($footnoteLink->getAttribute('href'), \PHP_URL_HOST); - if (!$linkDomain && isset($this->url)) { + if (!$linkDomain && null !== $this->url) { $linkDomain = @parse_url($this->url, \PHP_URL_HOST); } @@ -1223,15 +1223,16 @@ class Readability implements LoggerAwareInterface $topCandidate = $this->dom->createElement('div'); if ($page instanceof \DOMDocument) { - if (!isset($page->documentElement)) { + $documentElement = $page->documentElement; + if (null === $documentElement) { // we don't have a body either? what a mess! :) $this->logger->debug('The page has no body!'); } else { $this->logger->debug('Setting body to a raw HTML of original page!'); - $topCandidate->setInnerHtml($page->documentElement->getInnerHTML()); - $page->documentElement->setInnerHtml(''); + $topCandidate->setInnerHtml($documentElement->getInnerHTML()); + $documentElement->setInnerHtml(''); $this->reinitBody(); - $page->documentElement->appendChild($topCandidate); + $documentElement->appendChild($topCandidate); } } else { $topCandidate->setInnerHtml($page->getInnerHTML()); @@ -1457,7 +1458,7 @@ class Readability implements LoggerAwareInterface */ protected function reinitBody(): void { - if (!isset($this->body->childNodes)) { + if (null === $this->body) { $this->body = $this->dom->createElement('body'); $this->body->setInnerHtml($this->bodyCache); }