Skip to content

Commit ed9dfbd

Browse files
committed
fix
1 parent d1c5f09 commit ed9dfbd

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/Analyser/Generator/ExprHandler/UnaryMinusHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
use Generator;
66
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Scalar\Int_;
78
use PhpParser\Node\Stmt;
89
use PHPStan\Analyser\ExpressionContext;
910
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
1011
use PHPStan\Analyser\Generator\ExprAnalysisResult;
1112
use PHPStan\Analyser\Generator\ExprHandler;
1213
use PHPStan\Analyser\Generator\GeneratorScope;
14+
use PHPStan\Analyser\Generator\NoopNodeCallback;
1315
use PHPStan\Analyser\SpecifiedTypes;
1416
use PHPStan\DependencyInjection\AutowiredService;
1517
use PHPStan\Reflection\InitializerExprTypeResolver;
18+
use PHPStan\Type\IntegerRangeType;
19+
use function PHPStan\dumpType;
1620

1721
/**
1822
* @implements ExprHandler<Expr\UnaryMinus>
@@ -34,9 +38,17 @@ public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, Expre
3438
{
3539
$result = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);
3640

41+
$type = $this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->type);
42+
$nativeType = $this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->nativeType);
43+
if ($type instanceof IntegerRangeType) {
44+
$mulResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Mul($expr, new Int_(-1)), $scope, $context->enterDeep(), new NoopNodeCallback());
45+
$type = $mulResult->result;
46+
$nativeType = $mulResult->nativeType;
47+
}
48+
3749
return new ExprAnalysisResult(
38-
$this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->type),
39-
$this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->nativeType),
50+
$type,
51+
$nativeType,
4052
$result->scope,
4153
hasYield: $result->hasYield,
4254
isAlwaysTerminating: $result->isAlwaysTerminating,

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,12 @@ public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type
25242524
{
25252525
$type = $getTypeCallback($expr);
25262526

2527-
return $this->getUnaryMinusTypeFromType($expr, $type);
2527+
$type = $this->getUnaryMinusTypeFromType($expr, $type);
2528+
if ($type instanceof IntegerRangeType) {
2529+
return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new Int_(-1)));
2530+
}
2531+
2532+
return $type;
25282533
}
25292534

25302535
public function getUnaryMinusTypeFromType(Expr $expr, Type $type): Type
@@ -2550,10 +2555,6 @@ public function getUnaryMinusTypeFromType(Expr $expr, Type $type): Type
25502555
return TypeCombinator::union(...$newTypes);
25512556
}
25522557

2553-
if ($type instanceof IntegerRangeType) {
2554-
return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new Int_(-1)));
2555-
}
2556-
25572558
return $type;
25582559
}
25592560

0 commit comments

Comments
 (0)