-
Notifications
You must be signed in to change notification settings - Fork 545
Add option to prohibit bool to string coercion #4401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
schlndh
wants to merge
2
commits into
phpstan:2.1.x
from
schlndh:feature-addOptionToProhibitBoolToStringCoercion
Draft
Add option to prohibit bool to string coercion #4401
schlndh
wants to merge
2
commits into
phpstan:2.1.x
from
schlndh:feature-addOptionToProhibitBoolToStringCoercion
Conversation
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
@schlndh
schlndh
force-pushed
the
feature-addOptionToProhibitBoolToStringCoercion
branch
from
October 3, 2025 12:13
2b449dc to
e454058
Compare
I'd like this first phpstan/phpstan#13008 then it would be much easier to build rules for that
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.
Uh oh!
There was an error while loading. Please reload this page.
I'd like to know your opinion on this approach before I go too far with it.
It's a follow up to this comment (specifically the part about disabling "{$bool}" etc in phpstan-strict-rules).
The idea is to eventually (separate PRs) add all pairs of types which make sense to prohibit (e.g. it makes sense to prohibit coercing
nulltostring(not necessarily in phpstan-strict-rules), but it probably doesn't make sense to prohibit coercinginttostring). Note that the configuration is only meant to prohibit type coercions (i.e. implicit), not explicit type casts. This could then replace things like:booleansInConditionsandnumericOperandsInArithmeticOperatorsfrom phpstan-strict-rules: the rules would be moved to phpstan and they'd be enabled byallowedTypeCoercions.XToY.forbidNullInInterpolatedStringandforbidNullInBinaryOperationsfrom shipmonk/phpstan-rules: it would be handled by existing phpstan rules and enabled byallowedTypeCoercions.nullToY.checkStrictPrintfPlaceholderTypes: it would be removed and instead it would be configured byallowedTypeCoercions.XToY. phpstan-strict-rules would set the flags to preserve the current default behavior.The benefit of this approach is that it would be configured in one place and applied consistently across the rules. The downside is that it would make it harder to enable
XToYin one rule, but prohibit it in another. Given the discussion in the strict printf parameter check PR, I guess it's safe to assume that there will be users with strong opinions on how it should behave for them. The DI container could pass a differentTypeCoercionRuleHelperto every rule, so it has a possible solution, but there would have to be a sane way to configure it (ultimately theRuleLevelHelperhas the same limitation).