Skip to content

Commit 7cb1163

Browse files
phpstan-botVincentLangletstaabm
authored
Fix phpstan/phpstan#13786: incorrect type-narrowing to *NEVER* inside array row (#5263)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent c009cb0 commit 7cb1163

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Analyser/ExprHandler/AssignHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
991991
$offsetType !== null
992992
&& $arrayDimFetch !== null
993993
&& $scope->hasExpressionType($arrayDimFetch)->yes()
994+
&& !$offsetValueType->hasOffsetValueType($offsetType)->no()
994995
) {
995996
$hasOffsetType = null;
996997
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {

tests/PHPStan/Analyser/nsrt/bug-13270b-php8.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function parseData(array $data): array
1919
if (!array_key_exists('priceWithVat', $data['price'])) {
2020
$data['price']['priceWithVat'] = null;
2121
}
22-
assertType("non-empty-array&hasOffset('priceWithVat')", $data['price']);
22+
assertType("non-empty-array&hasOffsetValue('priceWithVat', mixed)", $data['price']);
2323
if (!array_key_exists('priceWithoutVat', $data['price'])) {
2424
$data['price']['priceWithoutVat'] = null;
2525
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug13786;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @var array<mixed> $arr */
8+
9+
/** @var non-empty-list<'a'|'b'|'c'> $cols */
10+
11+
$total = [ ];
12+
foreach ($arr as $id => $dummy) {
13+
$total[$id] = [ ];
14+
foreach ($cols as $col) {
15+
$total[$id][$col] = '0';
16+
}
17+
assertType("non-empty-array<'a'|'b'|'c'|'d', '0'>", $total[$id]);
18+
$total[$id]['d'] = '0';
19+
assertType("non-empty-array<'a'|'b'|'c'|'d', '0'>&hasOffsetValue('d', '0')", $total[$id]);
20+
}
21+
22+
$total[$id]['e'] = '1';
23+
assertType("non-empty-array<'a'|'b'|'c'|'d'|'e', '0'|'1'>&hasOffsetValue('e', '1')", $total[$id]);

0 commit comments

Comments
 (0)