1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

assert both sides of an equality

This commit is contained in:
orklah 2021-09-11 15:51:18 +02:00
parent 2e7e343ef8
commit 83ad836e88
2 changed files with 36 additions and 0 deletions

View File

@ -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

View File

@ -2675,6 +2675,22 @@ class ConditionalTest extends \Psalm\Tests\TestCase
'$_a===' => '"N"|"Y"',
]
],
'assertionsWorksBothWays' => [
'<?php
$a = 2;
$b = getPositiveInt();
assert($a === $b);
/** @return positive-int */
function getPositiveInt(): int{
return 2;
}',
'assertions' => [
'$a===' => '2',
'$b===' => '2',
]
],
'nullErasureWithSmallerAndGreater' => [
'<?php
function getIntOrNull(): ?int{return null;}