From 915835549e4018c67bd7914b320f71a8df574517 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Jul 2025 16:29:52 +0200 Subject: [PATCH] Fix `array_key_exists` narrows `$key` too much --- ...ayKeyExistsFunctionTypeSpecifyingExtension.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-11724.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-11724.php diff --git a/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php b/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php index af63cc047d..c9de826666 100644 --- a/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php +++ b/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php @@ -58,7 +58,7 @@ public function specifyTypes( } $key = $node->getArgs()[0]->value; $array = $node->getArgs()[1]->value; - $keyType = $scope->getType($key); + $keyType = $scope->getType($key)->toArrayKey(); $arrayType = $scope->getType($array); if ( diff --git a/tests/PHPStan/Analyser/nsrt/bug-11724.php b/tests/PHPStan/Analyser/nsrt/bug-11724.php new file mode 100644 index 0000000000..baf4c01658 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-11724.php @@ -0,0 +1,15 @@ + 'hello']; + assertType('true', array_key_exists($f, $a)); + if (array_key_exists($f, $a)) { + assertType('5.0', $f); + assertType("array{5: 'hello'}", $a); + } +}