Skip to content

Commit c36dd8e

Browse files
authored
README - add split on property fetch/method call (#40)
1 parent 59124fe commit c36dd8e

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

README.md

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ You can enable them all at once:
8888
```yaml
8989
parameters:
9090
type_perfect:
91-
no_mixed: true
91+
no_mixed_property: true
92+
no_mixed_caller: true
9293
null_over_false: true
9394
narrow_param: true
9495
narrow_return: true
@@ -140,15 +141,56 @@ public function getProduct(): ?Product
140141

141142
<br>
142143

143-
## 2. No mixed Caller
144+
## 2. No mixed Property
144145

145146
```yaml
146147
parameters:
147148
type_perfect:
148-
no_mixed: true
149+
no_mixed_property: true
149150
```
150151

151-
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.
152+
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.
153+
154+
```php
155+
private $someType;
156+
157+
public function run()
158+
{
159+
$this->someType->vale;
160+
}
161+
```
162+
163+
It doesn't see there is a typo in `vale` property name. It should be `value`
164+
165+
:no_good:
166+
167+
168+
169+
170+
```php
171+
private SomeType $someType;
172+
173+
public function run()
174+
{
175+
$this->someType->value;
176+
}
177+
```
178+
179+
This rule makes sure all property fetches know their type they're called on.
180+
181+
:heavy_check_mark:
182+
183+
<br>
184+
185+
## 3. No mixed Caller
186+
187+
```yaml
188+
parameters:
189+
type_perfect:
190+
no_mixed_caller: true
191+
```
192+
193+
Same as above, only for method calls:
152194

153195
```php
154196
private $someType;
@@ -175,13 +217,13 @@ public function run()
175217
}
176218
```
177219

178-
This group makes sure all property fetches and methods call know their type they're called on.
220+
This group makes sure methods call know their type they're called on.
179221

180222
:heavy_check_mark:
181223

182224
<br>
183225

184-
## 3. Narrow Param Types
226+
## 4. Narrow Param Types
185227

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

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

228270
<br>
229271

230-
## 4. Narrow Return Types
272+
## 5. Narrow Return Types
231273

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

config/extension.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ parametersSchema:
55
narrow_return: bool()
66
no_mixed: bool()
77
null_over_false: bool()
8+
9+
# replace for no_mixed
10+
no_mixed_property: bool()
11+
no_mixed_caller: bool()
812
])
913

1014
# defaults
@@ -14,6 +18,9 @@ parameters:
1418
narrow_return: false
1519
no_mixed: false
1620
null_over_false: false
21+
# priority override of no_mixed
22+
no_mixed_property: false
23+
no_mixed_caller: false
1724

1825
rules:
1926
# enabled by default

src/Configuration.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,21 @@ public function isNarrowReturnEnabled(): bool
3131
return $this->parameters['narrow_return'] ?? false;
3232
}
3333

34-
public function isNoMixedEnabled(): bool
34+
public function isNoMixedPropertyEnabled(): bool
3535
{
36+
if ($this->parameters['no_mixed_property']) {
37+
return true;
38+
}
39+
40+
return $this->parameters['no_mixed'] ?? false;
41+
}
42+
43+
public function isNoMixedCallerEnabled(): bool
44+
{
45+
if ($this->parameters['no_mixed_caller']) {
46+
return true;
47+
}
48+
3649
return $this->parameters['no_mixed'] ?? false;
3750
}
3851

src/Rules/NoMixedMethodCallerRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getNodeType(): string
4444
*/
4545
public function processNode(Node $node, Scope $scope): array
4646
{
47-
if (! $this->configuration->isNoMixedEnabled()) {
47+
if (! $this->configuration->isNoMixedCallerEnabled()) {
4848
return [];
4949
}
5050

src/Rules/NoMixedPropertyFetcherRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getNodeType(): string
4343
*/
4444
public function processNode(Node $node, Scope $scope): array
4545
{
46-
if (! $this->configuration->isNoMixedEnabled()) {
46+
if (! $this->configuration->isNoMixedPropertyEnabled()) {
4747
return [];
4848
}
4949

0 commit comments

Comments
 (0)