Merge pull request #89 from jtojnar/php74

Require PHP 7.4
pull/88/head
Jérémy Benoist 2 years ago committed by GitHub
commit 29122763db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      .github/workflows/coding-standards.yml
  2. 6
      .github/workflows/continuous-integration.yml
  3. 4
      .php-cs-fixer.php
  4. 2
      composer.json
  5. 4
      rector.php
  6. 51
      src/Readability.php
  7. 2
      tests/ReadabilityTest.php

@ -16,7 +16,7 @@ jobs:
strategy: strategy:
matrix: matrix:
php: php:
- "7.2" - "7.4"
steps: steps:
- name: "Checkout" - name: "Checkout"

@ -19,8 +19,6 @@ jobs:
strategy: strategy:
matrix: matrix:
php: php:
- "7.2"
- "7.3"
- "7.4" - "7.4"
- "8.0" - "8.0"
- "8.1" - "8.1"
@ -65,7 +63,7 @@ jobs:
strategy: strategy:
matrix: matrix:
php: php:
- "7.4" - "8.0"
steps: steps:
- name: "Checkout" - name: "Checkout"
@ -116,7 +114,7 @@ jobs:
strategy: strategy:
matrix: matrix:
php: php:
- "7.2" - "7.4"
steps: steps:
- name: "Checkout" - name: "Checkout"

@ -26,6 +26,10 @@ return (new PhpCsFixer\Config())
'strict_comparison' => true, 'strict_comparison' => true,
'strict_param' => true, 'strict_param' => true,
'concat_space' => ['spacing' => 'one'], 'concat_space' => ['spacing' => 'one'],
// Pulled in by @Symfony:risky but we still support PHP 7.4
'modernize_strpos' => false,
// Pulled in by @Symfony, we cannot add property types until we bump PHP to ≥ 7.4
'no_null_property_initialization' => false,
]) ])
->setFinder($finder) ->setFinder($finder)
; ;

@ -24,7 +24,7 @@
"role": "Developer (original JS version)" "role": "Developer (original JS version)"
}], }],
"require": { "require": {
"php": ">=7.2.0", "php": ">=7.4.0",
"ext-mbstring": "*", "ext-mbstring": "*",
"psr/log": "^1.0.1 || ^2.0 || ^3.0", "psr/log": "^1.0.1 || ^2.0 || ^3.0",
"masterminds/html5": "^2.7" "masterminds/html5": "^2.7"

@ -23,9 +23,9 @@ return static function (RectorConfig $rectorConfig): void {
// Define what rule sets will be applied // Define what rule sets will be applied
$rectorConfig->sets([ $rectorConfig->sets([
LevelSetList::UP_TO_PHP_72, LevelSetList::UP_TO_PHP_74,
]); ]);
// is your PHP version different from the one your refactor to? // is your PHP version different from the one your refactor to?
$rectorConfig->phpVersion(PhpVersion::PHP_72); $rectorConfig->phpVersion(PhpVersion::PHP_74);
}; };

@ -142,7 +142,7 @@ class Readability implements LoggerAwareInterface
* @param string $parser Which parser to use for turning raw HTML into a DOMDocument * @param string $parser Which parser to use for turning raw HTML into a DOMDocument
* @param bool $useTidy Use tidy * @param bool $useTidy Use tidy
*/ */
public function __construct(string $html, string $url = null, string $parser = 'libxml', bool $useTidy = true) public function __construct(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true)
{ {
$this->url = $url; $this->url = $url;
$this->html = $html; $this->html = $html;
@ -739,7 +739,7 @@ class Readability implements LoggerAwareInterface
*/ */
public function addFlag(int $flag): void public function addFlag(int $flag): void
{ {
$this->flags = $this->flags | $flag; $this->flags |= $flag;
} }
/** /**
@ -747,7 +747,7 @@ class Readability implements LoggerAwareInterface
*/ */
public function removeFlag(int $flag): void public function removeFlag(int $flag): void
{ {
$this->flags = $this->flags & ~$flag; $this->flags &= ~$flag;
} }
/** /**
@ -893,11 +893,9 @@ class Readability implements LoggerAwareInterface
* Using a variety of metrics (content score, classname, element types), find the content that is * Using a variety of metrics (content score, classname, element types), find the content that is
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div. * most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
* *
* @param \DOMElement $page
*
* @return \DOMElement|false * @return \DOMElement|false
*/ */
protected function grabArticle(\DOMElement $page = null) protected function grabArticle(?\DOMElement $page = null)
{ {
if (!$page) { if (!$page) {
$page = $this->dom; $page = $this->dom;
@ -933,9 +931,9 @@ class Readability implements LoggerAwareInterface
// Remove unlikely candidates // Remove unlikely candidates
$unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style'); $unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style');
if (mb_strlen($unlikelyMatchString) > 3 && // don't process "empty" strings if (mb_strlen($unlikelyMatchString) > 3 // don't process "empty" strings
preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) && && preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString)
!preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) && !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString)
) { ) {
$this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '"'); $this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '"');
$node->parentNode->removeChild($node); $node->parentNode->removeChild($node);
@ -1120,9 +1118,11 @@ class Readability implements LoggerAwareInterface
} }
} }
$topCandidates = array_filter($topCandidates, function ($v, $idx) { $topCandidates = array_filter(
return 0 === $idx || null !== $v; $topCandidates,
}, \ARRAY_FILTER_USE_BOTH); fn ($v, $idx) => 0 === $idx || null !== $v,
\ARRAY_FILTER_USE_BOTH
);
$topCandidate = $topCandidates[0]; $topCandidate = $topCandidates[0];
/* /*
@ -1442,7 +1442,7 @@ class Readability implements LoggerAwareInterface
libxml_use_internal_errors(false); libxml_use_internal_errors(false);
} }
$this->dom->registerNodeClass(\DOMElement::class, \Readability\JSLikeHTMLElement::class); $this->dom->registerNodeClass(\DOMElement::class, JSLikeHTMLElement::class);
} }
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
@ -1464,9 +1464,17 @@ class Readability implements LoggerAwareInterface
{ {
return \XML_TEXT_NODE === $node->nodeType return \XML_TEXT_NODE === $node->nodeType
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true) || \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
|| (\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true) && !\in_array(false, array_map(function ($c) { || (
return $this->isPhrasingContent($c); \in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true)
}, iterator_to_array($node->childNodes)), true)); && !\in_array(
false,
array_map(
fn ($c) => $this->isPhrasingContent($c),
iterator_to_array($node->childNodes)
),
true
)
);
} }
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
@ -1475,10 +1483,10 @@ class Readability implements LoggerAwareInterface
return false; return false;
} }
$a = array_filter(iterator_to_array($node->childNodes), function ($childNode) { $a = array_filter(
return $childNode instanceof \DOMText && iterator_to_array($node->childNodes),
preg_match($this->regexps['hasContent'], $this->getInnerText($childNode)); fn ($childNode) => $childNode instanceof \DOMText && preg_match($this->regexps['hasContent'], $this->getInnerText($childNode))
}); );
return 0 === \count($a); return 0 === \count($a);
} }
@ -1491,7 +1499,8 @@ class Readability implements LoggerAwareInterface
*/ */
private function isNodeVisible(\DOMElement $node): bool private function isNodeVisible(\DOMElement $node): bool
{ {
return !($node->hasAttribute('style') return !(
$node->hasAttribute('style')
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style')) && preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
) )
&& !$node->hasAttribute('hidden'); && !$node->hasAttribute('hidden');

@ -550,7 +550,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
} }
} }
private function getReadability(string $html, string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability private function getReadability(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
{ {
$readability = new Readability($html, $url, $parser, $useTidy); $readability = new Readability($html, $url, $parser, $useTidy);

Loading…
Cancel
Save