Skip to content

Commit 248723e

Browse files
ondrejmirtesclaude
andcommitted
Add regression tests for #1946, #11919 and #13959
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014LVEGd9G9w8j64EZQ7rysC
1 parent 003d601 commit 248723e

5 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13959;
4+
5+
use InvalidArgumentException;
6+
use function PHPStan\Testing\assertType;
7+
8+
interface GlobalId {}
9+
class GlobalTagId implements GlobalId {}
10+
11+
class HelloWorld
12+
{
13+
/**
14+
* @param list<GlobalId|string> $value
15+
*/
16+
public function sayHello(array $value): void
17+
{
18+
assertType('list<Bug13959\GlobalId|string>', $value);
19+
20+
foreach ($value as $item) {
21+
if ($item instanceof GlobalTagId) {
22+
continue;
23+
}
24+
25+
if (is_string($item)) {
26+
continue;
27+
}
28+
29+
throw new InvalidArgumentException('Invalid type');
30+
}
31+
32+
assertType('list<Bug13959\GlobalTagId|string>', $value);
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug1946;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$tag = null;
8+
foreach (["a", "b", "c"] as $tag) {
9+
if ($tag === "a") {
10+
$tag = null;
11+
break;
12+
} elseif ($tag === "b") {
13+
$tag = null;
14+
break;
15+
} else {
16+
$tag = null;
17+
break;
18+
}
19+
}
20+
21+
assertType('null', $tag);
22+
23+
echo strlen($tag);

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,4 +3178,20 @@ public function testBug11041(): void
31783178
$this->analyse([__DIR__ . '/data/bug-11041.php'], []);
31793179
}
31803180

3181+
public function testBug1946(): void
3182+
{
3183+
$this->analyse([__DIR__ . '/data/bug-1946.php'], [
3184+
[
3185+
'Parameter #1 $string of function strlen expects string, null given.',
3186+
19,
3187+
],
3188+
]);
3189+
}
3190+
3191+
#[RequiresPhp('>= 8.0.0')]
3192+
public function testBug11919(): void
3193+
{
3194+
$this->analyse([__DIR__ . '/data/bug-11919.php'], []);
3195+
}
3196+
31813197
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11919;
4+
5+
$handle = \fopen('some-file.csv', 'r');
6+
if ($handle === false)
7+
{
8+
exit(1);
9+
}
10+
$row = \fgetcsv( $handle );
11+
while ( $row !== false )
12+
{
13+
foreach ( ['a', 'b'] as $value )
14+
{
15+
// $row can never be false here
16+
if ( ! \in_array('some-key', $row) )
17+
{
18+
exit(1);
19+
}
20+
$row = \fgetcsv( $handle );
21+
// This shortcuts to the while loop - which eliminates false values
22+
continue 2;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bug1946;
4+
5+
$tag = null;
6+
foreach (["a", "b", "c"] as $tag) {
7+
if ($tag === "a") {
8+
$tag = null;
9+
break;
10+
} elseif ($tag === "b") {
11+
$tag = null;
12+
break;
13+
} else {
14+
$tag = null;
15+
break;
16+
}
17+
}
18+
19+
echo strlen($tag);

0 commit comments

Comments
 (0)