From 3f352f8aa45b81bc8b141ed0c4142bf9a78a2f08 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 16 Feb 2025 11:30:33 +0100 Subject: [PATCH] [TASK] Add PHPStan rules from Rector Type Perfect This is an opiniated set of PHPStan rules to make code more explicit and intuitive to read. https://github.com/rectorphp/type-perfect The new rules show some parts where we could improve our code, which have now been added to the PHPStan baseline. --- composer.json | 3 +- config/phpstan-baseline.neon | 114 +++++++++++++++++++++++++++++++++++ config/phpstan.neon | 7 +++ 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 65c984ca..f07d3222 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "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" + "rector/rector": "1.2.10 || 2.0.7", + "rector/type-perfect": "1.0.0 || 2.0.2" }, "suggest": { "ext-mbstring": "for parsing UTF-8 CSS" diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index c10c9c9d..8b834124 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -78,6 +78,12 @@ parameters: count: 1 path: ../src/CSSList/Document.php + - + message: '#^Parameters should have "string\|null" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/CSSList/Document.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -90,6 +96,24 @@ parameters: count: 1 path: ../src/CSSList/KeyFrame.php + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 2 + path: ../src/CSSList/KeyFrame.php + + - + message: '#^Parameters should have "string\|string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/OutputFormat.php + + - + message: '#^Returning false in non return bool class method\. Use null with type\|null instead or add bool return type$#' + identifier: typePerfect.nullOverFalse + count: 1 + path: ../src/OutputFormat.php + - message: '#^Variable property access on \$this\(Sabberworm\\CSS\\OutputFormat\)\.$#' identifier: property.dynamicName @@ -132,6 +156,30 @@ parameters: count: 1 path: ../src/Parsing/ParserState.php + - + message: '#^Parameters should have "bool" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 2 + path: ../src/Parsing/ParserState.php + + - + message: '#^Parameters should have "int" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 2 + path: ../src/Parsing/ParserState.php + + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Parsing/ParserState.php + + - + message: '#^Parameters should have "string\|int\|null" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Parsing/ParserState.php + - message: '#^Cannot call method render\(\) on string\.$#' identifier: method.nonObject @@ -180,6 +228,12 @@ parameters: count: 1 path: ../src/Property/Import.php + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Property/Selector.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -204,6 +258,24 @@ parameters: count: 1 path: ../src/Rule/Rule.php + - + message: '#^Parameters should have "bool" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Rule/Rule.php + + - + message: '#^Parameters should have "int\|int" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Rule/Rule.php + + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Rule/Rule.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound @@ -270,6 +342,18 @@ parameters: count: 1 path: ../src/RuleSet/DeclarationBlock.php + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/RuleSet/DeclarationBlock.php + + - + message: '#^Returning false in non return bool class method\. Use null with type\|null instead or add bool return type$#' + identifier: typePerfect.nullOverFalse + 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 @@ -324,6 +408,24 @@ parameters: count: 1 path: ../src/RuleSet/RuleSet.php + - + message: '#^Parameters should have "Sabberworm\\CSS\\Rule\\Rule" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/RuleSet/RuleSet.php + + - + message: '#^Parameters should have "string" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/RuleSet/RuleSet.php + + - + message: '#^Use explicit methods over array access on object$#' + identifier: typePerfect.noArrayAccessOnObject + count: 1 + path: ../src/RuleSet/RuleSet.php + - message: '#^Loose comparison via "\!\=" is not allowed\.$#' identifier: notEqual.notAllowed @@ -372,12 +474,24 @@ parameters: count: 3 path: ../src/Value/Color.php + - + message: '#^Provide more specific return type "Sabberworm\\CSS\\Value\\Color" over abstract one$#' + identifier: typePerfect.narrowReturnObjectType + count: 1 + path: ../src/Value/Color.php + - message: '#^Loose comparison via "\!\=" is not allowed\.$#' identifier: notEqual.notAllowed count: 1 path: ../src/Value/Size.php + - + message: '#^Parameters should have "float" types as the only types passed to this method$#' + identifier: typePerfect.narrowPublicClassMethodParamType + count: 1 + path: ../src/Value/Size.php + - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:implode\(\)\.$#' identifier: method.notFound diff --git a/config/phpstan.neon b/config/phpstan.neon index 910181dd..06ce70f2 100644 --- a/config/phpstan.neon +++ b/config/phpstan.neon @@ -12,3 +12,10 @@ parameters: - %currentWorkingDirectory%/bin/ - %currentWorkingDirectory%/src/ - %currentWorkingDirectory%/tests/ + + type_perfect: + no_mixed_property: true + no_mixed_caller: true + null_over_false: true + narrow_param: true + narrow_return: true