Skip to content

Commit 6a7f008

Browse files
committed
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.
1 parent c6d0eb2 commit 6a7f008

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Readability.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function init(): bool
308308
{
309309
$this->loadHtml();
310310

311-
if (!isset($this->dom->documentElement)) {
311+
if (null === $this->dom || null === $this->dom->documentElement) {
312312
return false;
313313
}
314314

@@ -400,7 +400,7 @@ public function addFootnotes(\DOMElement $articleContent): void
400400
$refLink = $this->dom->createElement('a');
401401
$footnote = $this->dom->createElement('li');
402402
$linkDomain = @parse_url($footnoteLink->getAttribute('href'), \PHP_URL_HOST);
403-
if (!$linkDomain && isset($this->url)) {
403+
if (!$linkDomain && null !== $this->url) {
404404
$linkDomain = @parse_url($this->url, \PHP_URL_HOST);
405405
}
406406

@@ -1223,15 +1223,16 @@ protected function grabArticle(?JSLikeHTMLElement $page = null)
12231223
$topCandidate = $this->dom->createElement('div');
12241224

12251225
if ($page instanceof \DOMDocument) {
1226-
if (!isset($page->documentElement)) {
1226+
$documentElement = $page->documentElement;
1227+
if (null === $documentElement) {
12271228
// we don't have a body either? what a mess! :)
12281229
$this->logger->debug('The page has no body!');
12291230
} else {
12301231
$this->logger->debug('Setting body to a raw HTML of original page!');
1231-
$topCandidate->setInnerHtml($page->documentElement->getInnerHTML());
1232-
$page->documentElement->setInnerHtml('');
1232+
$topCandidate->setInnerHtml($documentElement->getInnerHTML());
1233+
$documentElement->setInnerHtml('');
12331234
$this->reinitBody();
1234-
$page->documentElement->appendChild($topCandidate);
1235+
$documentElement->appendChild($topCandidate);
12351236
}
12361237
} else {
12371238
$topCandidate->setInnerHtml($page->getInnerHTML());
@@ -1457,7 +1458,7 @@ protected function weightAttribute(JSLikeHTMLElement $element, string $attribute
14571458
*/
14581459
protected function reinitBody(): void
14591460
{
1460-
if (!isset($this->body->childNodes)) {
1461+
if (null === $this->body) {
14611462
$this->body = $this->dom->createElement('body');
14621463
$this->body->setInnerHtml($this->bodyCache);
14631464
}

0 commit comments

Comments
 (0)