Skip to content

Commit 23392bb

Browse files
committed
Added regression tests
1 parent 3adc625 commit 23392bb

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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<int, array{add: non-empty-array<int<0, max>, 1>}>', $changes);
12+
13+
foreach (toRem() as $del) {
14+
$changes[$add['id']]['del'][] = doSomething($del);
15+
}
16+
assertType('array<int, array{add: non-empty-array<int<0, max>, 1>, del?: non-empty-array<int<0, max>, 2>}>', $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+
}
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<non-empty-array{'6M'?: non-empty-list<string>, '3M'?: non-empty-list<string>}>", $arrDataByKey);
61+
foreach ($arrDataByKey as $key => $arrDataByKeyForKey) {
62+
assertType("array<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("array<non-empty-array{'6M'?: non-empty-list<string>, '3M'?: non-empty-list<string>}>", $arrDataByKeyForKey);
66+
}
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug2294;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
// Removing one of the entries makes the error go away
8+
9+
$entries = ['A' => null, 'B' => null];
10+
foreach($entries as $key => $value) {
11+
$entries[$key] = ['a' => 1, 'b' => 2];
12+
}
13+
assertType('array{A: array{a: 1, b: 2}|null, B: array{a: 1, b: 2}|null}', $entries);
14+
// Uncommenting the next line does NOT make the error go away
15+
//$entries['A'] = ['a' => 1, 'b' => 2];
16+
17+
// Removing one of these lines also makes the error go away
18+
$entries['A']['a'] += 1;
19+
$entries['A']['b'] += 1;
20+
assertType('array{A: array{a: 2, b: 3}, B: array{a: 1, b: 2}|null}', $entries);
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug8270;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function (): void {
8+
$list = [];
9+
10+
for ($i = 0; $i < 10; $i++) {
11+
$list[] = [
12+
'test' => false,
13+
'value' => rand(),
14+
];
15+
}
16+
assertType('list<array{test: false, value: int<0, max>}>', $list);
17+
18+
// TODO: sort list by value asc...
19+
$k = array_key_first($list);
20+
$list[$k]['test'] = true; // <--- assign only first item!
21+
22+
foreach ($list as $item) {
23+
if ($item['test']) {
24+
echo $item['value'];
25+
}
26+
}
27+
assertType('array{test: true, value?: int<0, max>}', $list);
28+
29+
};

0 commit comments

Comments
 (0)