Skip to content

Commit c17a331

Browse files
committed
1 parent edc1e71 commit c17a331

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,11 @@ public function dataFileAsserts(): iterable
665665
yield from $this->gatherAssertTypes(__DIR__ . '/data/template-null-bound.php');
666666
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4592.php');
667667
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4903.php');
668+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2420.php');
669+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2718.php');
670+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3126.php');
671+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4586.php');
672+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4887.php');
668673
}
669674

670675
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Bug2420;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
const CONFIG = [
10+
0 => false,
11+
1 => false,
12+
];
13+
14+
public function sayHello(int $key): void
15+
{
16+
$config = self::CONFIG[$key] ?? true;
17+
assertType('bool', $config);
18+
}
19+
}
20+
21+
class HelloWorld2
22+
{
23+
const CONFIG = [
24+
0 => ['foo' => false],
25+
1 => ['foo' => false],
26+
];
27+
28+
public function sayHello(int $key): void
29+
{
30+
$config = self::CONFIG[$key]['foo'] ?? true;
31+
assertType('bool', $config);
32+
}
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Bug2218;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo()
11+
{
12+
$fun = function (): array {
13+
return [
14+
['group_id' => 'a', 'user_id' => 'id1'],
15+
['group_id' => 'a', 'user_id' => 'id2'],
16+
['group_id' => 'a', 'user_id' => 'id3'],
17+
['group_id' => 'b', 'user_id' => 'id4'],
18+
['group_id' => 'b', 'user_id' => 'id5'],
19+
['group_id' => 'b', 'user_id' => 'id6'],
20+
];
21+
};
22+
23+
$orders = $fun();
24+
25+
$result = [];
26+
foreach ($orders as $order) {
27+
assertType('bool', isset($result[$order['group_id']]['users']));
28+
if (isset($result[$order['group_id']]['users'])) {
29+
$result[$order['group_id']]['users'][] = $order['user_id'];
30+
continue;
31+
}
32+
33+
$result[$order['group_id']] = [
34+
'users' => [
35+
$order['user_id'],
36+
],
37+
];
38+
}
39+
}
40+
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Bug3126;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param array<int,string> $input
12+
*/
13+
function failure(array $input): void {
14+
$results = [];
15+
16+
foreach ($input as $keyOne => $layerOne) {
17+
assertType('bool', isset($results[$keyOne]['name']));
18+
if(isset($results[$keyOne]['name']) === false) {
19+
$results[$keyOne]['name'] = $layerOne;
20+
}
21+
}
22+
}
23+
24+
/**
25+
* @param array<int,string> $input
26+
*/
27+
function no_failure(array $input): void {
28+
$results = [];
29+
30+
foreach ($input as $keyOne => $layerOne) {
31+
if(isset($results[$keyOne]) === false) {
32+
$results[$keyOne] = $layerOne;
33+
}
34+
}
35+
}
36+
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Bug4586;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public function sayHello(): void
10+
{
11+
assertType('bool', isset($_SESSION));
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Bug4887;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
assertType('bool', isset($_SESSION));
8+
9+
$foo = ['$_REQUEST' => $_REQUEST,
10+
'$_COOKIE' => $_COOKIE,
11+
'$_SERVER' => $_SERVER,
12+
'$GLOBALS' => $GLOBALS,
13+
'$SESSION' => isset($_SESSION) ? $_SESSION : NULL];
14+
15+
foreach ($foo as $data)
16+
{
17+
assertType('bool', is_array($data));
18+
}

0 commit comments

Comments
 (0)