Skip to content

Commit

Permalink
README - add split on property fetch/method call (rectorphp#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Sep 8, 2024
1 parent 59124fe commit c36dd8e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
56 changes: 49 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ You can enable them all at once:
```yaml
parameters:
type_perfect:
no_mixed: true
no_mixed_property: true
no_mixed_caller: true
null_over_false: true
narrow_param: true
narrow_return: true
Expand Down Expand Up @@ -140,15 +141,56 @@ public function getProduct(): ?Product

<br>

## 2. No mixed Caller
## 2. No mixed Property

```yaml
parameters:
type_perfect:
no_mixed: true
no_mixed_property: true
```

This group of rules focuses on PHPStan blind spot. If we have a property/method call with unknown type, PHPStan is not be able to analyse it. It silently ignores it.
This rule focuses on PHPStan blind spot while fetching a property. If we have a property with unknown type, PHPStan is not be able to analyse it. It silently ignores it.

```php
private $someType;
public function run()
{
$this->someType->vale;
}
```

It doesn't see there is a typo in `vale` property name. It should be `value`

:no_good:



```php
private SomeType $someType;
public function run()
{
$this->someType->value;
}
```

This rule makes sure all property fetches know their type they're called on.

:heavy_check_mark:

<br>

## 3. No mixed Caller

```yaml
parameters:
type_perfect:
no_mixed_caller: true
```

Same as above, only for method calls:

```php
private $someType;
Expand All @@ -175,13 +217,13 @@ public function run()
}
```

This group makes sure all property fetches and methods call know their type they're called on.
This group makes sure methods call know their type they're called on.

:heavy_check_mark:

<br>

## 3. Narrow Param Types
## 4. Narrow Param Types

The more narrow param type we have, the reliable the code is. `string` beats `mixed`, `int` beats `scalar` and `ExactObject` beats `stdClass`.

Expand Down Expand Up @@ -227,7 +269,7 @@ That's where this group comes in. It checks all the passed types, and tells us k

<br>

## 4. Narrow Return Types
## 5. Narrow Return Types

Last but not least, the more narrow return type, the more reliable the code.

Expand Down
7 changes: 7 additions & 0 deletions config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ parametersSchema:
narrow_return: bool()
no_mixed: bool()
null_over_false: bool()

# replace for no_mixed
no_mixed_property: bool()
no_mixed_caller: bool()
])

# defaults
Expand All @@ -14,6 +18,9 @@ parameters:
narrow_return: false
no_mixed: false
null_over_false: false
# priority override of no_mixed
no_mixed_property: false
no_mixed_caller: false

rules:
# enabled by default
Expand Down
15 changes: 14 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ public function isNarrowReturnEnabled(): bool
return $this->parameters['narrow_return'] ?? false;
}

public function isNoMixedEnabled(): bool
public function isNoMixedPropertyEnabled(): bool
{
if ($this->parameters['no_mixed_property']) {
return true;
}

return $this->parameters['no_mixed'] ?? false;
}

public function isNoMixedCallerEnabled(): bool
{
if ($this->parameters['no_mixed_caller']) {
return true;
}

return $this->parameters['no_mixed'] ?? false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/NoMixedMethodCallerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $this->configuration->isNoMixedEnabled()) {
if (! $this->configuration->isNoMixedCallerEnabled()) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/NoMixedPropertyFetcherRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $this->configuration->isNoMixedEnabled()) {
if (! $this->configuration->isNoMixedPropertyEnabled()) {
return [];
}

Expand Down

0 comments on commit c36dd8e

Please sign in to comment.