Skip to content

Commit 0ab1c73

Browse files
committed
Fix for upcoming PHP-CS-Fixer changes
Once we bump minimum PHP version, we will get newer PHP-CS-Fixer, which will try to apply this cleanups.
1 parent 38870cd commit 0ab1c73

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

.php-cs-fixer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
'strict_comparison' => true,
2727
'strict_param' => true,
2828
'concat_space' => ['spacing' => 'one'],
29+
// Pulled in by @Symfony:risky but we still support PHP 7.4
30+
'modernize_strpos' => false,
31+
// Pulled in by @Symfony, we cannot add property types until we bump PHP to ≥ 7.4
32+
'no_null_property_initialization' => false,
2933
])
3034
->setFinder($finder)
3135
;

src/Readability.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Readability implements LoggerAwareInterface
142142
* @param string $parser Which parser to use for turning raw HTML into a DOMDocument
143143
* @param bool $useTidy Use tidy
144144
*/
145-
public function __construct(string $html, string $url = null, string $parser = 'libxml', bool $useTidy = true)
145+
public function __construct(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true)
146146
{
147147
$this->url = $url;
148148
$this->html = $html;
@@ -739,15 +739,15 @@ public function flagIsActive(int $flag): bool
739739
*/
740740
public function addFlag(int $flag): void
741741
{
742-
$this->flags = $this->flags | $flag;
742+
$this->flags |= $flag;
743743
}
744744

745745
/**
746746
* Remove a flag.
747747
*/
748748
public function removeFlag(int $flag): void
749749
{
750-
$this->flags = $this->flags & ~$flag;
750+
$this->flags &= ~$flag;
751751
}
752752

753753
/**
@@ -893,11 +893,9 @@ protected function initializeNode(\DOMElement $node): void
893893
* Using a variety of metrics (content score, classname, element types), find the content that is
894894
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
895895
*
896-
* @param \DOMElement $page
897-
*
898896
* @return \DOMElement|false
899897
*/
900-
protected function grabArticle(\DOMElement $page = null)
898+
protected function grabArticle(?\DOMElement $page = null)
901899
{
902900
if (!$page) {
903901
$page = $this->dom;
@@ -933,9 +931,9 @@ protected function grabArticle(\DOMElement $page = null)
933931
// Remove unlikely candidates
934932
$unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style');
935933

936-
if (mb_strlen($unlikelyMatchString) > 3 && // don't process "empty" strings
937-
preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) &&
938-
!preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString)
934+
if (mb_strlen($unlikelyMatchString) > 3 // don't process "empty" strings
935+
&& preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString)
936+
&& !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString)
939937
) {
940938
$this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '"');
941939
$node->parentNode->removeChild($node);
@@ -1120,9 +1118,13 @@ protected function grabArticle(\DOMElement $page = null)
11201118
}
11211119
}
11221120

1123-
$topCandidates = array_filter($topCandidates, function ($v, $idx) {
1124-
return 0 === $idx || null !== $v;
1125-
}, \ARRAY_FILTER_USE_BOTH);
1121+
$topCandidates = array_filter(
1122+
$topCandidates,
1123+
function ($v, $idx) {
1124+
return 0 === $idx || null !== $v;
1125+
},
1126+
\ARRAY_FILTER_USE_BOTH
1127+
);
11261128
$topCandidate = $topCandidates[0];
11271129

11281130
/*
@@ -1442,7 +1444,7 @@ private function loadHtml(): void
14421444
libxml_use_internal_errors(false);
14431445
}
14441446

1445-
$this->dom->registerNodeClass(\DOMElement::class, \Readability\JSLikeHTMLElement::class);
1447+
$this->dom->registerNodeClass(\DOMElement::class, JSLikeHTMLElement::class);
14461448
}
14471449

14481450
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
@@ -1464,9 +1466,18 @@ private function isPhrasingContent($node): bool
14641466
{
14651467
return \XML_TEXT_NODE === $node->nodeType
14661468
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
1467-
|| (\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true) && !\in_array(false, array_map(function ($c) {
1468-
return $this->isPhrasingContent($c);
1469-
}, iterator_to_array($node->childNodes)), true));
1469+
|| (\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true)
1470+
&& !\in_array(
1471+
false,
1472+
array_map(
1473+
function ($c) {
1474+
return $this->isPhrasingContent($c);
1475+
},
1476+
iterator_to_array($node->childNodes)
1477+
),
1478+
true
1479+
)
1480+
);
14701481
}
14711482

14721483
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
@@ -1475,10 +1486,12 @@ private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
14751486
return false;
14761487
}
14771488

1478-
$a = array_filter(iterator_to_array($node->childNodes), function ($childNode) {
1479-
return $childNode instanceof \DOMText &&
1480-
preg_match($this->regexps['hasContent'], $this->getInnerText($childNode));
1481-
});
1489+
$a = array_filter(
1490+
iterator_to_array($node->childNodes),
1491+
function ($childNode) {
1492+
return $childNode instanceof \DOMText && preg_match($this->regexps['hasContent'], $this->getInnerText($childNode));
1493+
}
1494+
);
14821495

14831496
return 0 === \count($a);
14841497
}
@@ -1491,9 +1504,10 @@ private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
14911504
*/
14921505
private function isNodeVisible(\DOMElement $node): bool
14931506
{
1494-
return !($node->hasAttribute('style')
1495-
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
1507+
return !(
1508+
$node->hasAttribute('style')
1509+
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
14961510
)
1497-
&& !$node->hasAttribute('hidden');
1511+
&& !$node->hasAttribute('hidden');
14981512
}
14991513
}

tests/ReadabilityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public function testVisibleNode(string $content, bool $shouldBeVisible): void
550550
}
551551
}
552552

553-
private function getReadability(string $html, string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
553+
private function getReadability(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
554554
{
555555
$readability = new Readability($html, $url, $parser, $useTidy);
556556

0 commit comments

Comments
 (0)