feat: add types to avoid useless runtime checks#107
Closed
simPod wants to merge 1 commit intobrick:masterfrom
Closed
feat: add types to avoid useless runtime checks#107simPod wants to merge 1 commit intobrick:masterfrom
simPod wants to merge 1 commit intobrick:masterfrom
Conversation
55f142d to
055e0f7
Compare
Member
|
Hi, replacing runtime checks with type annotations is perfectly OK for internal calls, but unfortunately you can't expect all library consumers to use a static analysis tool. This library must ensure that you cannot trigger undefined behaviour, whether you're using static analysis or not. I'll pass on this one, sorry. |
Author
|
mkay what if we at least keep phpdoc type changes while preserving runtime checks? |
Member
|
Yes we can do both, but then we'll need to mute phpstan who will complain that the value is already checked. I'm not against this change, if you want to draft a PR, please target the |
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.
Replace runtime
InvalidArgumentExceptionchecks with stricter PHPDoc types (non-negative-int,int<0, 1000000>,int<2, 36>), letting PHPStan catch misuse at analysis time instead of at runtime.The project already uses advanced PHPStan types extensively (level 10,
numeric-string,int<1, max>,@phpstan-sealed,purity annotations), so these additions are consistent with the existing type-safety approach. Lib consumers are already expected to use correct types.Such runtime
@throwsthat can be replaced by types unnecessarily pollute my codebase.If I know based on types/literals that they're never thrown, I don't have to pretend I catch those.
BigDecimaldividedBy($scale)→non-negative-int|nullsqrt($scale)→non-negative-intpower($exponent)→int<0, 1000000>BigIntegerfromBase($base)→int<2, 36>toBase($base)→int<2, 36>randomBits($numBits)→non-negative-intpower($exponent)→int<0, 1000000>isBitSet($n)/testBit($n)→int<0, 1000000>getBitLength()→non-negative-intshiftedRight($distance)→int<-1000000, 1000000>shiftedLeft($distance)→int<-1000000, 1000000>BigNumber:toScale($scale)→non-negative-intBigRational:power($exponent)→int<0, 1000000>@throws InvalidArgumentExceptionlines