Skip to content

Commit 6f6e9d3

Browse files
staabmondrejmirtes
authored andcommitted
Consdier LastConditionVisitor in checkAlwaysTrueCheckTypeFunctionCall-rules
1 parent be22d68 commit 6f6e9d3

10 files changed

+88
-0
lines changed

src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Parser\LastConditionVisitor;
78
use PHPStan\Rules\Rule;
89
use PHPStan\Rules\RuleErrorBuilder;
910
use function sprintf;
@@ -65,6 +66,10 @@ public function processNode(Node $node, Scope $scope): array
6566
)))->build(),
6667
];
6768
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
69+
if ($node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME) === true) {
70+
return [];
71+
}
72+
6873
return [
6974
$addTip(RuleErrorBuilder::message(sprintf(
7075
'Call to function %s()%s will always evaluate to true.',

src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Parser\LastConditionVisitor;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
@@ -65,6 +66,10 @@ public function processNode(Node $node, Scope $scope): array
6566
)))->build(),
6667
];
6768
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
69+
if ($node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME) === true) {
70+
return [];
71+
}
72+
6873
$method = $this->getMethod($node->var, $node->name->name, $scope);
6974
return [
7075
$addTip(RuleErrorBuilder::message(sprintf(

src/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRule.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Parser\LastConditionVisitor;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
@@ -66,6 +67,10 @@ public function processNode(Node $node, Scope $scope): array
6667
)))->build(),
6768
];
6869
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) {
70+
if ($node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME) === true) {
71+
return [];
72+
}
73+
6974
$method = $this->getMethod($node->class, $node->name->name, $scope);
7075

7176
return [

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ public function testImpossibleCheckTypeFunctionCall(): void
238238
'Call to function testIsInt() with int will always evaluate to true.',
239239
875,
240240
],
241+
[
242+
'Call to function is_int() with int will always evaluate to true.',
243+
889,
244+
],
241245
],
242246
);
243247
}

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleEqualsTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public function testRule(): void
117117
'Call to method ImpossibleMethodCall\Foo::isNotSame() with 2 and 2 will always evaluate to false.',
118118
194,
119119
],
120+
[
121+
'Call to method ImpossibleMethodCall\ConditionalAlwaysTrue::isInt() with int will always evaluate to true.',
122+
208,
123+
],
120124
]);
121125
}
122126

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public function testRule(): void
149149
'Call to method ImpossibleMethodCall\Foo::isNotSame() with 2 and 2 will always evaluate to false.',
150150
194,
151151
],
152+
[
153+
'Call to method ImpossibleMethodCall\ConditionalAlwaysTrue::isInt() with int will always evaluate to true.',
154+
208,
155+
],
152156
]);
153157
}
154158

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function testRule(): void
6161
'Call to static method PHPStan\Tests\AssertionClass::assertInt() with arguments 1, 2 and 3 will always evaluate to true.',
6262
34,
6363
],
64+
[
65+
'Call to static method ImpossibleStaticMethodCall\ConditionalAlwaysTrue::isInt() with int will always evaluate to true.',
66+
66,
67+
],
6468
]);
6569
}
6670

tests/PHPStan/Rules/Comparison/data/check-type-function-call.php

+15
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,18 @@ function (int $int) {
876876

877877
}
878878
};
879+
880+
class ConditionalAlwaysTrue
881+
{
882+
public function sayHello(?int $date): void
883+
{
884+
if ($date === null) {
885+
} elseif (is_int($date)) { // always-true should not be reported because last condition
886+
}
887+
888+
if ($date === null) {
889+
} elseif (is_int($date)) { // always-true should be reported, because another condition below
890+
} elseif (rand(0,1)) {
891+
}
892+
}
893+
}

tests/PHPStan/Rules/Comparison/data/impossible-method-call.php

+21
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,24 @@ public function scalars()
195195
}
196196

197197
}
198+
199+
class ConditionalAlwaysTrue
200+
{
201+
public function sayHello(?int $date): void
202+
{
203+
if ($date === null) {
204+
} elseif ($this->isInt($date)) { // always-true should not be reported because last condition
205+
}
206+
207+
if ($date === null) {
208+
} elseif ($this->isInt($date)) { // always-true should be reported, because another condition below
209+
} elseif (rand(0,1)) {
210+
}
211+
}
212+
213+
/**
214+
* @phpstan-assert-if-true int $value
215+
*/
216+
public function isInt($value): bool {
217+
}
218+
}

tests/PHPStan/Rules/Comparison/data/impossible-static-method-call.php

+21
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,24 @@ public function nullableInt(): ?int
5353
}
5454

5555
}
56+
57+
class ConditionalAlwaysTrue
58+
{
59+
public function sayHello(?int $date): void
60+
{
61+
if ($date === null) {
62+
} elseif (self::isInt($date)) { // always-true should not be reported because last condition
63+
}
64+
65+
if ($date === null) {
66+
} elseif (self::isInt($date)) { // always-true should be reported, because another condition below
67+
} elseif (rand(0,1)) {
68+
}
69+
}
70+
71+
/**
72+
* @phpstan-assert-if-true int $value
73+
*/
74+
static public function isInt($value): bool {
75+
}
76+
}

0 commit comments

Comments
 (0)