feat: add PHPStan dynamic throw type extensions#112
Open
simPod wants to merge 1 commit intobrick:masterfrom
Open
feat: add PHPStan dynamic throw type extensions#112simPod wants to merge 1 commit intobrick:masterfrom
simPod wants to merge 1 commit intobrick:masterfrom
Conversation
Contributor
Author
|
This compensates for #107 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #112 +/- ##
=========================================
Coverage 99.08% 99.08%
Complexity 663 663
=========================================
Files 18 18
Lines 1643 1643
=========================================
Hits 1628 1628
Misses 15 15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
824a7af to
273a199
Compare
1651990 to
2718f1a
Compare
Add four PHPStan extensions that narrow throw types based on argument types at call sites. Consumers using `phpstan/extension-installer` get automatic discovery; otherwise include `phpstan/extension.neon`. `BigNumberOfThrowTypeExtension` (static factory methods): - `of()`, `ofNullable()` on all `BigNumber` classes → no throw when arg is `int` or already the target type - `of()`, `ofNullable()` with `int|numeric-string` args → removes `DivisionByZeroException` (no `/` in input) - `min()`, `max()`, `sum()` on `BigNumber` → no throw when all args are `int|BigNumber` - `gcdAll()`, `lcmAll()` on `BigInteger` → no throw when all args are `int|BigInteger` - `ofUnscaledValue()` on `BigDecimal` → no throw when arg is `int|BigInteger|BigDecimal` - `ofFraction()` on `BigRational` → narrows to `DivisionByZeroException` when both args are `int|BigInteger`; removes `DivisionByZeroException` when denominator is non-zero - `randomRange()` on `BigInteger` → narrows to `InvalidArgumentException`|`RandomSourceException` when both args are `BigInteger` `BigNumberConversionThrowTypeExtension` (type conversion methods): - `toBigInteger()` → no throw when caller is `BigInteger` - `toBigDecimal()` → no throw when caller is `BigDecimal` - `toBigRational()` → no throw when caller is `BigRational` - `toInt()` on `BigInteger` → narrows to `IntegerOverflowException` only (removes `RoundingNecessaryException`) `BigNumberOperationThrowTypeExtension` (arithmetic & comparison methods): - No-throw when arg is `int` or correct type: `plus`, `minus`, `multipliedBy`, `gcd`, `lcm`, `and`, `or`, `xor` (`BigInteger`); `plus`, `minus`, `multipliedBy` (`BigDecimal`/`BigRational`); `compareTo`, `isEqualTo`, `isLessThan`, `isLessThanOrEqualTo`, `isGreaterThan`, `isGreaterThanOrEqualTo` (`BigNumber`) - Residual exceptions when arg is `int` or correct type: `quotient`/`remainder`/`quotientAndRemainder` → `DivisionByZeroException`; `mod` → `DivisionByZeroException`|`InvalidArgumentException`; `modInverse` → +`NoInverseException`; `modPow` (checks both args); `dividedByExact` → `DivisionByZeroException`|`RoundingNecessaryException`; `dividedBy` (`BigRational`) → `DivisionByZeroException`; `clamp` (checks both min/max args) → `InvalidArgumentException` - `DivisionByZeroException` removed when divisor is guaranteed non-zero (e.g. literal int, positive-int range) `RoundingModeThrowTypeExtension` (rounding-mode-dependent methods): - `toScale()` on all `BigNumber` subclasses → independently narrows on both axes: non-negative scale removes `InvalidArgumentException`, non-`Unnecessary` rounding mode removes `RoundingNecessaryException` - `dividedBy()` on `BigInteger`/`BigDecimal` → independently narrows on three axes: `int`/correct-type divisor removes `MathException`, non-`Unnecessary` rounding mode removes `RoundingNecessaryException`, non-zero divisor removes `DivisionByZeroException` - `sqrt()` on `BigInteger`/`BigDecimal` → narrows to `NegativeNumberException`
2718f1a to
e64b3a5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add four PHPStan extensions that narrow throw types based on argument
types at call sites. Consumers using
phpstan/extension-installergetautomatic discovery; otherwise include
phpstan/extension.neon.BigNumberOfThrowTypeExtension(static factory methods):of(),ofNullable()on allBigNumberclasses → no throw when arg isintor already the target typeof(),ofNullable()withint|numeric-stringargs → removesDivisionByZeroException(no/in input)min(),max(),sum()onBigNumber→ no throw when all args areint|BigNumbergcdAll(),lcmAll()onBigInteger→ no throw when all args areint|BigIntegerofUnscaledValue()onBigDecimal→ no throw when arg isint|BigInteger|BigDecimalofFraction()onBigRational→ narrows toDivisionByZeroExceptionwhen both args areint|BigInteger; removesDivisionByZeroExceptionwhen denominator is non-zerorandomRange()onBigInteger→ narrows toInvalidArgumentException|RandomSourceExceptionwhen both args areBigIntegerBigNumberConversionThrowTypeExtension(type conversion methods):toBigInteger()→ no throw when caller isBigIntegertoBigDecimal()→ no throw when caller isBigDecimaltoBigRational()→ no throw when caller isBigRationaltoInt()onBigInteger→ narrows toIntegerOverflowExceptiononly (removesRoundingNecessaryException)BigNumberOperationThrowTypeExtension(arithmetic & comparison methods):intor correct type:plus,minus,multipliedBy,gcd,lcm,and,or,xor(BigInteger);plus,minus,multipliedBy(BigDecimal/BigRational);compareTo,isEqualTo,isLessThan,isLessThanOrEqualTo,isGreaterThan,isGreaterThanOrEqualTo(BigNumber)intor correct type:quotient/remainder/quotientAndRemainder→DivisionByZeroException;mod→DivisionByZeroException|InvalidArgumentException;modInverse→ +NoInverseException;modPow(checks both args);dividedByExact→DivisionByZeroException|RoundingNecessaryException;dividedBy(BigRational) →DivisionByZeroException;clamp(checks both min/max args) →InvalidArgumentExceptionDivisionByZeroExceptionremoved when divisor is guaranteed non-zero (e.g. literal int, positive-int range)RoundingModeThrowTypeExtension(rounding-mode-dependent methods):toScale()on allBigNumbersubclasses → independently narrows on both axes: non-negative scale removesInvalidArgumentException, non-Unnecessaryrounding mode removesRoundingNecessaryExceptiondividedBy()onBigInteger/BigDecimal→ independently narrows on three axes:int/correct-type divisor removesMathException, non-Unnecessaryrounding mode removesRoundingNecessaryException, non-zero divisor removesDivisionByZeroExceptionsqrt()onBigInteger/BigDecimal→ narrows toNegativeNumberException