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
13 changes: 12 additions & 1 deletion src/Analyser/Generator/ExprHandler/BinaryDivHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Analyser\Generator\ExprHandler;

use DivisionByZeroError;
use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
Expand All @@ -10,9 +11,12 @@
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\Generator\InternalThrowPoint;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ObjectType;
use function array_merge;

/**
Expand All @@ -36,13 +40,20 @@ public function analyseExpr(Stmt $stmt, Expr $expr, GeneratorScope $scope, Expre
$leftResult = yield new ExprAnalysisRequest($stmt, $expr->left, $scope, $context, $alternativeNodeCallback);
$rightResult = yield new ExprAnalysisRequest($stmt, $expr->right, $leftResult->scope, $context, $alternativeNodeCallback);

$throwPoints = array_merge($leftResult->throwPoints, $rightResult->throwPoints);
if (
!$rightResult->type->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no()
) {
$throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false);
}
Comment on lines +43 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addresses #4585 (comment)


return new ExprAnalysisResult(
$this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type),
$this->initializerExprTypeResolver->getDivTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType),
$rightResult->scope,
hasYield: $leftResult->hasYield || $rightResult->hasYield,
isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating,
throwPoints: array_merge($leftResult->throwPoints, $rightResult->throwPoints),
throwPoints: $throwPoints,
impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints),
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
Expand Down
64 changes: 64 additions & 0 deletions src/Analyser/Generator/ExprHandler/BinaryModHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use DivisionByZeroError;
use Generator;
use PhpParser\Node\Expr;
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\InternalThrowPoint;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ObjectType;
use function array_merge;

/**
* @implements ExprHandler<Expr\BinaryOp\Mod>
*/
#[AutowiredService]
final class BinaryModHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

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

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

$throwPoints = array_merge($leftResult->throwPoints, $rightResult->throwPoints);
if (
!$rightResult->type->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no()
) {
$throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false);
}

return new ExprAnalysisResult(
$this->initializerExprTypeResolver->getModTypeFromTypes($expr->left, $expr->right, $leftResult->type, $rightResult->type),
$this->initializerExprTypeResolver->getModTypeFromTypes($expr->left, $expr->right, $leftResult->nativeType, $rightResult->nativeType),
$rightResult->scope,
hasYield: $leftResult->hasYield || $rightResult->hasYield,
isAlwaysTerminating: $leftResult->isAlwaysTerminating || $rightResult->isAlwaysTerminating,
throwPoints: $throwPoints,
impurePoints: array_merge($leftResult->impurePoints, $rightResult->impurePoints),
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
5 changes: 5 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,11 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
$leftType = $getTypeCallback($left);
$rightType = $getTypeCallback($right);

return $this->getModTypeFromTypes($left, $right, $leftType, $rightType);
}

public function getModTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
{
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
return $this->getNeverType($leftType, $rightType);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace GeneratorNodeScopeResolverTest;

use Closure;
use DivisionByZeroError;
use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

Expand Down Expand Up @@ -50,6 +51,25 @@ public function doDiv($a, $b, int $c, int $d): void
assertNativeType('1', 1 / 1);
assertType('(float|int)', $c / $d);
assertNativeType('(float|int)', $c / $d);

assertType('*ERROR*', $c / 0); // DivisionByZeroError
}

/**
* @param int $a
* @param int $b
* @return void
*/
public function doMod($a, $b, int $c, int $d): void
{
assertType('int', $a % $b);
assertNativeType('int', $a % $b);
assertType('0', 1 % 1);
assertNativeType('0', 1 % 1);
assertType('int', $c % $d);
assertNativeType('int', $c % $d);

assertType('*ERROR*', $c % 0); // DivisionByZeroError
}

/**
Expand Down
Loading