diff --git a/src/Psalm/Internal/Type/AssertionReconciler.php b/src/Psalm/Internal/Type/AssertionReconciler.php index 4387bd020..161147fdb 100644 --- a/src/Psalm/Internal/Type/AssertionReconciler.php +++ b/src/Psalm/Internal/Type/AssertionReconciler.php @@ -1658,7 +1658,13 @@ class AssertionReconciler extends \Psalm\Type\Reconciler $const_type_atomic = $const_type_atomic->getGenericArrayType(); } - return clone $const_type_atomic->type_params[0]; + if (TypeAnalyzer::isContainedBy( + $codebase, + $const_type_atomic->type_params[0], + $existing_var_type + )) { + return clone $const_type_atomic->type_params[0]; + } } } } diff --git a/tests/ArrayFunctionCallTest.php b/tests/ArrayFunctionCallTest.php index 5838048e2..b78abcbb6 100644 --- a/tests/ArrayFunctionCallTest.php +++ b/tests/ArrayFunctionCallTest.php @@ -1324,6 +1324,24 @@ class ArrayFunctionCallTest extends TestCase return $elements; }' ], + 'arrayKeyExistsShoudldNotModifyIntType' => [ + ' "a", + 404 => "b", + 500 => "c" + ]; + } + + function init(string $code) : string { + if (array_key_exists($code, HttpError::ERRS)) { + return $code; + } + + return ""; + }' + ], ]; }