Skip to content

Commit 11d3571

Browse files
committed
regression tests
1 parent 665d455 commit 11d3571

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10422;
4+
5+
use stdClass;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Foo
9+
{
10+
11+
public function something(): bool
12+
{
13+
return true;
14+
}
15+
16+
public function test(): void
17+
{
18+
}
19+
20+
}
21+
22+
function createOrNotObject(): ?Foo
23+
{
24+
return new Foo();
25+
}
26+
27+
function testSimple(): void
28+
{
29+
$test = createOrNotObject();
30+
31+
$error = '';
32+
33+
if (!$test) {
34+
$error = 'missing test';
35+
} else if ($test->something()) {
36+
$error = 'another';
37+
}
38+
if ($error) {
39+
die('Done');
40+
}
41+
assertType(Foo::class, $test);
42+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,11 @@ public function testBug10527(): void
18511851
$this->analyse([__DIR__ . '/data/bug-10527.php'], []);
18521852
}
18531853

1854+
public function testBug10055(): void
1855+
{
1856+
$this->analyse([__DIR__ . '/data/bug-10055.php'], []);
1857+
}
1858+
18541859
public function testBug10626(): void
18551860
{
18561861
$this->analyse([__DIR__ . '/data/bug-10626.php'], [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug10055;
6+
7+
function expectInt(int $param): void
8+
{
9+
}
10+
11+
function expectBool(bool $param): void
12+
{
13+
}
14+
15+
/**
16+
* @param 'value1'|'value2'|'value3' $param1
17+
* @param ($param1 is 'value3' ? bool : int) $param2
18+
*/
19+
function test(string $param1, int|bool $param2): void
20+
{
21+
match ($param1) {
22+
'value1' => expectInt($param2),
23+
'value2' => expectInt($param2),
24+
'value3' => expectBool($param2),
25+
};
26+
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,25 @@ public function testBug14227(): void
14761476
$this->analyse([__DIR__ . '/data/bug-14227.php'], []);
14771477
}
14781478

1479+
public function testBug12597(): void
1480+
{
1481+
$this->cliArgumentsVariablesRegistered = true;
1482+
$this->polluteScopeWithLoopInitialAssignments = false;
1483+
$this->checkMaybeUndefinedVariables = true;
1484+
$this->polluteScopeWithAlwaysIterableForeach = true;
1485+
$this->analyse([__DIR__ . '/data/bug-12597.php'], []);
1486+
}
1487+
1488+
#[RequiresPhp('>= 8.0')]
1489+
public function testBug12597NonFinite(): void
1490+
{
1491+
$this->cliArgumentsVariablesRegistered = true;
1492+
$this->polluteScopeWithLoopInitialAssignments = false;
1493+
$this->checkMaybeUndefinedVariables = true;
1494+
$this->polluteScopeWithAlwaysIterableForeach = true;
1495+
$this->analyse([__DIR__ . '/data/bug-12597-non-finite.php'], []);
1496+
}
1497+
14791498
public function testBug14117(): void
14801499
{
14811500
$this->cliArgumentsVariablesRegistered = true;
@@ -1499,4 +1518,14 @@ public function testBug14117(): void
14991518
]);
15001519
}
15011520

1521+
public function testBug11218(): void
1522+
{
1523+
$this->cliArgumentsVariablesRegistered = true;
1524+
$this->polluteScopeWithLoopInitialAssignments = false;
1525+
$this->checkMaybeUndefinedVariables = true;
1526+
$this->polluteScopeWithAlwaysIterableForeach = true;
1527+
1528+
$this->analyse([__DIR__ . '/data/bug-11218.php'], []);
1529+
}
1530+
15021531
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11218;
4+
5+
function doFoo() {
6+
$level = 'test';
7+
8+
for ($i = 0 ; $i <= 3 ; $i++) {
9+
if ($i === 0) {
10+
$test[$level] = 'this is a';
11+
} else {
12+
$test[$level] .= ' test';
13+
}
14+
}
15+
}
16+
17+
function doBar() {
18+
$level = 'test';
19+
20+
$test = [];
21+
22+
for ($i = 0 ; $i <= 3 ; $i++) {
23+
if ($i === 0) {
24+
$test[$level] = 'this is a';
25+
} else {
26+
$test[$level] .= ' test';
27+
}
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12597NonFinite;
4+
5+
class HelloWorld
6+
{
7+
public function test(mixed $type): void
8+
{
9+
if (is_int($type) || is_string($type)) {
10+
$message = 'Hello!';
11+
}
12+
13+
if (is_int($type)) {
14+
$this->message($message);
15+
}
16+
}
17+
18+
public function message(string $message): void {}
19+
}
20+
21+
class Foo {}
22+
class Bar {}
23+
24+
class HelloWorld2
25+
{
26+
public function test(mixed $type): void
27+
{
28+
if (is_int($type) || is_object($type)) {
29+
$message = 'Hello!';
30+
}
31+
32+
if (is_int($type)) {
33+
$this->message($message);
34+
}
35+
}
36+
37+
public function test2(mixed $type): void
38+
{
39+
if ($type instanceof Foo || $type instanceof Bar) {
40+
$message = 'Hello!';
41+
}
42+
43+
if ($type instanceof Foo) {
44+
$this->message($message);
45+
}
46+
}
47+
48+
public function message(string $message): void
49+
{
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12597;
4+
5+
class HelloWorld
6+
{
7+
private const TYPE_1 = 1;
8+
private const TYPE_2 = 2;
9+
10+
public function test(int $type): void
11+
{
12+
if (in_array($type, [self::TYPE_1, self::TYPE_2], true)) {
13+
$message = 'Hello!';
14+
}
15+
16+
if ($type === self::TYPE_1) {
17+
$this->message($message);
18+
}
19+
}
20+
21+
public function message(string $message): void {}
22+
}

0 commit comments

Comments
 (0)