Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type widening when Any assertion is used #8494

Merged
merged 2 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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