Skip to content

Commit 067ff00

Browse files
committed
Stop array_search() at the first surely matching key of an array whose key order is known
turbo-ext: the same in ConstantArrayType. Amp-Thread-ID: https://ampcode.com/threads/T-01a230f4-34d7-76c1-abde-c90173e47a85
1 parent 286fc16 commit 067ff00

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎src/Type/Constant/ConstantArrayType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,11 @@ public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Typ
21832183
}
21842184

21852185
$matches[] = $this->keyTypes[$index];
2186+
2187+
// the search stops at the first key that surely matches, unless the keys can come in any order
2188+
if ($hasIdenticalValue && $this->isKeyOrderKnown()) {
2189+
break;
2190+
}
21862191
}
21872192

21882193
// Unsealed extras can host additional entries beyond the explicit

‎tests/PHPStan/Analyser/nsrt/array-search.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function constantArrays(array $arr, string $string): void
5151

5252
/** @var array{1, '1', '2'} $arr */
5353
assertType('1', array_search('1', $arr, true));
54-
assertType('0|1', array_search('1', $arr));
54+
assertType('0', array_search('1', $arr));
5555
assertType('1|2|false', array_search($string, $arr, true));
5656
assertType('0|1|2|false', array_search($string, $arr, false));
5757
}

‎tests/PHPStan/Analyser/nsrt/array-shape-key-order.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ function literalArrays(): void
7575
assertType('true', $x === ['a' => 1, 'b' => 2]);
7676
}
7777

78+
/**
79+
* @param array{a: int, b: int} $p
80+
*/
81+
function searchFindsFirstMatchingKey(array $p, bool $b): void
82+
{
83+
// array_search() returns the first key whose value matches
84+
assertType("'a'", array_search(1, ['a' => 1, 'b' => 1]));
85+
assertType("'b'", array_search(1, ['a' => 2, 'b' => 1, 'c' => 1], true));
86+
assertType("'b'", array_search('1', ['a' => 2, 'b' => 1, 'c' => '1']));
87+
assertType("'c'", array_search('1', ['a' => 2, 'b' => 1, 'c' => '1'], true));
88+
89+
// a key that might be missing does not hide the keys after it
90+
$x = ['a' => 1, 'b' => 1];
91+
if ($b) {
92+
unset($x['a']);
93+
}
94+
assertType("'a'|'b'", array_search(1, $x));
95+
96+
// a PHPDoc shape does not say which of its keys comes first
97+
$p['a'] = 1;
98+
$p['b'] = 1;
99+
assertType("'a'|'b'", array_search(1, $p));
100+
}
101+
78102
/**
79103
* @param list<array{start: float, end: float}> $segments
80104
* @param array<string, mixed> $call

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function doFoo(string $needle, array $haystack): void {
1616

1717
$haystack[1] = 'foo';
1818
assertType('0|1|2|false', array_search($needle, $haystack, true));
19-
assertType('0|1|2', array_search('foo', $haystack, true));
19+
assertType('0|1', array_search('foo', $haystack, true));
2020

2121
assertType('0|1|2|false', array_search($needle, $haystack));
22-
assertType('0|1|2', array_search('foo', $haystack));
22+
assertType('0|1', array_search('foo', $haystack));
2323
}

‎turbo-ext/src/ConstantArrayType.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,6 +4040,13 @@ class ConstantArrayType
40404040
zval *keyType = arrayIndexObject(k, index, "keyTypes");
40414041
if (UNEXPECTED(keyType == NULL)) return zv::Val();
40424042
matches.push(zv::Ref(keyType));
4043+
4044+
/* the search stops at the first key that surely matches, unless the keys can come in any order */
4045+
if (hasIdenticalValue) {
4046+
bool orderKnown;
4047+
if (UNEXPECTED(!isKeyOrderKnown(orderKnown))) return zv::Val();
4048+
if (orderKnown) break;
4049+
}
40434050
}
40444051

40454052
/* Unsealed extras can host additional entries beyond the explicit

0 commit comments

Comments
 (0)