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
39 changes: 9 additions & 30 deletions src/Type/Php/IntdivThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use const PHP_INT_MIN;

Expand All @@ -29,39 +28,19 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
return $functionReflection->getThrowType();
}

$containsMin = false;
$valueType = $scope->getType($funcCall->getArgs()[0]->value);
foreach ($valueType->getConstantScalarTypes() as $constantScalarType) {
if ($constantScalarType->getValue() === PHP_INT_MIN) {
$containsMin = true;
}

$valueType = TypeCombinator::remove($valueType, $constantScalarType);
}

if (!$valueType instanceof NeverType) {
$containsMin = true;
}
$valueType = $scope->getType($funcCall->getArgs()[0]->value)->toInteger();
$containsMin = $valueType->isSuperTypeOf(new ConstantIntegerType(PHP_INT_MIN));

$divisionByZero = false;
$divisorType = $scope->getType($funcCall->getArgs()[1]->value);
foreach ($divisorType->getConstantScalarTypes() as $constantScalarType) {
if ($containsMin && $constantScalarType->getValue() === -1) {
$divisorType = $scope->getType($funcCall->getArgs()[1]->value)->toInteger();
if (!$containsMin->no()) {
$divisionByMinusOne = $divisorType->isSuperTypeOf(new ConstantIntegerType(-1));
if (!$divisionByMinusOne->no()) {
return new ObjectType(ArithmeticError::class);
}

if ($constantScalarType->getValue() === 0) {
$divisionByZero = true;
}

$divisorType = TypeCombinator::remove($divisorType, $constantScalarType);
}

if (!$divisorType instanceof NeverType) {
return new ObjectType($containsMin ? ArithmeticError::class : DivisionByZeroError::class);
}

if ($divisionByZero) {
$divisionByZero = $divisorType->isSuperTypeOf(new ConstantIntegerType(0));
if (!$divisionByZero->no()) {
return new ObjectType(DivisionByZeroError::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public function testRule(): void
'Dead catch - InvalidArgumentException is never thrown in the try block.',
741,
],
[
'Dead catch - ArithmeticError is never thrown in the try block.',
762,
],
]);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/unthrown-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,49 @@ public function passCallableToMethod2(): void
}

}

class TestIntdivWithRange
{
/**
* @param int $int
* @param int<min, -1> $negativeInt
* @param int<1, max> $positiveInt
*/
public function doFoo(int $int, int $negativeInt, int $positiveInt): void
{
try {
intdiv($int, $positiveInt);
intdiv($positiveInt, $negativeInt);
intdiv($negativeInt, $positiveInt);
intdiv($positiveInt, $positiveInt);
} catch (\ArithmeticError $e) {

}
try {
intdiv($int, $negativeInt);
} catch (\ArithmeticError $e) {

}
try {
intdiv($negativeInt, $negativeInt);
} catch (\ArithmeticError $e) {

}
try {
intdiv($positiveInt, $int);
} catch (\ArithmeticError $e) {

}
try {
intdiv($negativeInt, $int);
} catch (\ArithmeticError $e) {

}
try {
intdiv($int, '-1,5');
} catch (\ArithmeticError $e) {

}
}

}
Loading