From c928057ae712a9581e39d6a21d91dff85b602216 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 11 Feb 2025 16:06:31 +0100 Subject: [PATCH] [TASK] Add strict PHPStan rules Now we are using the same static analysis tools as our sister project. --- composer.json | 1 + config/phpstan-baseline.neon | 150 +++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) diff --git a/composer.json b/composer.json index b147d6a7..65c984ca 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "phpstan/extension-installer": "1.4.3", "phpstan/phpstan": "1.12.16 || 2.1.2", "phpstan/phpstan-phpunit": "1.4.2 || 2.0.4", + "phpstan/phpstan-strict-rules": "1.6.2 || 2.0.3", "phpunit/phpunit": "8.5.41", "rector/rector": "1.2.10 || 2.0.7" }, diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 2551deed..17b94e84 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -12,6 +12,12 @@ parameters: count: 1 path: ../src/CSSList/AtRuleBlockList.php + - + message: '#^Only booleans are allowed in an if condition, string given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/CSSList/AtRuleBlockList.php + - message: '#^Parameter &\$result by\-ref type of method Sabberworm\\CSS\\CSSList\\CSSBlockList\:\:allSelectors\(\) expects array\, array\ given\.$#' identifier: parameterByRef.type @@ -48,6 +54,24 @@ parameters: count: 1 path: ../src/CSSList/CSSList.php + - + message: '#^Loose comparison via "\!\=" is not allowed\.$#' + identifier: notEqual.notAllowed + count: 1 + path: ../src/CSSList/CSSList.php + + - + message: '#^Loose comparison via "\=\=" is not allowed\.$#' + identifier: equal.notAllowed + count: 1 + path: ../src/CSSList/CSSList.php + + - + message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' + identifier: ternary.shortNotAllowed + count: 1 + path: ../src/CSSList/CSSList.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -66,6 +90,12 @@ parameters: count: 1 path: ../src/CSSList/KeyFrame.php + - + message: '#^Variable property access on \$this\(Sabberworm\\CSS\\OutputFormat\)\.$#' + identifier: property.dynamicName + count: 4 + path: ../src/OutputFormat.php + - message: '#^Default value of the parameter \#2 \$bIncludeEnd \(false\) of method Sabberworm\\CSS\\Parsing\\ParserState\:\:consumeUntil\(\) is incompatible with type string\.$#' identifier: parameter.defaultValue @@ -78,6 +108,24 @@ parameters: count: 1 path: ../src/Parsing/ParserState.php + - + message: '#^Loose comparison via "\=\=" is not allowed\.$#' + identifier: equal.notAllowed + count: 1 + path: ../src/Parsing/ParserState.php + + - + message: '#^Only booleans are allowed in a negated boolean, string given\.$#' + identifier: booleanNot.exprNotBoolean + count: 1 + path: ../src/Parsing/ParserState.php + + - + message: '#^Only booleans are allowed in an if condition, string given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/Parsing/ParserState.php + - message: '#^PHPDoc tag @return with type array\\|void is not subtype of native type array\.$#' identifier: return.phpDocType @@ -90,6 +138,12 @@ parameters: count: 1 path: ../src/Property/CSSNamespace.php + - + message: '#^Only booleans are allowed in an if condition, string given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/Property/CSSNamespace.php + - message: '#^Return type \(array\\) of method Sabberworm\\CSS\\Property\\CSSNamespace\:\:atRuleArgs\(\) should be compatible with return type \(string\|null\) of method Sabberworm\\CSS\\Property\\AtRule\:\:atRuleArgs\(\)$#' identifier: method.childReturnType @@ -114,6 +168,12 @@ parameters: count: 1 path: ../src/Property/Import.php + - + message: '#^Only booleans are allowed in an if condition, string given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/Property/Import.php + - message: '#^Return type \(array\\) of method Sabberworm\\CSS\\Property\\Import\:\:atRuleArgs\(\) should be compatible with return type \(string\|null\) of method Sabberworm\\CSS\\Property\\AtRule\:\:atRuleArgs\(\)$#' identifier: method.childReturnType @@ -132,6 +192,18 @@ parameters: count: 1 path: ../src/Rule/Rule.php + - + message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' + identifier: empty.notAllowed + count: 1 + path: ../src/Rule/Rule.php + + - + message: '#^Only booleans are allowed in an if condition, Sabberworm\\CSS\\Value\\RuleValueList\|string\|null given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/Rule/Rule.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -144,6 +216,12 @@ parameters: count: 1 path: ../src/RuleSet/AtRuleSet.php + - + message: '#^Only booleans are allowed in an if condition, string given\.$#' + identifier: if.condNotBoolean + count: 1 + path: ../src/RuleSet/AtRuleSet.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -174,6 +252,24 @@ parameters: count: 1 path: ../src/RuleSet/DeclarationBlock.php + - + message: '#^Foreach overwrites \$mSelector with its value variable\.$#' + identifier: foreach.valueOverwrite + count: 1 + path: ../src/RuleSet/DeclarationBlock.php + + - + message: '#^Loose comparison via "\!\=" is not allowed\.$#' + identifier: notEqual.notAllowed + count: 1 + path: ../src/RuleSet/DeclarationBlock.php + + - + message: '#^Loose comparison via "\=\=" is not allowed\.$#' + identifier: equal.notAllowed + count: 1 + path: ../src/RuleSet/DeclarationBlock.php + - message: '#^Argument of an invalid type Sabberworm\\CSS\\Rule\\Rule supplied for foreach, only iterables are supported\.$#' identifier: foreach.nonIterable @@ -210,6 +306,30 @@ parameters: count: 1 path: ../src/RuleSet/RuleSet.php + - + message: '#^Only booleans are allowed in a negated boolean, string\|null given\.$#' + identifier: booleanNot.exprNotBoolean + count: 2 + path: ../src/RuleSet/RuleSet.php + + - + message: '#^Parameter \#1 \$comments \(array\\) of method Sabberworm\\CSS\\RuleSet\\RuleSet\:\:addComments\(\) should be contravariant with parameter \$comments \(array\\) of method Sabberworm\\CSS\\Comment\\Commentable\:\:addComments\(\)$#' + identifier: method.childParameterType + count: 1 + path: ../src/RuleSet/RuleSet.php + + - + message: '#^Parameter \#1 \$comments \(array\\) of method Sabberworm\\CSS\\RuleSet\\RuleSet\:\:setComments\(\) should be contravariant with parameter \$comments \(array\\) of method Sabberworm\\CSS\\Comment\\Commentable\:\:setComments\(\)$#' + identifier: method.childParameterType + count: 1 + path: ../src/RuleSet/RuleSet.php + + - + message: '#^Loose comparison via "\!\=" is not allowed\.$#' + identifier: notEqual.notAllowed + count: 3 + path: ../src/Value/CalcFunction.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:implode\(\)\.$#' identifier: method.notFound @@ -234,12 +354,30 @@ parameters: count: 1 path: ../src/Value/Color.php + - + message: '#^Call to method Sabberworm\\CSS\\Value\\Color\:\:hasNoneAsComponentValue\(\) with incorrect case\: HasNoneAsComponentValue$#' + identifier: method.nameCase + count: 1 + path: ../src/Value/Color.php + - message: '#^Cannot call method getSize\(\) on Sabberworm\\CSS\\Value\\Value\|string\.$#' identifier: method.nonObject count: 3 path: ../src/Value/Color.php + - + message: '#^Loose comparison via "\=\=" is not allowed\.$#' + identifier: equal.notAllowed + count: 3 + path: ../src/Value/Color.php + + - + message: '#^Loose comparison via "\!\=" is not allowed\.$#' + identifier: notEqual.notAllowed + count: 1 + path: ../src/Value/Size.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:implode\(\)\.$#' identifier: method.notFound @@ -258,3 +396,15 @@ parameters: count: 1 path: ../src/Value/ValueList.php + - + message: '#^Dynamic call to static method Sabberworm\\CSS\\Tests\\ParserTest\:\:parsedStructureForFile\(\)\.$#' + identifier: staticMethod.dynamicCall + count: 2 + path: ../tests/ParserTest.php + + - + message: '#^Loose comparison via "\=\=" is not allowed\.$#' + identifier: equal.notAllowed + count: 11 + path: ../tests/ParserTest.php +