From 57536814a265f5a79a1742e8efc927a9a8dc5934 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 2 Jun 2024 18:38:09 +0900 Subject: [PATCH] improve README --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 44b2167a..1b3115f6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,11 @@ 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. +* These rules make skipped object types explicit, param types narrow and help you to fill more accurate object type hints. +* They're easy to enable, even if your code does not pass level 0 +* They're effortless to resolve. + +If you care about code quality and type safety, add these rules to your CI.
@@ -20,7 +24,59 @@ composer require rector/type-perfect --dev
+There are 2 checks enabled out of the box. First one makes sure we don't miss a chance to use `instanceof` to make further code know about exact object type: + +```php +private ?SomeType $someType = null; + +if (! empty($this->someType)) { + // ... +} + +if (! isset($this->someType)) { + // ... +} + +// here we only know, that $this->someType is not empty/null +``` + +:no_good: + +↓ + + +```php +if (! $this->someType instanceof SomeType) { + return; +} + +// here we know $this->someType is exactly SomeType +``` + +:heavy_check_mark: + +
+ +Second rule checks we use explicit object methods over magic array access: + +```php +$article = new Article(); + +$id = $article['id']; +// we have no idea, what the type is +``` +:no_good: +↓ + +```php +$id = $article->getId(); +// we know the type is int +``` + +:heavy_check_mark: + +
## Configure @@ -34,10 +90,27 @@ parameters: null_over_false: true ``` -## Narrow Param Types +## 1. Narrow Param Types + +```php + +``` + +## 2. No mixed Caller ```php +``` + +## 3. Null over False + +```php + +``` + + + + Add sets one by one, fix what you find useful and ignore the rest.