1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Convert numeric literal strings to their int/float equivalents for numeric operations

This commit is contained in:
Matt Brown 2020-10-07 18:06:41 -04:00 committed by Daniil Gentili
parent f1be27b18a
commit 8d63dfec0a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -547,6 +547,22 @@ class NonDivArithmeticOpAnalyzer
return null;
}
if ($left_type_part instanceof Type\Atomic\TLiteralString) {
if (preg_match('/^\-?\d+$/', $left_type_part->value)) {
$left_type_part = new Type\Atomic\TLiteralInt((int) $left_type_part->value);
} elseif (preg_match('/^\-?\d?\.\d+$/', $left_type_part->value)) {
$left_type_part = new Type\Atomic\TLiteralFloat((float) $left_type_part->value);
}
}
if ($right_type_part instanceof Type\Atomic\TLiteralString) {
if (preg_match('/^\-?\d+$/', $right_type_part->value)) {
$right_type_part = new Type\Atomic\TLiteralInt((int) $right_type_part->value);
} elseif (preg_match('/^\-?\d?\.\d+$/', $right_type_part->value)) {
$right_type_part = new Type\Atomic\TLiteralFloat((float) $right_type_part->value);
}
}
if ($left_type_part->isNumericType() || $right_type_part->isNumericType()) {
if (($left_type_part instanceof TNumeric || $right_type_part instanceof TNumeric)
&& ($left_type_part->isNumericType() && $right_type_part->isNumericType())