Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Analyser/Generator/ExprHandler/UnaryMinusHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\Generator\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Type\IntegerRangeType;

/**
* @implements ExprHandler<Expr\UnaryMinus>
*/
#[AutowiredService]
final class UnaryMinusHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

public function supports(Expr $expr): bool
{
return $expr instanceof Expr\UnaryMinus;
}

public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, ExpressionContext $context, ?callable $alternativeNodeCallback): Generator
{
$result = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);

$type = $this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->type);
$nativeType = $this->initializerExprTypeResolver->getUnaryMinusTypeFromType($expr->expr, $result->nativeType);
if ($type instanceof IntegerRangeType) {
$mulResult = yield new ExprAnalysisRequest($stmt, new Expr\BinaryOp\Mul($expr, new Int_(-1)), $scope, $context->enterDeep(), new NoopNodeCallback());
$type = $mulResult->type;
$nativeType = $mulResult->nativeType;
}

return new ExprAnalysisResult(
$type,
$nativeType,
$result->scope,
hasYield: $result->hasYield,
isAlwaysTerminating: $result->isAlwaysTerminating,
throwPoints: $result->throwPoints,
impurePoints: $result->impurePoints,
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
18 changes: 13 additions & 5 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,19 @@ public function getClassConstFetchType(Name|Expr $class, string $constantName, ?
*/
public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type
{
$type = $getTypeCallback($expr)->toNumber();
$type = $getTypeCallback($expr);

$type = $this->getUnaryMinusTypeFromType($expr, $type);
if ($type instanceof IntegerRangeType) {
return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new Int_(-1)));
}

return $type;
}

public function getUnaryMinusTypeFromType(Expr $expr, Type $type): Type
{
$type = $type->toNumber();
$scalarValues = $type->getConstantScalarValues();

if (count($scalarValues) > 0) {
Expand All @@ -2543,10 +2555,6 @@ public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type
return TypeCombinator::union(...$newTypes);
}

if ($type instanceof IntegerRangeType) {
return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new Int_(-1)));
}

return $type;
}

Expand Down
17 changes: 16 additions & 1 deletion tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,28 @@ public function doConcat($a, $b, string $c, string $d): void
assertNativeType('string', $c . $d);
}

function doUnaryPlus(int $i) {
/**
* @param int $ii
*/
function doUnaryPlus(int $i, $ii)
{
$a = '1';

assertType('1', +$a);
assertNativeType('1', +$a);
assertType('int', +$i);
assertNativeType('int', +$i);
assertType('int', +$ii);
assertNativeType('float|int', +$ii);
}

function doUnaryMinus(int $i) {
$a = '1';

assertType('-1', -$a);
assertNativeType('-1', -$a);
assertType('int', -$i);
assertNativeType('int', -$i);
}

/**
Expand Down
Loading