Skip to content

Commit 7fc92ed

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 5a1048a + bebb16f commit 7fc92ed

File tree

12 files changed

+96
-18
lines changed

12 files changed

+96
-18
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ public function specifyTypesInCondition(
10771077
);
10781078
} else {
10791079
$varType = $scope->getType($var->var);
1080+
10801081
$narrowedKey = AllowedArrayKeysTypes::narrowOffsetKeyType($varType, $dimType);
10811082
if ($narrowedKey !== null) {
10821083
$types = $types->unionWith(
@@ -1088,6 +1089,17 @@ public function specifyTypesInCondition(
10881089
)->setRootExpr($expr),
10891090
);
10901091
}
1092+
1093+
if ($varType->isArray()->yes()) {
1094+
$types = $types->unionWith(
1095+
$this->create(
1096+
$var->var,
1097+
new NonEmptyArrayType(),
1098+
$context,
1099+
$scope,
1100+
)->setRootExpr($expr),
1101+
);
1102+
}
10911103
}
10921104
}
10931105

tests/PHPStan/Analyser/nsrt/bug-12274.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getItemsByModifiedIndex(array $items): array
4040
function testKeepListAfterIssetIndex(array $list, int $i): void
4141
{
4242
if (isset($list[$i])) {
43-
assertType('list<int>', $list);
43+
assertType('non-empty-list<int>', $list);
4444
$list[$i] = 21;
4545
assertType('non-empty-list<int>', $list);
4646
$list[$i+1] = 21;
@@ -53,8 +53,8 @@ function testKeepListAfterIssetIndex(array $list, int $i): void
5353
function testKeepNestedListAfterIssetIndex(array $nestedList, int $i, int $j): void
5454
{
5555
if (isset($nestedList[$i][$j])) {
56-
assertType('list<list<int>>', $nestedList);
57-
assertType('list<int>', $nestedList[$i]);
56+
assertType('non-empty-list<list<int>>', $nestedList);
57+
assertType('non-empty-list<int>', $nestedList[$i]);
5858
$nestedList[$i][$j] = 21;
5959
assertType('non-empty-list<list<int>>', $nestedList);
6060
assertType('list<int>', $nestedList[$i]);
@@ -66,7 +66,7 @@ function testKeepNestedListAfterIssetIndex(array $nestedList, int $i, int $j): v
6666
function testKeepListAfterIssetIndexPlusOne(array $list, int $i): void
6767
{
6868
if (isset($list[$i])) {
69-
assertType('list<int>', $list);
69+
assertType('non-empty-list<int>', $list);
7070
$list[$i+1] = 21;
7171
assertType('non-empty-list<int>', $list);
7272
}
@@ -77,7 +77,7 @@ function testKeepListAfterIssetIndexPlusOne(array $list, int $i): void
7777
function testKeepListAfterIssetIndexOnePlus(array $list, int $i): void
7878
{
7979
if (isset($list[$i])) {
80-
assertType('list<int>', $list);
80+
assertType('non-empty-list<int>', $list);
8181
$list[1+$i] = 21;
8282
assertType('non-empty-list<int>', $list);
8383
}
@@ -90,7 +90,7 @@ function testShouldLooseListbyAst(array $list, int $i): void
9090
if (isset($list[$i])) {
9191
$i++;
9292

93-
assertType('list<int>', $list);
93+
assertType('non-empty-list<int>', $list);
9494
$list[1+$i] = 21;
9595
assertType('non-empty-array<int<0, max>, int>', $list);
9696
}
@@ -101,7 +101,7 @@ function testShouldLooseListbyAst(array $list, int $i): void
101101
function testShouldLooseListbyAst2(array $list, int $i): void
102102
{
103103
if (isset($list[$i])) {
104-
assertType('list<int>', $list);
104+
assertType('non-empty-list<int>', $list);
105105
$list[2+$i] = 21;
106106
assertType('non-empty-array<int<0, max>, int>', $list);
107107
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug13674b;
4+
5+
use function assert;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
/**
11+
* @param array<int> $arrayA
12+
* @param list<int> $listA
13+
*/
14+
public function sayHello($arrayA, $listA, int $i): void
15+
{
16+
if (isset($arrayA[$i])) {
17+
assertType('non-empty-array<int>', $arrayA);
18+
} else {
19+
assertType('array<int>', $arrayA);
20+
}
21+
assertType('array<int>', $arrayA);
22+
23+
if (isset($listA[$i])) {
24+
assertType('non-empty-list<int>', $listA);
25+
} else {
26+
assertType('list<int>', $listA);
27+
}
28+
assertType('list<int>', $listA);
29+
30+
if (!isset($listA[$i])) {
31+
assertType('list<int>', $listA);
32+
return;
33+
}
34+
assertType('non-empty-list<int>', $listA);
35+
36+
$emptyArray = [];
37+
assertType('false', isset($emptyArray[$i]));
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bug13675;
4+
5+
use function assert;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
/**
11+
* @param list<int> $listA
12+
* @param list<int> $listB
13+
*/
14+
public function sayHello(int $i, $listA, $listB): void
15+
{
16+
if (!isset($listA[$i])) {
17+
return;
18+
}
19+
assertType('non-empty-list<int>', $listA);
20+
21+
if (count($listA) !== count($listB)) {
22+
return;
23+
}
24+
assertType('non-empty-list<int>', $listB);
25+
}
26+
}
27+

tests/PHPStan/Analyser/nsrt/bug-7000.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function doBar(): void
1212
$composer = array();
1313
foreach (array('require', 'require-dev') as $linkType) {
1414
if (isset($composer[$linkType])) {
15-
assertType('array{require?: array<string, string>, require-dev?: array<string, string>}', $composer);
15+
assertType('non-empty-array{require?: array<string, string>, require-dev?: array<string, string>}', $composer);
1616
foreach ($composer[$linkType] as $x) {}
1717
}
1818
}

tests/PHPStan/Analyser/nsrt/has-offset-type-bug.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function doFoo(array $errorMessages): void
2626
continue;
2727
}
2828

29-
assertType('array<string, int<1, max>>', $fileErrorsCounts);
29+
assertType('non-empty-array<string, int<1, max>>', $fileErrorsCounts);
3030
assertType('int<1, max>', $fileErrorsCounts[$errorMessage]);
3131

3232
$fileErrorsCounts[$errorMessage]++;

tests/PHPStan/Analyser/nsrt/shopware-connection-profiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getGroupedQueries(): void
2929
$connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'.
3030
}
3131

32-
assertType("array<string, array{sql: string, executionMS: 0, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: 0, index: int}|array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
32+
assertType("non-empty-array<string, array{sql: string, executionMS: 0, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: 0, index: int}|array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<1, max>, index: int}>", $connectionGroupedQueries);
3333
$connectionGroupedQueries[$key]['executionMS'] += $query['executionMS'];
3434
assertType("non-empty-array<string, array{sql: string, executionMS: float, types: array<int|string, int|Shopware\Core\Profiling\Doctrine\ParameterType>, count: int<0, max>, index: int}>", $connectionGroupedQueries);
3535
++$connectionGroupedQueries[$key]['count'];

tests/PHPStan/Analyser/nsrt/specified-types-closure-use.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ function ($arr) use ($key): void {
4848
public function doBuzz(array $arr, string $key): void
4949
{
5050
if (isset($arr[$key])) {
51-
assertType('array', $arr);
51+
assertType('non-empty-array', $arr);
5252
assertType("mixed~null", $arr[$key]);
5353
function () use ($arr, $key): void {
54-
assertType('array', $arr);
54+
assertType('non-empty-array', $arr);
5555
assertType("mixed~null", $arr[$key]);
5656
};
5757
}
@@ -60,10 +60,10 @@ function () use ($arr, $key): void {
6060
public function doBuzz(array $arr, string $key): void
6161
{
6262
if (isset($arr[$key])) {
63-
assertType('array', $arr);
63+
assertType('non-empty-array', $arr);
6464
assertType("mixed~null", $arr[$key]);
6565
function ($key) use ($arr): void {
66-
assertType('array', $arr);
66+
assertType('non-empty-array', $arr);
6767
assertType("mixed", $arr[$key]);
6868
};
6969
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public function testBug7000(): void
395395
{
396396
$this->analyse([__DIR__ . '/data/bug-7000.php'], [
397397
[
398-
"Offset 'require'|'require-dev' might not exist on array{require?: array<string, string>, require-dev?: array<string, string>}.",
398+
"Offset 'require'|'require-dev' might not exist on non-empty-array{require?: array<string, string>, require-dev?: array<string, string>}.",
399399
16,
400400
],
401401
]);

tests/PHPStan/Rules/Arrays/data/bug-11679.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function sayHello(int $index): bool
3434
assertType('non-empty-array<int, array{foo?: bool}>', $this->arr);
3535
assertType('true', $this->arr[$index]['foo']);
3636
}
37-
assertType('array<int, array{foo?: bool}>', $this->arr);
37+
assertType('non-empty-array<int, array{foo?: bool}>', $this->arr);
3838
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
3939
}
4040
}

0 commit comments

Comments
 (0)