1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #6635 from orklah/resolution_of_unary_ops

Resolution of unary ops
This commit is contained in:
orklah 2021-10-10 10:41:44 +02:00 committed by GitHub
commit 45ec0b36d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -195,6 +195,42 @@ class ExpressionResolver
return new UnresolvedConstant\ScalarValue($stmt->value); 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
);
if (!$right) {
return null;
}
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
);
if (!$right) {
return null;
}
return new UnresolvedConstant\UnresolvedSubtractionOp(
new UnresolvedConstant\ScalarValue(0),
$right
);
}
if ($stmt instanceof PhpParser\Node\Expr\Array_) { if ($stmt instanceof PhpParser\Node\Expr\Array_) {
$items = []; $items = [];

View File

@ -1159,6 +1159,27 @@ class ConstantTest extends TestCase
'$arr===' => 'array{5: "a", 6: "aa", zz: "z"}', '$arr===' => 'array{5: "a", 6: "aa", zz: "z"}',
], ],
], ],
'unresolvedConstWithUnaryMinus' => [
'<?php
const K = 5;
abstract class C6 {
public const M = [
1 => -1,
K => 6,
];
/**
* @param int $k
*/
public static function f(int $k): void {
$a = self::M;
print_r($a);
}
}',
],
]; ];
} }