|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brick\Math\PHPStan; |
| 6 | + |
| 7 | +use Brick\Math\BigDecimal; |
| 8 | +use Brick\Math\BigInteger; |
| 9 | +use Brick\Math\BigNumber; |
| 10 | +use Brick\Math\BigRational; |
| 11 | +use Brick\Math\Exception\IntegerOverflowException; |
| 12 | +use Brick\Math\Exception\RoundingNecessaryException; |
| 13 | +use PhpParser\Node\Expr\MethodCall; |
| 14 | +use PHPStan\Analyser\Scope; |
| 15 | +use PHPStan\Reflection\MethodReflection; |
| 16 | +use PHPStan\Type\DynamicMethodThrowTypeExtension; |
| 17 | +use PHPStan\Type\ObjectType; |
| 18 | +use PHPStan\Type\Type; |
| 19 | + |
| 20 | +/** |
| 21 | + * Narrows the throw type of toBigInteger(), toBigDecimal(), toBigRational(), and toInt(). |
| 22 | + * |
| 23 | + * When the caller is already the target type, toBigInteger/toBigDecimal/toBigRational are no-ops and cannot throw. |
| 24 | + * When toInt() is called on {@see BigInteger}, only {@see IntegerOverflowException} can be thrown |
| 25 | + * (no {@see RoundingNecessaryException}). |
| 26 | + */ |
| 27 | +final class BigNumberConversionThrowTypeExtension implements DynamicMethodThrowTypeExtension |
| 28 | +{ |
| 29 | + private const METHOD_TO_CLASS = [ |
| 30 | + 'toBigInteger' => BigInteger::class, |
| 31 | + 'toBigDecimal' => BigDecimal::class, |
| 32 | + 'toBigRational' => BigRational::class, |
| 33 | + ]; |
| 34 | + |
| 35 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 36 | + { |
| 37 | + $className = $methodReflection->getDeclaringClass()->getName(); |
| 38 | + $isBigNumber = $className === BigNumber::class |
| 39 | + || $methodReflection->getDeclaringClass()->isSubclassOf(BigNumber::class); |
| 40 | + |
| 41 | + if (! $isBigNumber) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + return isset(self::METHOD_TO_CLASS[$methodReflection->getName()]) |
| 46 | + || $methodReflection->getName() === 'toInt'; |
| 47 | + } |
| 48 | + |
| 49 | + public function getThrowTypeFromMethodCall( |
| 50 | + MethodReflection $methodReflection, |
| 51 | + MethodCall $methodCall, |
| 52 | + Scope $scope, |
| 53 | + ): ?Type { |
| 54 | + $callerType = $scope->getType($methodCall->var); |
| 55 | + $methodName = $methodReflection->getName(); |
| 56 | + |
| 57 | + if ($methodName === 'toInt') { |
| 58 | + // BigInteger::toInt() can only throw IntegerOverflowException (no RoundingNecessaryException). |
| 59 | + if ((new ObjectType(BigInteger::class))->isSuperTypeOf($callerType)->yes()) { |
| 60 | + return new ObjectType(IntegerOverflowException::class); |
| 61 | + } |
| 62 | + |
| 63 | + return $methodReflection->getThrowType(); |
| 64 | + } |
| 65 | + |
| 66 | + $targetClass = self::METHOD_TO_CLASS[$methodName]; |
| 67 | + |
| 68 | + if ((new ObjectType($targetClass))->isSuperTypeOf($callerType)->yes()) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + return $methodReflection->getThrowType(); |
| 73 | + } |
| 74 | +} |
0 commit comments