Skip to content
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
12 changes: 12 additions & 0 deletions src/Analyser/TypeSpecifier.php
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(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm interessting.. for some reason this operation makes array loose their template type.

3) PHPStan\Rules\Methods\ReturnTypeRuleTest::testBug7511
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'
+'48: Method Bug7511\HelloWorld::computeForFrontByPosition() should return array<T of Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface> but returns array<Bug7511\PositionEntityInterface&Bug7511\TgEntityInterface>.
 '

/Users/m.staab/dvl/phpstan-src/src/Testing/RuleTestCase.php:179
/Users/m.staab/dvl/phpstan-src/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php:759

$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
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
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
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/IssetRuleTest.php
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
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