Skip to content

Commit 7c0c857

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Fix specifying types on nullsafe true/false comparison
1 parent 50ef61e commit 7c0c857

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/Analyser/TypeSpecifier.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ private function specifyTypesForConstantBinaryExpression(
11341134
{
11351135
if (!$context->null() && $constantType->isFalse()->yes()) {
11361136
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
1137-
if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) {
1137+
if (!$context->true() && ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch)) {
11381138
return $types;
11391139
}
11401140

@@ -1148,7 +1148,7 @@ private function specifyTypesForConstantBinaryExpression(
11481148

11491149
if (!$context->null() && $constantType->isTrue()->yes()) {
11501150
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
1151-
if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) {
1151+
if (!$context->true() && ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch)) {
11521152
return $types;
11531153
}
11541154

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php declare(strict_types = 1); // lint >= 8.0
2+
3+
namespace Bug12866;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface I
8+
{
9+
/**
10+
* @phpstan-assert-if-true A $this
11+
*/
12+
public function isA(): bool;
13+
}
14+
15+
class A implements I
16+
{
17+
public function isA(): bool
18+
{
19+
return true;
20+
}
21+
}
22+
23+
class B implements I
24+
{
25+
public function isA(): bool
26+
{
27+
return false;
28+
}
29+
}
30+
31+
function takesI(I $i): void
32+
{
33+
if (!$i->isA()) {
34+
return;
35+
}
36+
37+
assertType('Bug12866\\A', $i);
38+
}
39+
40+
function takesIStrictComparison(I $i): void
41+
{
42+
if ($i->isA() !== true) {
43+
return;
44+
}
45+
46+
assertType('Bug12866\\A', $i);
47+
}
48+
49+
function takesNullableI(?I $i): void
50+
{
51+
if (!$i?->isA()) {
52+
return;
53+
}
54+
55+
assertType('Bug12866\\A', $i);
56+
}
57+
58+
function takesNullableIStrictComparison(?I $i): void
59+
{
60+
if ($i?->isA() !== true) {
61+
return;
62+
}
63+
64+
assertType('Bug12866\\A', $i);
65+
}

0 commit comments

Comments
 (0)