From 8421ed5962ac03ad613b2d55348f116b67dc8f83 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 16 Mar 2024 16:42:54 +0100 Subject: [PATCH] Update coding style for upcoming PHP-CS-Fixer changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Once we bump minimum PHP version, we will get newer PHP-CS-Fixer, which will try to apply this cleanups. (partially cherry picked from commit 648d8c605b21c54fbcd395354181ed1b044cc664) Though avoid disabling `modernize_strpos` since it was only introduced in PHP-CS-Fixer 3.2.0: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/commit/2ca22a27c4ffb8cc46f20e7110b1dfcbb0c8c47c Also had to disable `visibility_required` for constants since those require PHP ≥ 7.1: https://cs.symfony.com/doc/rules/class_notation/visibility_required.html And remove type hint from `grabArticle` since implicitly nullable types were deprecated in PHP 8.4: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types But we cannot use explicitly nullable types, which require PHP ≥ 7.1: https://wiki.php.net/rfc/nullable_types Also switch code blocks to Markdown syntax to work around `phpdoc_separation`, ApiGen uses Markdown these days anyway. (partially cherry picked from commit 9ed89bde92764babdde6d10b16fffcb225944bcc) --- .php-cs-fixer.php | 4 ++++ src/JSLikeHTMLElement.php | 8 ++++---- src/Readability.php | 30 ++++++++++++++++-------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 19cd18d..93f51fe 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -26,6 +26,10 @@ return (new PhpCsFixer\Config()) 'strict_comparison' => true, 'strict_param' => true, 'concat_space' => ['spacing' => 'one'], + // Pulled in by @Symfony, we cannot add property types until we bump PHP to ≥ 7.4 + 'no_null_property_initialization' => false, + // Pulled in by @Symfony with `const` but const visibility requires PHP ≥ 7.1 + 'visibility_required' => ['elements' => ['method', 'property']], ]) ->setFinder($finder) ; diff --git a/src/JSLikeHTMLElement.php b/src/JSLikeHTMLElement.php index bb5c9ea..18116a6 100644 --- a/src/JSLikeHTMLElement.php +++ b/src/JSLikeHTMLElement.php @@ -39,9 +39,9 @@ class JSLikeHTMLElement extends \DOMElement /** * Used for setting innerHTML like it's done in JavaScript:. * - * @code + * ```php * $div->innerHTML = '

Chapter 2

The story begins...

'; - * @endcode + * ``` */ public function __set($name, $value) { @@ -104,9 +104,9 @@ class JSLikeHTMLElement extends \DOMElement /** * Used for getting innerHTML like it's done in JavaScript:. * - * @code + * ```php * $string = $div->innerHTML; - * @endcode + * ``` */ public function __get($name) { diff --git a/src/Readability.php b/src/Readability.php index 08963c0..1d2d2f5 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -144,7 +144,7 @@ class Readability implements LoggerAwareInterface // HACK: replace linebreaks plus br's with p's '!(]*>[ \r\n\s]*){2,}!i' => '

', // replace noscripts - //'!!is' => '', + // '!!is' => '', // replace fonts to spans '!<(/?)font[^>]*>!is' => '<\\1span>', ]; @@ -155,8 +155,8 @@ class Readability implements LoggerAwareInterface // replace empty tags that break layouts '!<(?:a|div|p|figure)[^>]+/>!is' => '', // remove all attributes on text tags - //'!<(\s*/?\s*(?:blockquote|br|hr|code|div|article|span|footer|aside|p|pre|dl|li|ul|ol)) [^>]+>!is' => "<\\1>", - //single newlines cleanup + // '!<(\s*/?\s*(?:blockquote|br|hr|code|div|article|span|footer|aside|p|pre|dl|li|ul|ol)) [^>]+>!is' => "<\\1>", + // single newlines cleanup "/\n+/" => "\n", // modern web... '!]*>\s* 'setAttribute('style', 'color: inherit; text-decoration: none;'); $articleLink->setAttribute('name', 'readabilityLink-' . $linkCount); $footnote->setInnerHtml('^ '); - $footnoteLink->setInnerHtml(('' !== $footnoteLink->getAttribute('title') ? $footnoteLink->getAttribute('title') : $linkText)); + $footnoteLink->setInnerHtml('' !== $footnoteLink->getAttribute('title') ? $footnoteLink->getAttribute('title') : $linkText); $footnoteLink->setAttribute('name', 'readabilityFootnoteLink-' . $linkCount); $footnote->appendChild($footnoteLink); @@ -796,7 +796,7 @@ class Readability implements LoggerAwareInterface */ public function addFlag($flag) { - $this->flags = $this->flags | $flag; + $this->flags |= $flag; } /** @@ -806,13 +806,14 @@ class Readability implements LoggerAwareInterface */ public function removeFlag($flag) { - $this->flags = $this->flags & ~$flag; + $this->flags &= ~$flag; } /** * Debug. * * @deprecated use $this->logger->debug() instead + * * @codeCoverageIgnore */ protected function dbg($msg) @@ -824,6 +825,7 @@ class Readability implements LoggerAwareInterface * Dump debug info. * * @deprecated since Monolog gather log, we don't need it + * * @codeCoverageIgnore */ protected function dump_dbg() @@ -973,11 +975,11 @@ class Readability implements LoggerAwareInterface * 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. * - * @param \DOMElement $page + * @param ?\DOMElement $page * * @return \DOMElement|false */ - protected function grabArticle(\DOMElement $page = null) + protected function grabArticle($page = null) { if (!$page) { $page = $this->dom; @@ -992,7 +994,7 @@ class Readability implements LoggerAwareInterface $allElements = $page->getElementsByTagName('*'); - for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); ++$nodeIndex) { + for ($nodeIndex = 0; $node = $allElements->item($nodeIndex); ++$nodeIndex) { $tagName = $node->tagName; $nodeContent = $node->getInnerHTML(); @@ -1136,9 +1138,9 @@ class Readability implements LoggerAwareInterface // Remove unlikely candidates $unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style'); - if (mb_strlen($unlikelyMatchString) > 3 && // don't process "empty" strings - preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) && - !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) + if (mb_strlen($unlikelyMatchString) > 3 // don't process "empty" strings + && preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) + && !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) ) { $this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '" with readability ' . ($node->hasAttribute('readability') ? (int) $node->getAttributeNode('readability')->value : 0)); $node->parentNode->removeChild($node); @@ -1289,8 +1291,8 @@ class Readability implements LoggerAwareInterface // To ensure a node does not interfere with readability styles, remove its classnames & ids. // Now done via RegExp post_filter. - //$nodeToAppend->removeAttribute('class'); - //$nodeToAppend->removeAttribute('id'); + // $nodeToAppend->removeAttribute('class'); + // $nodeToAppend->removeAttribute('id'); // Append sibling and subtract from our list as appending removes a node. $articleContent->appendChild($nodeToAppend); }