From 28f1b08fac6a8bc21643d46ad85181681fe91878 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 4 Feb 2022 13:17:33 +0100 Subject: [PATCH] Use `SetList::CODE_QUALITY` with Rector --- rector.php | 4 ++ src/JSLikeHTMLElement.php | 4 +- src/Readability.php | 90 +++++++++++++++++++-------------------- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/rector.php b/rector.php index 47e4da8..a8c05cb 100644 --- a/rector.php +++ b/rector.php @@ -4,7 +4,9 @@ declare(strict_types=1); use Rector\Core\Configuration\Option; use Rector\Core\ValueObject\PhpVersion; +use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; +use Rector\Set\ValueObject\SetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { @@ -26,6 +28,8 @@ return static function (ContainerConfigurator $containerConfigurator): void { // Define what rule sets will be applied $containerConfigurator->import(LevelSetList::UP_TO_PHP_72); + $containerConfigurator->import(PHPUnitSetList::PHPUNIT_80); + $containerConfigurator->import(SetList::CODE_QUALITY); // is your PHP version different from the one your refactor to? $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72); diff --git a/src/JSLikeHTMLElement.php b/src/JSLikeHTMLElement.php index 3f382e1..a6a6b33 100644 --- a/src/JSLikeHTMLElement.php +++ b/src/JSLikeHTMLElement.php @@ -53,7 +53,7 @@ class JSLikeHTMLElement extends \DOMElement } // first, empty the element - if (isset($this->childNodes)) { + if (null !== $this->childNodes) { for ($x = $this->childNodes->length - 1; $x >= 0; --$x) { $this->removeChild($this->childNodes->item($x)); } @@ -114,7 +114,7 @@ class JSLikeHTMLElement extends \DOMElement if ('innerHTML' === $name) { $inner = ''; - if (isset($this->childNodes)) { + if (null !== $this->childNodes) { foreach ($this->childNodes as $child) { $inner .= $this->ownerDocument->saveXML($child); } diff --git a/src/Readability.php b/src/Readability.php index 844ee1c..3201d98 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -220,7 +220,7 @@ class Readability implements LoggerAwareInterface { $this->loadHtml(); - if (!isset($this->dom->documentElement)) { + if (!(property_exists($this->dom, 'documentElement') && null !== $this->dom->documentElement)) { return false; } @@ -236,7 +236,7 @@ class Readability implements LoggerAwareInterface } } - if ($bodyElems->length > 0 && null === $this->body) { + if ($bodyElems->length > 0 && !$this->body instanceof \DOMElement) { $this->body = $bodyElems->item(0); } @@ -264,7 +264,7 @@ class Readability implements LoggerAwareInterface $overlay->appendChild($innerDiv); // without tidy the body can (sometimes) be wiped, so re-create it - if (false === isset($this->body->childNodes)) { + if (!(property_exists($this->body, 'childNodes') && null !== $this->body->childNodes)) { $this->body = $this->dom->createElement('body'); } @@ -313,7 +313,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); } @@ -433,7 +433,7 @@ class Readability implements LoggerAwareInterface } // add extra text to iframe tag to avoid an auto-closing iframe and then break the html code - if ($iframeCount) { + if (0 !== $iframeCount) { $iframe = $item->getElementsByTagName('iframe'); $iframe->item(0)->nodeValue = ' '; @@ -463,7 +463,7 @@ class Readability implements LoggerAwareInterface */ public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLines = false): string { - if (null === $e || !isset($e->textContent) || '' === $e->textContent) { + if (!$e instanceof \DOMNode || !(property_exists($e, 'textContent') && null !== $e->textContent) || '' === $e->textContent) { return ''; } @@ -679,29 +679,27 @@ class Readability implements LoggerAwareInterface $this->logger->debug(' more than 3 embeds'); $toRemove = true; } - } else { - if ($img > $p) { - $this->logger->debug(' more image elements than paragraph elements'); - $toRemove = true; - } elseif (!$isList && $li > $p) { - $this->logger->debug(' too many
  • elements, and parent is not