Skip to content

Commit 4116b39

Browse files
staabmondrejmirtes
authored andcommitted
added regression test
1 parent 616ae15 commit 4116b39

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,4 +1034,10 @@ public function testBug11035(): void
10341034
]);
10351035
}
10361036

1037+
public function testBug9804(): void
1038+
{
1039+
$this->checkAlwaysTrueStrictComparison = true;
1040+
$this->analyse([__DIR__ . '/data/bug-9804.php'], []);
1041+
}
1042+
10371043
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug9804;
4+
5+
class Test
6+
{
7+
public function error(?string $someString): void
8+
{
9+
// Line below is the only difference to "pass" method
10+
if (!$someString) {
11+
return;
12+
}
13+
14+
// Strict comparison using === between int<min, -1>|int<1, max> and 0 will always evaluate to false.
15+
$firstLetterAsInt = (int)substr($someString, 0, 1);
16+
if ($firstLetterAsInt === 0) {
17+
return;
18+
}
19+
}
20+
21+
public function pass(?string $someString): void
22+
{
23+
// Line below is the only difference to "error" method
24+
if ($someString === null) {
25+
return;
26+
}
27+
28+
// All ok
29+
$firstLetterAsInt = (int)substr($someString, 0, 1);
30+
if ($firstLetterAsInt === 0) {
31+
return;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)