From 9b00eb458548b3a554d04ec1826b4f780077bc40 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Sun, 23 Feb 2020 13:49:25 +0100 Subject: [PATCH] [+]: update "symfony/css-selector" -> fix phpstan reported errors --- .travis.yml | 5 +++-- composer.json | 4 ++-- phpcs.php_cs | 2 +- phpstan.neon | 12 ++++++------ phpunit.xml | 3 --- src/CssToInlineStyles.php | 23 +++++++++++++++-------- src/Specificity.php | 10 ++++++---- 7 files changed, 33 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2b0180f..10ebe43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,19 +8,20 @@ php: - 7.1 - 7.2 - 7.3 + - 7.4 before_script: - wget https://scrutinizer-ci.com/ocular.phar - travis_retry composer self-update - travis_retry composer require satooshi/php-coveralls - - travis_retry composer require phpstan/phpstan-shim + - if [ "$(phpenv version-name)" == 7.3 ]; then travis_retry composer require phpstan/phpstan; fi - travis_retry composer install --no-interaction --prefer-source - composer dump-autoload -o script: - mkdir -p build/logs - php vendor/bin/phpunit -c phpunit.xml - - php vendor/bin/phpstan analyse --level=7 --configuration=phpstan.neon src + - if [ "$(phpenv version-name)" == 7.3 ]; then php vendor/bin/phpstan analyse; fi after_script: - php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 20f64ab..21ff614 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,8 @@ ], "require": { "php": ">=7.0.0", - "symfony/css-selector": "~3.0 || ~4.0", - "voku/simple_html_dom": "~4.5", + "symfony/css-selector": "~3.0 || ~4.0 || ~5.0", + "voku/simple_html_dom": "~4.7", "ext-dom": "*" }, "require-dev": { diff --git a/phpcs.php_cs b/phpcs.php_cs index bbc6028..dac9ef4 100644 --- a/phpcs.php_cs +++ b/phpcs.php_cs @@ -131,7 +131,7 @@ return PhpCsFixer\Config::create() 'no_unset_on_property' => true, 'no_unused_imports' => true, 'no_useless_else' => true, - 'no_useless_return' => true, + 'no_useless_return' => false, // allow void 'no_whitespace_before_comma_in_array' => true, 'no_whitespace_in_blank_line' => true, 'non_printable_character' => true, diff --git a/phpstan.neon b/phpstan.neon index 1f63cd8..efed087 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,9 +1,9 @@ parameters: + level: max reportUnmatchedIgnoredErrors: false - excludes_analyse: - - %rootDir%/vendor/* - - %rootDir%/tests/* - autoload_files: - - %rootDir%/vendor/autoload.php + checkGenericClassInNonGenericObjectType: true + checkMissingIterableValueType: false + paths: + - %currentWorkingDirectory%/src/ ignoreErrors: - - '#Binary operation "-" between voku\\CssToInlineStyles\\Specificity#' + - '#Unsafe usage of new static#' diff --git a/phpunit.xml b/phpunit.xml index 6ea18f9..d7d3dbb 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,9 +6,6 @@ ./src/ - - ./vendor - diff --git a/src/CssToInlineStyles.php b/src/CssToInlineStyles.php index 7d659f2..57a1440 100644 --- a/src/CssToInlineStyles.php +++ b/src/CssToInlineStyles.php @@ -441,10 +441,12 @@ public function setUseInlineStylesBlock(bool $on = true): self * Remove id and class attributes. * * @param \DOMXPath $xPath the DOMXPath for the entire document + * + * @return void */ private function cleanupHTML(\DOMXPath $xPath) { - /** @var \DOMAttr[]|\DOMNodeList|false $nodes */ + /** @var \DOMAttr[]|\DOMNodeList<\DOMAttr>|false $nodes */ $nodes = $xPath->query('//@class | //@id'); if ($nodes !== false) { foreach ($nodes as $node) { @@ -546,7 +548,7 @@ private function createXPath(\DOMDocument $document, array $cssRules): \DOMXPath } // search elements - /** @var \DOMElement[]|\DOMNodeList|false $elements */ + /** @var \DOMElement[]|\DOMNodeList<\DOMElement>|false $elements */ $elements = $xPath->query($query); // validate elements @@ -556,8 +558,6 @@ private function createXPath(\DOMDocument $document, array $cssRules): \DOMXPath // loop found elements foreach ($elements as $element) { - - /** @var \DOMElement */ if ( $ruleSelector === '*' && @@ -799,10 +799,13 @@ private function processCSSProperties($propertyString): array * * INFO: Lower specificity will be sorted to the beginning of the array. * - * @param Specificity[] $e1 the first element - * @param Specificity[] $e2 the second element + * @param array $e1 the first element + * @param array $e2 the second element * * @return int + * + * @psalm-param array $e1 + * @psalm-param array $e2 */ private static function sortOnSpecificity(array $e1, array $e2): int { @@ -888,11 +891,13 @@ private function splitStyleIntoChunks(array $definedProperties): array * Strip style tags into the generated HTML. * * @param \DOMXPath $xPath the DOMXPath for the entire document + * + * @return void */ private function stripOriginalStyleTags(\DOMXPath $xPath) { // get all style tags - /** @var \DOMElement[]|\DOMNodeList|false $nodes */ + /** @var \DOMElement[]|\DOMNodeList<\DOMElement>|false $nodes */ $nodes = $xPath->query('descendant-or-self::style'); if ($nodes !== false) { foreach ($nodes as $node) { @@ -908,7 +913,9 @@ private function stripOriginalStyleTags(\DOMXPath $xPath) $node->nodeValue = \implode("\n", $mqs[0]); } else { // remove the entire style tag - $node->parentNode->removeChild($node); + if ($node->parentNode !== null) { + $node->parentNode->removeChild($node); + } } } } diff --git a/src/Specificity.php b/src/Specificity.php index a225532..0080b9d 100644 --- a/src/Specificity.php +++ b/src/Specificity.php @@ -52,6 +52,8 @@ public function __construct($a = 0, $b = 0, $c = 0) * @param int $a The number of ID selectors in the selector * @param int $b The number of class selectors, attributes selectors, and pseudo-classes in the selector * @param int $c The number of type selectors and pseudo-elements in the selector + * + * @return void */ public function increase($a, $b, $c) { @@ -110,10 +112,10 @@ public static function fromSelector(string $selector) )"; return new static( - \preg_match_all("/{$pattern_a}/ix", $selector, $matches), - \preg_match_all("/{$pattern_b}/ix", $selector, $matches), - \preg_match_all("/{$pattern_c}/ix", $selector, $matches) - ); + (int)\preg_match_all("/{$pattern_a}/ix", $selector, $matches), + (int)\preg_match_all("/{$pattern_b}/ix", $selector, $matches), + (int)\preg_match_all("/{$pattern_c}/ix", $selector, $matches) + ); } /**