Skip to content

Commit 74d1c9a

Browse files
committed
Report ?-> call on always-null
1 parent 411adf2 commit 74d1c9a

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,10 @@ private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Clo
16471647
private function ensureShallowNonNullability(MutatingScope $scope, Scope $originalScope, Expr $exprToSpecify): EnsuredNonNullabilityResult
16481648
{
16491649
$exprType = $scope->getType($exprToSpecify);
1650+
$isNull = $exprType->isNull();
1651+
if ($isNull->yes()) {
1652+
return new EnsuredNonNullabilityResult($scope, []);
1653+
}
16501654
$exprTypeWithoutNull = TypeCombinator::removeNull($exprType);
16511655
if ($exprType->equals($exprTypeWithoutNull)) {
16521656
$originalExprType = $originalScope->getType($exprToSpecify);

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,14 @@ public function testNullSafe(): void
17421742
'Parameter #1 $passedByRef of method NullsafeMethodCall\Foo::doBaz() is passed by reference, so it expects variables only.',
17431743
27,
17441744
],
1745+
[
1746+
'Cannot call method foo() on null.',
1747+
33,
1748+
],
1749+
[
1750+
'Cannot call method foo() on null.',
1751+
34,
1752+
],
17451753
]);
17461754
}
17471755

tests/PHPStan/Rules/Methods/data/nullsafe-method-call.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public function doLorem(?self $selfOrNull): void
2727
$this->doBaz($selfOrNull?->test->test);
2828
}
2929

30+
public function doNull(): void
31+
{
32+
$null = null;
33+
$null->foo();
34+
$null?->foo();
35+
}
36+
3037
}

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ public function testNullSafe(): void
487487
'Cannot access property $bar on string.',
488488
22,
489489
],
490+
[
491+
'Cannot access property $foo on null.',
492+
28,
493+
],
494+
[
495+
'Cannot access property $foo on null.',
496+
29,
497+
],
490498
]);
491499
}
492500

tests/PHPStan/Rules/Properties/data/bug-4559.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
class HelloWorld
66
{
7-
public function doBar()
7+
public function doBar(string $s)
88
{
9-
$response = json_decode('');
9+
$response = json_decode($s);
1010
if (isset($response->error->code)) {
1111
echo $response->error->message ?? '';
1212
}

tests/PHPStan/Rules/Properties/data/nullsafe-property-fetch.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public function doBar(string $string, ?string $nullableString): void
2222
echo $nullableString?->bar ?? 4;
2323
}
2424

25+
public function doNull(): void
26+
{
27+
$null = null;
28+
$null->foo;
29+
$null?->foo;
30+
}
31+
2532
}

0 commit comments

Comments
 (0)