diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php index 07f12204342..eb4f256f444 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php @@ -20,6 +20,8 @@ use Psalm\Type\Union; use RuntimeException; +use function is_int; + /** * @internal */ @@ -51,7 +53,9 @@ public static function analyze( continue; } if ($type_part instanceof TLiteralInt) { - $type_part = new TLiteralInt(-$type_part->value); + /** @var int|float $value */ + $value = -$type_part->value; + $type_part = is_int($value) ? new TLiteralInt($value) : new TLiteralFloat($value); } elseif ($type_part instanceof TLiteralFloat) { $type_part = new TLiteralFloat(-$type_part->value); } elseif ($type_part instanceof TIntRange) { diff --git a/tests/BinaryOperationTest.php b/tests/BinaryOperationTest.php index 8f9bbf2e5a5..3ac18147fd4 100644 --- a/tests/BinaryOperationTest.php +++ b/tests/BinaryOperationTest.php @@ -1013,6 +1013,15 @@ function toPositiveInt(int $i): int '$b===' => 'non-falsy-string', ], ], + 'unaryMinusOverflows' => [ + 'code' => <<<'PHP' + [ + '$a===' => 'float(9.2233720368548E+18)', + ], + ], ]; }