Skip to content

Commit

Permalink
Merge pull request #8494 from pvandommelen/fix_any_type_widening
Browse files Browse the repository at this point in the history
Fix type widening when `Any` assertion is used
  • Loading branch information
orklah committed Sep 18, 2022
2 parents d957ff2 + 2eb4ca0 commit 028ac7f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Expand Up @@ -293,6 +293,10 @@ private static function refine(

$old_var_type_string = $existing_var_type->getId();

if ($new_type_part instanceof TMixed) {
return $existing_var_type;
}

$new_type_has_interface = false;

if ($new_type_part->isObjectType()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Expand Up @@ -105,7 +105,7 @@ public static function reconcile(
int &$failed_reconciliation = Reconciler::RECONCILIATION_OK,
bool $inside_loop = false
): ?Union {
if ($assertion instanceof Any && $existing_var_type->hasMixed()) {
if ($assertion instanceof Any) {
return $existing_var_type;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/TypeReconciliation/IssetTest.php
Expand Up @@ -188,6 +188,24 @@ function foo(array $arr) : int {
return $arr[$b];
}',
],
'issetWithCalculatedKeyAndEqualComparison' => [
'code' => '<?php
/** @var array<string, string> $array */
$array = [];
function sameString(string $string): string {
return $string;
}
if (isset($array[sameString("key")]) === false) {
throw new \LogicException("No such key");
}
$value = $array[sameString("key")];
',
'assertions' => [
'$value' => 'string',
],
],
'issetArrayOffsetConditionalCreationWithInt' => [
'code' => '<?php
/** @param array<int, string> $arr */
Expand Down
2 changes: 2 additions & 0 deletions tests/TypeReconciliation/ReconcilerTest.php
Expand Up @@ -9,6 +9,7 @@
use Psalm\Internal\Type\AssertionReconciler;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Storage\Assertion;
use Psalm\Storage\Assertion\Any;
use Psalm\Storage\Assertion\Falsy;
use Psalm\Storage\Assertion\IsIdentical;
use Psalm\Storage\Assertion\IsLooselyEqual;
Expand Down Expand Up @@ -179,6 +180,7 @@ public function providerTestReconcilation(): array
'SimpleXMLElementNotAlwaysTruthy2' => ['SimpleXMLElement', new Falsy(), 'SimpleXMLElement'],
'SimpleXMLIteratorNotAlwaysTruthy' => ['SimpleXMLIterator', new Truthy(), 'SimpleXMLIterator'],
'SimpleXMLIteratorNotAlwaysTruthy2' => ['SimpleXMLIterator', new Falsy(), 'SimpleXMLIterator'],
'stringWithAny' => ['string', new Any(), 'string'],
];
}

Expand Down

0 comments on commit 028ac7f

Please sign in to comment.