Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,17 @@ public function specifyTypesInCondition(
)->setRootExpr($expr),
);
}

if ($scope->getNativeType($var->var)->isArray()->yes()) {
$types = $types->unionWith(
$this->create(
$var->var,
new NonEmptyArrayType(),
$context,
$scope,
)->setRootExpr($expr),
);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/bug-12274.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getItemsByModifiedIndex(array $items): array
function testKeepListAfterIssetIndex(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
assertType('non-empty-list<int>', $list);
$list[$i] = 21;
assertType('non-empty-list<int>', $list);
$list[$i+1] = 21;
Expand All @@ -53,7 +53,7 @@ function testKeepListAfterIssetIndex(array $list, int $i): void
function testKeepNestedListAfterIssetIndex(array $nestedList, int $i, int $j): void
{
if (isset($nestedList[$i][$j])) {
assertType('list<list<int>>', $nestedList);
assertType('non-empty-list<list<int>>', $nestedList);
assertType('list<int>', $nestedList[$i]);
$nestedList[$i][$j] = 21;
assertType('non-empty-list<list<int>>', $nestedList);
Expand All @@ -66,7 +66,7 @@ function testKeepNestedListAfterIssetIndex(array $nestedList, int $i, int $j): v
function testKeepListAfterIssetIndexPlusOne(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
assertType('non-empty-list<int>', $list);
$list[$i+1] = 21;
assertType('non-empty-list<int>', $list);
}
Expand All @@ -77,7 +77,7 @@ function testKeepListAfterIssetIndexPlusOne(array $list, int $i): void
function testKeepListAfterIssetIndexOnePlus(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
assertType('non-empty-list<int>', $list);
$list[1+$i] = 21;
assertType('non-empty-list<int>', $list);
}
Expand All @@ -90,7 +90,7 @@ function testShouldLooseListbyAst(array $list, int $i): void
if (isset($list[$i])) {
$i++;

assertType('list<int>', $list);
assertType('non-empty-list<int>', $list);
$list[1+$i] = 21;
assertType('non-empty-array<int<0, max>, int>', $list);
}
Expand All @@ -101,7 +101,7 @@ function testShouldLooseListbyAst(array $list, int $i): void
function testShouldLooseListbyAst2(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
assertType('non-empty-list<int>', $list);
$list[2+$i] = 21;
assertType('non-empty-array<int<0, max>, int>', $list);
}
Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13674.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace Bug13674;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param list<int> $listA
*/
public function sayHello(int $i, array $listA): void
{
if (isset($listA[$i])) {
assertType('non-empty-list<int>', $listA);

return;
}
}

/**
* @param list<int> $listB
*/
public function sayHello2(int $i, array $listB): void
{
if (!isset($listB[$i])) {
return;
}
assertType('non-empty-list<int>', $listB);
}

/**
* @param array<string, int> $arr
*/
public function sayHello3(string $key, array $arr): void
{
if (isset($arr[$key])) {
assertType('non-empty-array<string, int>', $arr);
}
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/has-offset-type-bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function doFoo(array $errorMessages): void
continue;
}

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

$fileErrorsCounts[$errorMessage]++;
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/specified-types-closure-use.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function ($arr) use ($key): void {
public function doBuzz(array $arr, string $key): void
{
if (isset($arr[$key])) {
assertType('array', $arr);
assertType('non-empty-array', $arr);
assertType("mixed~null", $arr[$key]);
function () use ($arr, $key): void {
assertType('array', $arr);
assertType('non-empty-array', $arr);
assertType("mixed~null", $arr[$key]);
};
}
Expand All @@ -60,10 +60,10 @@ function () use ($arr, $key): void {
public function doBuzz(array $arr, string $key): void
{
if (isset($arr[$key])) {
assertType('array', $arr);
assertType('non-empty-array', $arr);
assertType("mixed~null", $arr[$key]);
function ($key) use ($arr): void {
assertType('array', $arr);
assertType('non-empty-array', $arr);
assertType("mixed", $arr[$key]);
};
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Arrays/data/bug-11679.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function sayHello(int $index): bool
$this->arr[$index]['foo'] = true;
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
}
assertType('array<int, array{foo?: bool}>', $this->arr);
assertType('non-empty-array<int, array{foo?: bool}>', $this->arr);
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function testBug7190(): void
{
$this->analyse([__DIR__ . '/../Properties/data/bug-7190.php'], [
[
'Offset int on array<int, int> on left side of ?? always exists and is not nullable.',
'Offset int on non-empty-array<int, int> on left side of ?? always exists and is not nullable.',
20,
],
]);
Expand Down
Loading