Skip to content

Commit df6de8d

Browse files
committed
Test MethodCall, StaticMethodCall
1 parent d30e95b commit df6de8d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ public function testBug12473(): void
275275
]);
276276
}
277277

278+
#[RequiresPhp('>= 8.1')]
279+
public function testBug12087b(): void
280+
{
281+
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
282+
283+
$this->treatPhpDocTypesAsCertain = true;
284+
$this->analyse([__DIR__ . '/data/bug-12087b.php'], [
285+
[
286+
'Call to method Bug12087b\MyAssert::is_null() with null will always evaluate to true.',
287+
37,
288+
$tipText,
289+
],
290+
]);
291+
}
292+
278293
public static function getAdditionalConfigFiles(): array
279294
{
280295
return [

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
77
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\RequiresPhp;
89

910
/**
1011
* @extends RuleTestCase<ImpossibleCheckTypeStaticMethodCallRule>
@@ -145,6 +146,21 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast
145146
$this->analyse([__DIR__ . '/data/impossible-static-method-report-always-true-last-condition.php'], $expectedErrors);
146147
}
147148

149+
#[RequiresPhp('>= 8.1')]
150+
public function testBug12087b(): void
151+
{
152+
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
153+
154+
$this->treatPhpDocTypesAsCertain = true;
155+
$this->analyse([__DIR__ . '/data/bug-12087b.php'], [
156+
[
157+
'Call to static method Bug12087b\MyAssert::static_is_null() with null will always evaluate to true.',
158+
31,
159+
$tipText,
160+
],
161+
]);
162+
}
163+
148164
public static function getAdditionalConfigFiles(): array
149165
{
150166
return [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug12087b;
4+
5+
enum Button: int
6+
{
7+
case On = 1;
8+
9+
case Off = 0;
10+
}
11+
12+
class MyAssert {
13+
/**
14+
* @return ($value is null ? true : false)
15+
*/
16+
static public function static_is_null(mixed $value): bool {
17+
return $value === null;
18+
}
19+
20+
/**
21+
* @return ($value is null ? true : false)
22+
*/
23+
public function is_null(mixed $value): bool {
24+
return $value === null;
25+
}
26+
}
27+
28+
function doFoo(): void {
29+
$value = 10;
30+
31+
MyAssert::static_is_null($value = Button::tryFrom($value));
32+
}
33+
34+
function doBar(MyAssert $assert): void {
35+
$value = 10;
36+
37+
$assert->is_null($value = Button::tryFrom($value));
38+
}

0 commit comments

Comments
 (0)