Skip to content

Commit cb959cb

Browse files
authored
Merge branch refs/heads/1.11.x into 1.12.x
2 parents e3a7aa7 + f7fca4a commit cb959cb

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

Diff for: src/Type/Php/IntdivThrowTypeExtension.php

+9-30
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
use PhpParser\Node\Expr\FuncCall;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\FunctionReflection;
10+
use PHPStan\Type\Constant\ConstantIntegerType;
1011
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
11-
use PHPStan\Type\NeverType;
1212
use PHPStan\Type\ObjectType;
1313
use PHPStan\Type\Type;
14-
use PHPStan\Type\TypeCombinator;
1514
use function count;
1615
use const PHP_INT_MIN;
1716

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

32-
$containsMin = false;
33-
$valueType = $scope->getType($funcCall->getArgs()[0]->value);
34-
foreach ($valueType->getConstantScalarTypes() as $constantScalarType) {
35-
if ($constantScalarType->getValue() === PHP_INT_MIN) {
36-
$containsMin = true;
37-
}
38-
39-
$valueType = TypeCombinator::remove($valueType, $constantScalarType);
40-
}
41-
42-
if (!$valueType instanceof NeverType) {
43-
$containsMin = true;
44-
}
31+
$valueType = $scope->getType($funcCall->getArgs()[0]->value)->toInteger();
32+
$containsMin = $valueType->isSuperTypeOf(new ConstantIntegerType(PHP_INT_MIN));
4533

46-
$divisionByZero = false;
47-
$divisorType = $scope->getType($funcCall->getArgs()[1]->value);
48-
foreach ($divisorType->getConstantScalarTypes() as $constantScalarType) {
49-
if ($containsMin && $constantScalarType->getValue() === -1) {
34+
$divisorType = $scope->getType($funcCall->getArgs()[1]->value)->toInteger();
35+
if (!$containsMin->no()) {
36+
$divisionByMinusOne = $divisorType->isSuperTypeOf(new ConstantIntegerType(-1));
37+
if (!$divisionByMinusOne->no()) {
5038
return new ObjectType(ArithmeticError::class);
5139
}
52-
53-
if ($constantScalarType->getValue() === 0) {
54-
$divisionByZero = true;
55-
}
56-
57-
$divisorType = TypeCombinator::remove($divisorType, $constantScalarType);
58-
}
59-
60-
if (!$divisorType instanceof NeverType) {
61-
return new ObjectType($containsMin ? ArithmeticError::class : DivisionByZeroError::class);
6240
}
6341

64-
if ($divisionByZero) {
42+
$divisionByZero = $divisorType->isSuperTypeOf(new ConstantIntegerType(0));
43+
if (!$divisionByZero->no()) {
6544
return new ObjectType(DivisionByZeroError::class);
6645
}
6746

Diff for: tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ public function testRule(): void
137137
'Dead catch - InvalidArgumentException is never thrown in the try block.',
138138
741,
139139
],
140+
[
141+
'Dead catch - ArithmeticError is never thrown in the try block.',
142+
762,
143+
],
140144
]);
141145
}
142146

Diff for: tests/PHPStan/Rules/Exceptions/data/unthrown-exception.php

+46
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,49 @@ public function passCallableToMethod2(): void
744744
}
745745

746746
}
747+
748+
class TestIntdivWithRange
749+
{
750+
/**
751+
* @param int $int
752+
* @param int<min, -1> $negativeInt
753+
* @param int<1, max> $positiveInt
754+
*/
755+
public function doFoo(int $int, int $negativeInt, int $positiveInt): void
756+
{
757+
try {
758+
intdiv($int, $positiveInt);
759+
intdiv($positiveInt, $negativeInt);
760+
intdiv($negativeInt, $positiveInt);
761+
intdiv($positiveInt, $positiveInt);
762+
} catch (\ArithmeticError $e) {
763+
764+
}
765+
try {
766+
intdiv($int, $negativeInt);
767+
} catch (\ArithmeticError $e) {
768+
769+
}
770+
try {
771+
intdiv($negativeInt, $negativeInt);
772+
} catch (\ArithmeticError $e) {
773+
774+
}
775+
try {
776+
intdiv($positiveInt, $int);
777+
} catch (\ArithmeticError $e) {
778+
779+
}
780+
try {
781+
intdiv($negativeInt, $int);
782+
} catch (\ArithmeticError $e) {
783+
784+
}
785+
try {
786+
intdiv($int, '-1,5');
787+
} catch (\ArithmeticError $e) {
788+
789+
}
790+
}
791+
792+
}

0 commit comments

Comments
 (0)