From e7a86148b0453a65bccc010c548eb633c163baee Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 10 Oct 2021 09:24:25 +0200 Subject: [PATCH] Allow Psalm to store unresolved Unary Ops --- .../Reflector/ExpressionResolver.php | 28 +++++++++++++++++++ tests/ConstantTest.php | 21 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php index e85ddbe97..2ab1a5e7d 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php @@ -195,6 +195,34 @@ class ExpressionResolver return new UnresolvedConstant\ScalarValue($stmt->value); } + if ($stmt instanceof PhpParser\Node\Expr\UnaryPlus) { + $right = self::getUnresolvedClassConstExpr( + $stmt->expr, + $aliases, + $fq_classlike_name, + $parent_fq_class_name + ); + + return new UnresolvedConstant\UnresolvedAdditionOp( + new UnresolvedConstant\ScalarValue(0), + $right + ); + } + + if ($stmt instanceof PhpParser\Node\Expr\UnaryMinus) { + $right = self::getUnresolvedClassConstExpr( + $stmt->expr, + $aliases, + $fq_classlike_name, + $parent_fq_class_name + ); + + return new UnresolvedConstant\UnresolvedSubtractionOp( + new UnresolvedConstant\ScalarValue(0), + $right + ); + } + if ($stmt instanceof PhpParser\Node\Expr\Array_) { $items = []; diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php index d51f7361f..378f61337 100644 --- a/tests/ConstantTest.php +++ b/tests/ConstantTest.php @@ -1159,6 +1159,27 @@ class ConstantTest extends TestCase '$arr===' => 'array{5: "a", 6: "aa", zz: "z"}', ], ], + 'unresolvedConstWithUnaryMinus' => [ + ' -1, + K => 6, + ]; + + /** + * @param int $k + */ + public static function f(int $k): void { + $a = self::M; + print_r($a); + } + + }', + ], ]; }