1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #3061 - prevent array_key_exists from completely changing type

This commit is contained in:
Matthew Brown 2020-04-03 08:05:21 -04:00
parent af4a7cabe9
commit ecac9d56c0
2 changed files with 25 additions and 1 deletions

View File

@ -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];
}
}
}
}

View File

@ -1324,6 +1324,24 @@ class ArrayFunctionCallTest extends TestCase
return $elements;
}'
],
'arrayKeyExistsShoudldNotModifyIntType' => [
'<?php
class HttpError {
const ERRS = [
403 => "a",
404 => "b",
500 => "c"
];
}
function init(string $code) : string {
if (array_key_exists($code, HttpError::ERRS)) {
return $code;
}
return "";
}'
],
];
}