Skip to content

Commit df98f0e

Browse files
authored
Added regression tests
1 parent 3adc625 commit df98f0e

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug10025;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class MyClass
8+
{
9+
public int $groupId;
10+
}
11+
12+
/**
13+
* @param list<MyClass> $foos
14+
* @param list<MyClass> $bars
15+
*/
16+
function x(array $foos, array $bars): void
17+
{
18+
$arr = [];
19+
foreach ($foos as $foo) {
20+
$arr[$foo->groupId]['foo'][] = $foo;
21+
}
22+
foreach ($bars as $bar) {
23+
$arr[$bar->groupId]['bar'][] = $bar;
24+
}
25+
26+
assertType('array<int, non-empty-array{foo?: non-empty-list<Bug10025\MyClass>, bar?: non-empty-list<Bug10025\MyClass>}>', $arr);
27+
foreach ($arr as $groupId => $group) {
28+
if (isset($group['foo'])) {
29+
}
30+
if (isset($group['bar'])) {
31+
}
32+
}
33+
34+
assertType('array<int, non-empty-array{foo?: non-empty-list<Bug10025\MyClass>, bar?: non-empty-list<Bug10025\MyClass>}>', $arr);
35+
}
36+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug10640;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$changes = [];
8+
foreach (toAdd() as $add) {
9+
$changes[$add['id']]['add'][] = doSomething($add);
10+
}
11+
assertType('array<array{add: non-empty-list}>', $changes);
12+
13+
foreach (toRem() as $del) {
14+
$changes[$add['id']]['del'][] = doSomething($del);
15+
}
16+
assertType('array<non-empty-array{add?: non-empty-list, del?: non-empty-list}>', $changes);
17+
18+
foreach ($changes as $changeSet) {
19+
if (isset($changeSet['del'])) {
20+
doDel($changeSet['del']);
21+
}
22+
if (isset($changeSet['add'])) {
23+
doAdd($changeSet['add']);
24+
}
25+
}
26+
27+
function doSomething($s) {}
28+
function toAdd($s) {}
29+
function toRem($s) {}
30+
function doDel($s) {}
31+
function doAdd($s) {}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12078;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @return array <string,string>
9+
*/
10+
function returnsData6M(): array
11+
{
12+
return ["A" => 'data A', "B" => 'Data B'];
13+
}
14+
15+
/**
16+
* @return array <string,string>
17+
*/
18+
function returnsData3M(): array
19+
{
20+
return ["A" => 'data A', "C" => 'Data C'];
21+
}
22+
23+
function main()
24+
{
25+
$arrDataByKey = [];
26+
27+
$arrData6M = returnsData6M();
28+
if ([] === $arrData6M) {
29+
echo "No data for 6M\n";
30+
} else {
31+
foreach ($arrData6M as $key => $data) {
32+
$arrDataByKey[$key]['6M'][] = $data;
33+
}
34+
}
35+
36+
$arrData3M = returnsData3M();
37+
if ([] === $arrData3M) {
38+
echo "No data for 3M\n";
39+
} else {
40+
foreach ($arrData3M as $key => $data) {
41+
$arrDataByKey[$key]['3M'][] = $data;
42+
}
43+
}
44+
/*
45+
So $arrDataByKey looks like
46+
[
47+
'A'=>[
48+
'6M'=>['data A'],
49+
'3M'=>['data A']
50+
],
51+
'B'=>[
52+
'6M'=>['data B']
53+
],
54+
'C'=>[
55+
'3M'=>['data C']
56+
]
57+
]
58+
*/
59+
60+
assertType("array<string, non-empty-array{'6M'?: non-empty-list<string>, '3M'?: non-empty-list<string>}>", $arrDataByKey);
61+
foreach ($arrDataByKey as $key => $arrDataByKeyForKey) {
62+
assertType("non-empty-array{'6M'?: non-empty-list<string>, '3M'?: non-empty-list<string>}", $arrDataByKeyForKey);
63+
echo [] === ($arrDataByKeyForKey['6M'] ?? []) ? 'No 6M data for key ' . $key . "\n" : 'We got 6M data for key ' . $key . "\n";
64+
echo [] === ($arrDataByKeyForKey['3M'] ?? []) ? 'No 3M data for key ' . $key . "\n" : 'We got 3M data for key ' . $key . "\n";
65+
assertType("non-empty-array{'6M'?: non-empty-list<string>, '3M'?: non-empty-list<string>}", $arrDataByKeyForKey);
66+
}
67+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug6173;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param int[] $ids1
11+
* @param int[] $ids2
12+
*/
13+
public function sayHello(array $ids1, array $ids2): bool
14+
{
15+
$res = [];
16+
foreach ($ids1 as $id) {
17+
$res[$id]['foo'] = $id;
18+
}
19+
20+
foreach ($ids2 as $id) {
21+
$res[$id]['bar'] = $id;
22+
}
23+
24+
assertType('array<int, non-empty-array{foo?: int, bar?: int}>', $res);
25+
foreach ($res as $id => $r) {
26+
assertType('non-empty-array{foo?: int, bar?: int}', $r);
27+
return isset($r['foo']);
28+
}
29+
30+
return false;
31+
}
32+
}

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,11 @@ public function testPr4374(): void
504504
]);
505505
}
506506

507+
public function testBug10640(): void
508+
{
509+
$this->treatPhpDocTypesAsCertain = true;
510+
511+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10640.php'], []);
512+
}
513+
507514
}

0 commit comments

Comments
 (0)