From dafc720aebd6b0b89aa81169d3dadf681add9a4e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 2 Jun 2024 18:20:49 +0900 Subject: [PATCH] misc --- README.md | 20 ++++++++++++++++---- config/extension.neon | 43 +++++++++++++++++++++++-------------------- src/Configuration.php | 4 ++-- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 037276fc..44b2167a 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Next level type declaration check PHPStan rules. +We use these sets to improve code quality of our clients' code beyond PHPStan features. + +These rules make skipped object types explicit, param types narrow and help you to fill more accurate object type hints. If you care about code quality and type safety, add these rules to your CI. +
## Install @@ -16,16 +20,24 @@ composer require rector/type-perfect --dev
-@todo enable by configuration + + +## Configure + +Next rules you can enable by configuration: ```yaml parameters: type_coverage: - narrow: false - no_mixed: false - null_over_false: false + narrow_param: true + no_mixed: true + null_over_false: true ``` +## Narrow Param Types + +```php + Add sets one by one, fix what you find useful and ignore the rest.
diff --git a/config/extension.neon b/config/extension.neon index 6c7a29d6..8c0084cd 100644 --- a/config/extension.neon +++ b/config/extension.neon @@ -1,18 +1,38 @@ parametersSchema: # see https://doc.nette.org/en/schema for configuration type_perfect: structure([ - narrow: bool() + narrow_param: bool() no_mixed: bool() null_over_false: bool() ]) -# default parameters +# defaults parameters: type_perfect: - narrow: false + narrow_param: false no_mixed: false null_over_false: false +rules: + # enabled by default + - Rector\TypePerfect\Rules\NoParamTypeRemovalRule + - Rector\TypePerfect\Rules\NoArrayAccessOnObjectRule + - Rector\TypePerfect\Rules\NoIssetOnObjectRule + + # turn on by the group name + + # narrow_param + - Rector\TypePerfect\Rules\NarrowPublicClassMethodParamTypeRule + - Rector\TypePerfect\Rules\NarrowPrivateClassMethodParamTypeRule + - Rector\TypePerfect\Rules\NarrowReturnObjectTypeRule + + # no_mixed + - Rector\TypePerfect\Rules\NoMixedPropertyFetcherRule + - Rector\TypePerfect\Rules\NoMixedMethodCallerRule + + # null_over_false + - Rector\TypePerfect\Rules\ReturnNullOverFalseRule + services: - factory: Rector\TypePerfect\Configuration @@ -44,20 +64,3 @@ services: class: Rector\TypePerfect\Collector\MethodCallableNode\MethodCallableCollector tags: - phpstan.collector - -# these rule focus on the whole-project analysis, see https://phpstan.org/developing-extensions/collectors -rules: - # narrow types - - Rector\TypePerfect\Rules\NarrowPublicClassMethodParamTypeRule - - Rector\TypePerfect\Rules\NarrowPrivateClassMethodParamTypeRule - - Rector\TypePerfect\Rules\NarrowReturnObjectTypeRule - - Rector\TypePerfect\Rules\ReturnNullOverFalseRule - - # no mixed - - Rector\TypePerfect\Rules\NoMixedPropertyFetcherRule - - Rector\TypePerfect\Rules\NoMixedMethodCallerRule - - # simple - always enable these - - Rector\TypePerfect\Rules\NoParamTypeRemovalRule - - Rector\TypePerfect\Rules\NoArrayAccessOnObjectRule - - Rector\TypePerfect\Rules\NoIssetOnObjectRule diff --git a/src/Configuration.php b/src/Configuration.php index 01986caf..99170575 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -14,7 +14,7 @@ public function __construct( ) { // enabed by default in tests if (defined('PHPUNIT_COMPOSER_INSTALL')) { - $this->parameters['narrow'] = true; + $this->parameters['narrow_param'] = true; $this->parameters['no_mixed'] = true; $this->parameters['null_over_false'] = true; } @@ -22,7 +22,7 @@ public function __construct( public function isNarrowEnabled(): bool { - return $this->parameters['narrow'] ?? false; + return $this->parameters['narrow_param'] ?? false; } public function isNoMixedEnabled(): bool