Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

infer non-empty-list/array after isset($arr[$i]) #4441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
staabm wants to merge 7 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from staabm:narr
Draft
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

Some comments aren't visible on the classic Files Changed page.

12 changes: 12 additions & 0 deletions src/Analyser/TypeSpecifier.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,18 @@ public function specifyTypesInCondition(
);
} else {
$varType = $scope->getType($var->var);

if ($varType->isArray()->yes() && count($dimType->getConstantScalarTypes()) <= 1) {
$types = $types->unionWith(
$this->create(
$var->var,
new NonEmptyArrayType(),
$context,
$scope,
)->setRootExpr($expr),
);
}

$narrowedKey = AllowedArrayKeysTypes::narrowOffsetKeyType($varType, $dimType);
if ($narrowedKey !== null) {
$types = $types->unionWith(
Expand Down
14 changes: 7 additions & 7 deletions tests/PHPStan/Analyser/nsrt/bug-12274.php
View file Open in desktop
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,8 +53,8 @@ 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('list<int>', $nestedList[$i]);
assertType('non-empty-list<list<int>>', $nestedList);
assertType('non-empty-list<int>', $nestedList[$i]);
$nestedList[$i][$j] = 21;
assertType('non-empty-list<list<int>>', $nestedList);
assertType('list<int>', $nestedList[$i]);
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
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13674.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Bug13674a;

use function assert;
use function PHPStan\Testing\assertType;

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

if (isset($listA[$i])) {
assertType('non-empty-list<int>', $listA);
} else {
assertType('list<int>', $listA);
}
assertType('list<int>', $listA);

if (!isset($listA[$i])) {
assertType('list<int>', $listA);
return;
}
assertType('non-empty-list<int>', $listA);

$emptyArray = [];
assertType('false', isset($emptyArray[$i]));
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/has-offset-type-bug.php
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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/IssetRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public function testPr4374(): void

$this->analyse([__DIR__ . '/data/pr-4374.php'], [
[
'Offset string on array<PR4374\Foo> in isset() always exists and is not nullable.',
'Offset string on non-empty-array<PR4374\Foo> in isset() always exists and is not nullable.',
23,
],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,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

AltStyle によって変換されたページ (->オリジナル) /