Skip to content

Commit 937be01

Browse files
committed
Keep what other entries held when writing a shape into a general array's item shape
ArrayType::setExistingOffsetValueType() wrote the keys of a written constant array into the item shape, replacing what an existing key held. With a general key type only one entry is written, so the other entries still hold the old value: unsetting a nested optional key through one entry removed it from every entry. Existing keys now union the old and the new value; keys the item shape does not have are still added. Amp-Thread-ID: https://ampcode.com/threads/T-01a21312-8ea8-77bf-b8ff-3ff845484e0a
1 parent 7fc00a5 commit 937be01

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎src/Type/ArrayType.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ public function setExistingOffsetValueType(Type $offsetType, Type $valueType): T
494494
$newItemType = $this->itemType;
495495
$optionalKeyTypes = [];
496496
foreach ($constArray->getKeyTypes() as $i => $keyType) {
497-
$newItemType = $newItemType->setExistingOffsetValueType($keyType, $constArray->getOffsetValueType($keyType));
497+
$offsetValueType = $constArray->getOffsetValueType($keyType);
498+
if (!$newItemType->hasOffsetValueType($keyType)->no()) {
499+
// Only one entry of the general array is written, so the
500+
// other entries keep what the key held before.
501+
$offsetValueType = TypeCombinator::union($newItemType->getOffsetValueType($keyType), $offsetValueType);
502+
}
503+
$newItemType = $newItemType->setExistingOffsetValueType($keyType, $offsetValueType);
498504

499505
if (!$constArray->isOptionalKey($i)) {
500506
continue;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15825;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param array<string, array{properties: array{id: int, date_creation?: string}}> $schemas
9+
* @return array<string, array{properties: array{id: int, date_creation?: string}}>
10+
*/
11+
function removeDates(array $schemas): array
12+
{
13+
unset($schemas['GenericDeviceType']['properties']['date_creation']);
14+
assertType('array<string, array{properties: array{id: int, date_creation?: string}}>', $schemas);
15+
unset($schemas['SensorType']['properties']['date_creation']);
16+
17+
return $schemas;
18+
}

‎tests/PHPStan/Rules/Variables/UnsetRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,9 @@ public function testNullCoalesceAssignRightSideScope(): void
228228
$this->analyse([__DIR__ . '/data/null-coalesce-assign-right-side-scope.php'], []);
229229
}
230230

231+
public function testBug15825(): void
232+
{
233+
$this->analyse([__DIR__ . '/data/bug-15825.php'], []);
234+
}
235+
231236
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15825Rule;
4+
5+
/**
6+
* @param array<string, array{properties: array{id: int, date_creation?: string}}> $schemas
7+
* @return array<string, array{properties: array{id: int, date_creation?: string}}>
8+
*/
9+
function removeDates(array $schemas): array
10+
{
11+
unset($schemas['GenericDeviceType']['properties']['date_creation']);
12+
unset($schemas['SensorType']['properties']['date_creation']);
13+
14+
return $schemas;
15+
}

‎turbo-ext/src/ArrayType.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,16 @@ class ArrayType
790790
}
791791
zv::Val offsetValue = callType(Z_OBJ_P(constArray), PT_LC("getoffsetvaluetype"), 1, keyType);
792792
if (UNEXPECTED(offsetValue.isUndef())) return zv::Val();
793+
/* only one entry of the general array is written, so
794+
* the other entries keep what the key held before */
795+
zend_long hasOffset = pt_type_op_trinary(Z_OBJ_P(newItemType.raw()), PT_OP_HAS_OFFSET_VALUE_TYPE, 1, keyType);
796+
if (UNEXPECTED(hasOffset < 0)) return zv::Val();
797+
if (hasOffset != PT_TRI_NO) {
798+
zv::Val heldValue = pt_type_op(Z_OBJ_P(newItemType.raw()), PT_OP_GET_OFFSET_VALUE_TYPE, 1, keyType);
799+
if (UNEXPECTED(heldValue.isUndef())) return zv::Val();
800+
offsetValue = combinator2(PT_LC("union"), heldValue.raw(), offsetValue.raw());
801+
if (UNEXPECTED(offsetValue.isUndef())) return zv::Val();
802+
}
793803
zv::Args args{keyType, offsetValue.raw()};
794804
newItemType = callType(Z_OBJ_P(newItemType.raw()), PT_LC("setexistingoffsetvaluetype"), 2, args);
795805
if (UNEXPECTED(newItemType.isUndef())) return zv::Val();

0 commit comments

Comments
 (0)