Skip to content

Add option to prohibit bool to string coercion - #4401

Closed
schlndh wants to merge 2 commits into
phpstan:2.3.xfrom
schlndh:feature-addOptionToProhibitBoolToStringCoercion
Closed

schlndh wants to merge 2 commits into
phpstan:2.3.xfrom
schlndh:feature-addOptionToProhibitBoolToStringCoercion

Conversation

@schlndh

@schlndh schlndh commented Oct 3, 2025 •

Copy link
Copy Markdown
Contributor

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 null to string (not necessarily in phpstan-strict-rules), but it probably doesn't make sense to prohibit coercing int to string). Note that the configuration is only meant to prohibit type coercions (i.e. implicit), not explicit type casts. This could then replace things like:

  • booleansInConditions and numericOperandsInArithmeticOperators from phpstan-strict-rules: the rules would be moved to phpstan and they'd be enabled by allowedTypeCoercions.XToY.
  • forbidNullInInterpolatedString and forbidNullInBinaryOperations from shipmonk/phpstan-rules: it would be handled by existing phpstan rules and enabled by allowedTypeCoercions.nullToY.
  • checkStrictPrintfPlaceholderTypes: it would be removed and instead it would be configured by allowedTypeCoercions.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 XToY in 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 different TypeCoercionRuleHelper to every rule, so it has a possible solution, but there would have to be a sane way to configure it (ultimately the RuleLevelHelper has the same limitation).

@schlndh
schlndh force-pushed the feature-addOptionToProhibitBoolToStringCoercion branch from 2b449dc to e454058 Compare October 3, 2025 12:13
@ondrejmirtes

Copy link
Copy Markdown
Member

I'd like this first phpstan/phpstan#13008 then it would be much easier to build rules for that

@ondrejmirtes
ondrejmirtes force-pushed the feature-addOptionToProhibitBoolToStringCoercion branch from e454058 to 2f3dce8 Compare October 31, 2026 13:27
@ondrejmirtes
ondrejmirtes changed the base branch from 2.1.x to 2.3.x October 31, 2026 13:27
@ondrejmirtes
ondrejmirtes force-pushed the feature-addOptionToProhibitBoolToStringCoercion branch from 2f3dce8 to 620237b Compare October 31, 2026 13:54
@ondrejmirtes

Copy link
Copy Markdown
Member

I feel like we don't need this PR since phpstan/phpstan#13008 has to be done first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants