Skip to content

Commit 54bd526

Browse files
committed
Set the empty array apart only beside non-empty shapes on both sides
A shape of only optional keys holds the empty array already: without the empty array, the other side no longer covered its sizes and the shapes fell back to a general array, such as array<'aware'|'name', ...> for $values['aware'][] = ...; $values['name'] = ... in a loop. And when one side holds the empty array only, the join never merged key by key, so setting it apart only changed which loops degrade to a general array. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c27-3464-768a-b452-8bf169dee364
1 parent 6988863 commit 54bd526

4 files changed

Lines changed: 60 additions & 11 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5288,16 +5288,31 @@ private function generalizeType(Type $a, Type $b, int $depth, ?Type $entryType =
52885288
$constantIntegers['b'] = [];
52895289
}
52905290

5291-
// the empty array stays apart, as in TypeCombinator::union(): merged
5292-
// key by key with the other shapes, it would make all their keys optional
5291+
// the empty array stays apart from the shapes both sides have, as in
5292+
// TypeCombinator::union(): merged key by key with them, it would make
5293+
// all their keys optional. A shape that may be empty, having only
5294+
// optional keys, holds the empty array already, and then all merge key
5295+
// by key.
52935296
$emptyArrays = [];
5294-
foreach (['a', 'b'] as $key) {
5295-
foreach ($constantArrays[$key] as $i => $type) {
5296-
if (!$type->isIterableAtLeastOnce()->no()) {
5297-
continue;
5297+
$splitEmptyArrays = true;
5298+
foreach ($constantArrays as $sideTypes) {
5299+
$hasNonEmptyShape = false;
5300+
foreach ($sideTypes as $type) {
5301+
$isNonEmpty = $type->isIterableAtLeastOnce();
5302+
$splitEmptyArrays = $splitEmptyArrays && !$isNonEmpty->maybe();
5303+
$hasNonEmptyShape = $hasNonEmptyShape || $isNonEmpty->yes();
5304+
}
5305+
$splitEmptyArrays = $splitEmptyArrays && $hasNonEmptyShape;
5306+
}
5307+
if ($splitEmptyArrays) {
5308+
foreach (['a', 'b'] as $key) {
5309+
foreach ($constantArrays[$key] as $i => $type) {
5310+
if (!$type->isIterableAtLeastOnce()->no()) {
5311+
continue;
5312+
}
5313+
$emptyArrays[] = $type;
5314+
unset($constantArrays[$key][$i]);
52985315
}
5299-
$emptyArrays[] = $type;
5300-
unset($constantArrays[$key][$i]);
53015316
}
53025317
}
53035318

‎tests/PHPStan/Analyser/nsrt/bug-15854.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ function build(array $entries): void
8080
echo count($values['items']), isset($values['perItem']) ? 'y' : 'n';
8181
}
8282

83+
84+
function parse(\DOMElement $element): void
85+
{
86+
$values = [];
87+
88+
foreach ($element->childNodes as $child) {
89+
if (!$child instanceof \DOMElement) {
90+
continue;
91+
}
92+
93+
if ($child->nodeName === 'aware') {
94+
$values['aware'][] = $child->nodeValue;
95+
96+
continue;
97+
}
98+
99+
if ($child->nodeName === 'name') {
100+
$values['name'] = $child->nodeValue;
101+
}
102+
}
103+
104+
assertType('array{name?: string|null, aware?: non-empty-list<string|null>}', $values);
105+
}

‎tests/PHPStan/Analyser/nsrt/weird-array_key_exists-issue.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function doFoo(array $data): array
3131
} else {
3232
$key = 'Ostatní';
3333
assertType('bool', array_key_exists($key, $otherData));
34-
assertType('array{}|array{Ostatní: array{abs: int, rel: (float|int)}}', $otherData);
34+
assertType('array<\'Ostatní\', array{abs: int, rel: (float|int)}>', $otherData);
3535
if (!array_key_exists($key, $otherData)) {
3636
$otherData[$key] = [
3737
'abs' => 0,

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9827,10 +9827,21 @@ class MutatingScope
98279827
constantIntegers.list[1] = zv::Arr::create(0);
98289828
}
98299829

9830-
/* the empty array stays apart, as in TypeCombinator::union(): merged
9831-
* key by key with the other shapes, it would make all their keys optional */
9830+
/* the empty array stays apart from the shapes both sides have (see the
9831+
* twin's comment) */
98329832
zv::Arr emptyArrays = zv::Arr::create(0);
9833+
bool splitEmptyArrays = true;
98339834
for (int side = 0; side < 2; side++) {
9835+
bool hasNonEmptyShape = false;
9836+
for (auto entry : zv::TableRef(constantArrays.table(side))) {
9837+
zend_long isIterableAtLeastOnce = typeOpTrinary(entry.value().deref().asObject(), PT_OP_IS_ITERABLE_AT_LEAST_ONCE, 0, NULL);
9838+
if (UNEXPECTED(isIterableAtLeastOnce < 0)) return zv::Val();
9839+
splitEmptyArrays = splitEmptyArrays && isIterableAtLeastOnce != PT_TRI_MAYBE;
9840+
hasNonEmptyShape = hasNonEmptyShape || isIterableAtLeastOnce == PT_TRI_YES;
9841+
}
9842+
splitEmptyArrays = splitEmptyArrays && hasNonEmptyShape;
9843+
}
9844+
for (int side = 0; splitEmptyArrays && side < 2; side++) {
98349845
zv::Arr kept = zv::Arr::create(constantArrays.count(side));
98359846
for (auto entry : zv::TableRef(constantArrays.table(side))) {
98369847
zv::Ref type = entry.value().deref();

0 commit comments

Comments
 (0)