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
4 changes: 3 additions & 1 deletion src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,9 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
}

} else {
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
$unionValues = $i === 0
|| ($offsetType !== null && count($offsetType->getConstantScalarTypes()) === 0);
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $unionValues);
}

if ($arrayDimFetch === null || !$offsetValueType->isList()->yes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function loadConfiguration(): void

/**
* @param class-string $className
* @param array<lowercase-string, non-empty-list<TargetMethodParameter<AutowiredParameter>>> $constructorParameters
* @param array<lowercase-string, list<TargetMethodParameter<AutowiredParameter>>> $constructorParameters
*/
private function processConstructorParameters(string $className, ServiceDefinition $definition, array $constructorParameters): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/assign-nested-arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function doFoo(int $i)
$array[$i]['bar'] = 1;
$array[$i]['baz'] = 2;

assertType('non-empty-array<int, array{bar: 1, baz: 2}>', $array);
assertType('non-empty-array<int, array{bar: 1, baz?: 2}>', $array);
}

public function doBar(int $i, int $j)
Expand All @@ -27,7 +27,7 @@ public function doBar(int $i, int $j)
echo $array[$i][$j]['bar'];
echo $array[$i][$j]['baz'];

assertType('non-empty-array<int, non-empty-array<int, array{bar: 1, baz: 2}>>', $array);
assertType('non-empty-array<int, non-empty-array<int, array{bar: 1, baz?: 2}>>', $array);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-10438.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function extract(SimpleXMLElement $data, string $type = 'Meta'): array
$meta[$key] = [];
assertType('array{}', $meta[$key]);
foreach ($tag->{$valueName} as $value) {
assertType('list<string>', $meta[$key]);
assertType('list<string>|string', $meta[$key]);
$meta[$key][] = (string)$value;
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13857.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug13857;

use function PHPStan\Testing\assertType;

/**
* @param array<int, array{state: string}> $array
*/
function test(array $array, int $id): void {
$array[$id]['state'] = 'foo';
// only one element was set to 'foo', not all of them.
// correct type would be: non-empty-array<int, array{state: string}>
assertType('non-empty-array<int, array{state: string}>', $array);
}

/**
* @param array<string, array{name: string, age: int}> $people
*/
function test2(array $people, string $key): void {
$people[$key]['name'] = 'John';
// age becomes optional because $key might not exist yet, creating array{name: 'John'} without age
assertType('non-empty-array<string, array{name: string, age?: int}>', $people);
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/pr-4390.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ function (string $s): void {
}
}

assertType('non-empty-array<int<0, 9>, non-empty-array<int<0, 9>, string>>', $locations);
assertType('non-empty-array<int<0, 9>, string>', $locations[0]);
assertType('non-empty-array<int<0, 9>, list<string>>', $locations);
assertType('list<string>', $locations[0]);
};
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 @@ -31,7 +31,7 @@ public function sayHello(int $index): bool
assertType('array<int, array{foo?: bool}>', $this->arr);
if (!isset($this->arr[$index]['foo'])) {
$this->arr[$index]['foo'] = true;
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
assertType('non-empty-array<int, array{foo?: bool}>', $this->arr);
}
assertType('array<int, array{foo?: bool}>', $this->arr);
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function doFoo(array $percentageIntervals, array $changes): void
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: mixed, interval: mixed}', $intervalResults[$key]);
$intervalResults[$key]['itemsCount'] += $itemsCount;
assertType('non-empty-array<array{itemsCount: (array|float|int), interval: mixed}>', $intervalResults);
assertType('non-empty-array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
assertType('array{itemsCount: (array|float|int), interval: mixed}', $intervalResults[$key]);
} else {
assertType('array<array{itemsCount: mixed, interval: mixed}>', $intervalResults);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/data/bug-12927.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function sayFoo2(array $list): void
{
foreach($list as $k => $v) {
$list[$k]['abc'] = 'world';
assertType("non-empty-list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list);
assertType("non-empty-list<array<string, string>>", $list);
assertType("non-empty-array<string, string>&hasOffsetValue('abc', 'world')", $list[$k]);
}
assertType("list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list);
Expand Down
Loading