diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index b9ece1306..7da61d42a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -3354,6 +3354,12 @@ class AssertionFinder $source ); + $other_var_name = ExpressionIdentifier::getArrayVarId( + $conditional->right, + $this_class_name, + $source + ); + $other_type = $source->node_data->getType($conditional->left); $var_type = $source->node_data->getType($conditional->right); } elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) { @@ -3363,6 +3369,12 @@ class AssertionFinder $source ); + $other_var_name = ExpressionIdentifier::getArrayVarId( + $conditional->left, + $this_class_name, + $source + ); + $var_type = $source->node_data->getType($conditional->left); $other_type = $source->node_data->getType($conditional->right); } else { @@ -3383,6 +3395,14 @@ class AssertionFinder } else { $if_types[$var_name] = [['~' . $var_type->getAssertionString()]]; } + + if ($other_var_name && $other_type && count($other_type->getAtomicTypes()) === 1) { + if ($identical) { + $if_types[$other_var_name] = [['=' . $other_type->getAssertionString(true)]]; + } else { + $if_types[$other_var_name] = [['~' . $other_type->getAssertionString()]]; + } + } } if ($codebase diff --git a/tests/TypeReconciliation/ConditionalTest.php b/tests/TypeReconciliation/ConditionalTest.php index ae21e7fdb..82e1a54ab 100644 --- a/tests/TypeReconciliation/ConditionalTest.php +++ b/tests/TypeReconciliation/ConditionalTest.php @@ -2675,6 +2675,22 @@ class ConditionalTest extends \Psalm\Tests\TestCase '$_a===' => '"N"|"Y"', ] ], + 'assertionsWorksBothWays' => [ + ' [ + '$a===' => '2', + '$b===' => '2', + ] + ], 'nullErasureWithSmallerAndGreater' => [ '