Skip to content

Commit e1d6a39

Browse files
committed
fix setExistingOffsetValueType() for unset() use-case
1 parent 8874122 commit e1d6a39

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@
160160
use PHPStan\ShouldNotHappenException;
161161
use PHPStan\TrinaryLogic;
162162
use PHPStan\Type\Accessory\AccessoryArrayListType;
163+
use PHPStan\Type\Accessory\HasOffsetValueType;
163164
use PHPStan\Type\Accessory\NonEmptyArrayType;
164165
use PHPStan\Type\ArrayType;
165166
use PHPStan\Type\ClosureType;
166167
use PHPStan\Type\Constant\ConstantArrayType;
167168
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
168169
use PHPStan\Type\Constant\ConstantBooleanType;
169170
use PHPStan\Type\Constant\ConstantIntegerType;
171+
use PHPStan\Type\Constant\ConstantStringType;
170172
use PHPStan\Type\ErrorType;
171173
use PHPStan\Type\FileTypeMapper;
172174
use PHPStan\Type\GeneralizePrecision;
@@ -5931,7 +5933,25 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
59315933
&& $arrayDimFetch !== null
59325934
&& $scope->hasExpressionType($arrayDimFetch)->yes()
59335935
) {
5936+
$hasOffsetType = null;
5937+
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
5938+
$hasOffsetType = new HasOffsetValueType($offsetType, $valueToWrite);
5939+
}
59345940
$valueToWrite = $offsetValueType->setExistingOffsetValueType($offsetType, $valueToWrite);
5941+
5942+
if ($hasOffsetType !== null) {
5943+
$valueToWrite = TypeCombinator::intersect(
5944+
$valueToWrite,
5945+
$hasOffsetType,
5946+
new NonEmptyArrayType(),
5947+
);
5948+
} else {
5949+
$valueToWrite = TypeCombinator::intersect(
5950+
$valueToWrite,
5951+
new NonEmptyArrayType(),
5952+
);
5953+
}
5954+
59355955
} else {
59365956
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
59375957
}

src/Type/ArrayType.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,23 +356,11 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
356356

357357
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
358358
{
359-
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
360-
return TypeCombinator::intersect(
361-
new self(
362-
TypeCombinator::union($this->keyType, $offsetType),
363-
TypeCombinator::union($this->itemType, $valueType),
364-
),
365-
new HasOffsetValueType($offsetType, $valueType),
366-
new NonEmptyArrayType(),
367-
);
368-
}
369-
370359
return TypeCombinator::intersect(
371360
new self(
372361
$this->keyType,
373362
TypeCombinator::union($this->itemType, $valueType),
374363
),
375-
new NonEmptyArrayType(),
376364
);
377365
}
378366

0 commit comments

Comments
 (0)