Skip to content
Merged
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
24 changes: 22 additions & 2 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,33 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
{
if ($this->itemType->isConstantArray()->yes() && $valueType->isConstantArray()->yes()) {
$newItemType = $this->itemType;
$newItemTypes = [];

foreach ($valueType->getConstantArrays() as $constArray) {
foreach ($constArray->getKeyTypes() as $keyType) {
$newItemType = $this->itemType;
$optionalKeyTypes = [];
foreach ($constArray->getKeyTypes() as $i => $keyType) {
$newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));

if (!$constArray->isOptionalKey($i)) {
continue;
}

$optionalKeyTypes[] = $keyType;
}
$newItemTypes[] = $newItemType;

if ($optionalKeyTypes === []) {
continue;
}

foreach ($optionalKeyTypes as $keyType) {
$newItemType = $newItemType->unsetOffset($keyType);
}
$newItemTypes[] = $newItemType;
}

$newItemType = TypeCombinator::union(...$newItemTypes);
if ($newItemType !== $this->itemType) {
return new self(
$this->keyType,
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public function testBug7581(): void
public function testBug7903(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-7903.php');
$this->assertCount(23, $errors);
$this->assertCount(24, $errors);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm ignoring this change. This test was added as a performance regression test (I'm not sure if/how that works, or why it makes assertions about the errors in the first place) and it's extremely hard to reason about the code. So I'm assuming that the reported errors don't matter.

Fundamentally, the array handling in PHPStan has a set of bugs and as a result of this PR it will have a slightly different (hopefully smaller) set of bugs. But there can definitely be cases where the new code causes a regression.

If you want to see the difference detected in the test for yourself here are the errors:

Previous:

115: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: 0, ssss: 0, ssss_next: 0}|array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: float}|array{oooo_rrrrr: float}.
125: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
130: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
135: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
169: Offset 'ccccc_rrrrr' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
170: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr?: float, ccccc_rrrrr: float}.
174: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr?: float, ccccc_rrrrr: float}.
178: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float, ccccc_rrrrr: float}.
182: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0|float, oooo_rrrrr: 0, oooo_yyyy: 0|float, ssss: 0, ssss_next: 0}|array{oooo_yyyy: float}.
184: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0|float, oooo_rrrrr: 0, oooo_yyyy: 0|float, ssss: 0, ssss_next: 0}|array{oooo_yyyy: float}.
194: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
195: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0|float, oooo_rrrrr: 0, oooo_yyyy: 0|float, ssss: 0, ssss_next: 0}|array{oooo_yyyy: float}.
198: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
199: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
203: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
207: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
208: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0|float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{oooo_rrrrr: float}.
212: Comparison operation ">" between 0 and 0 is always false.
213: Comparison operation ">" between 0 and 0 is always false.
214: Comparison operation ">" between 0 and 0 is always false.
215: Comparison operation ">" between 0 and 0 is always false.
229: Comparison operation ">" between 0 and 0 is always false.
230: Comparison operation ">" between 0 and 0 is always false.

New:

115: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: 0, ssss: 0, ssss_next: 0}|array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: float}|array{oooo_rrrrr: float}.
116: Offset 'ssss' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: float, ssss: 0, ssss_next: 0}|array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: float}|array{oooo_rrrrr: float, oooo_yyyy: float}.
117: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next: 0|float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss: float, ssss_next?: float}.
125: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy?: 0|float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss?: float, ssss_next?: float}.
126: Offset 'ssss' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy: float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss?: float, ssss_next?: float}.
127: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy?: 0|float, ssss: float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss: float, ssss_next?: float}.
130: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy?: 0|float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss?: float, ssss_next?: float}.
131: Offset 'ssss' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy: float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss?: float, ssss_next?: float}.
132: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy?: 0|float, ssss: float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss: float, ssss_next?: float}.
135: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: float, oooo_yyyy?: 0|float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss?: float, ssss_next?: float}.
136: Offset 'ssss' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy: float, ssss?: 0|float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy: float, ssss?: float, ssss_next?: float}.
137: Offset 'ssss_next' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0, oooo_rrrrr: 0|float, oooo_yyyy?: 0|float, ssss: float, ssss_next?: 0|float}|array{oooo_rrrrr: float, oooo_yyyy?: float, ssss: float, ssss_next?: float}.
170: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy?: 0|float, oooo_rrrrr?: 0|float, oooo_yyyy?: 0|float, ssss?: 0|float, ssss_next?: 0|float}.
172: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy?: float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{ccccc_rrrrr: float, ccccc_yyyy?: float}.
174: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy?: 0|float, oooo_rrrrr?: 0|float, oooo_yyyy?: 0|float, ssss?: 0|float, ssss_next?: 0|float}.
176: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{ccccc_rrrrr: float}.
178: Offset 'ccccc_yyyy' might not exist on array{ccccc_rrrrr: float, ccccc_yyyy: 0|float, oooo_rrrrr: 0|float, oooo_yyyy: 0|float, ssss: 0|float, ssss_next: 0|float}|array{ccccc_rrrrr: float}.
200: Offset 'oooo_yyyy' might not exist on array{ccccc_rrrrr: 0, ccccc_yyyy: 0|float, oooo_rrrrr: 0, oooo_yyyy?: 0|float, ssss: 0, ssss_next: 0}|non-empty-array{oooo_yyyy?: float, ccccc_yyyy?: float}.
212: Comparison operation ">" between 0 and 0 is always false.
213: Comparison operation ">" between 0 and 0 is always false.
214: Comparison operation ">" between 0 and 0 is always false.
215: Comparison operation ">" between 0 and 0 is always false.
229: Comparison operation ">" between 0 and 0 is always false.
230: Comparison operation ">" between 0 and 0 is always false.

}

public function testBug7901(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace OptionalArrayKeyInMultiDimArrayLoop;

/**
* @param array<int, array{1: int, 2: int, 3: float}> $rows
*/
function foo(array $rows): mixed
{
$itemMap = [];

foreach ($rows as $row) {
$x = $row[1];
$month = $row[2];

$itemMap[$x][$month]['foo'] ??= 5;
$itemMap[$x][$month]['bar'] ??= 5;

\PHPStan\Testing\assertType('array{foo: 5, bar: 5, amount?: 0.0}', $itemMap[$x][$month]);
$itemMap[$x][$month]['amount'] ??= 0.0;
}

return $itemMap;
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ public function testBug12553(): void
$this->analyse([__DIR__ . '/data/bug-12553.php'], []);
}

public function testBugMultiDimLoop(): void
{
$this->analyse([__DIR__ . '/data/bug-nullCoalesceMultiDimLoop.php'], []);
}

public function testIssetAfterRememberedConstructor(): void
{
$this->analyse([__DIR__ . '/data/isset-after-remembered-constructor.php'], [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace BugNullCoalesceMultiDimLoop;

/**
* @param array<int, array{1: int, 2: int, 3: float}> $rows
*/
function foo(array $rows): mixed
{
$itemMap = [];

foreach ($rows as $row) {
$x = $row[1];
$month = $row[2];

$itemMap[$x][$month]['foo'] ??= 5;
$itemMap[$x][$month]['bar'] ??= 5;

$itemMap[$x][$month]['amount'] ??= 0.0;
Copy link
Member

Choose a reason for hiding this comment

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

The same test in tests/PHPStan/Analyser/nsrt/ with some assertType calls would be nice. Make sure it fails first 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

return $itemMap;
}
Loading