Skip to content

PHPStan: Enable numericOperandsInArithmeticOperators strict rule - #87057

Merged
westonruter merged 4 commits into
WordPress:trunkfrom
noruzzamans:add/phpstan-strict-rules-numeric-operands
Nov 19, 2026
Merged

westonruter merged 4 commits into
WordPress:trunkfrom
noruzzamans:add/phpstan-strict-rules-numeric-operands

Conversation

@noruzzamans

Copy link
Copy Markdown
Member

What?

Follow up to #86460.

Enables the numericOperandsInArithmeticOperators strict rule from phpstan-strict-rules by default in phpstan.neon.dist.

Why?

As evaluated and documented in #86460, Gutenberg's codebase already complies with numericOperandsInArithmeticOperators, reporting zero errors across all analyzed files. Enabling this rule ensures that arithmetic operations (+, -, *, /, %, **, ++, --) are performed strictly on numeric types, catching non-numeric operand regressions that level 5 lets through.

How?

  • Registers PHPStan\Rules\Operators\OperatorRuleHelper under services: with autowired: false. This avoids conflicting with vendor/phpstan/phpstan-strict-rules/rules.neon, which also registers an autowired OperatorRuleHelper, ensuring that local phpstan.neon configs including rules.neon load cleanly without failing with "Multiple services of type PHPStan\Rules\Operators\OperatorRuleHelper found".
  • Registers the ten arithmetic operator rules as services passed @operatorRuleHelper explicitly with the phpstan.rules.rule tag:
    • OperandInArithmeticPostDecrementRule
    • OperandInArithmeticPostIncrementRule
    • OperandInArithmeticPreDecrementRule
    • OperandInArithmeticPreIncrementRule
    • OperandsInArithmeticAdditionRule
    • OperandsInArithmeticDivisionRule
    • OperandsInArithmeticExponentiationRule
    • OperandsInArithmeticModuloRule
    • OperandsInArithmeticMultiplicationRule
    • OperandsInArithmeticSubtractionRule
  • Updates the local configuration example in docs/contributors/code/coding-guidelines.md to disable numericOperandsInArithmeticOperators: false to prevent duplicate error reporting when a local phpstan.neon includes rules.neon.

Testing Instructions

  1. Run composer phpstan (or npm run typecheck:php). Verify analysis finishes with zero errors.
  2. In a PHP file, introduce an invalid arithmetic operation such as 'invalid' + 5;. Run composer phpstan and verify plus.leftNonNumeric is reported.
  3. Test local configuration coexistence: create a temporary local phpstan.neon including vendor/phpstan/phpstan-strict-rules/rules.neon and verify PHPStan loads cleanly without multiple service definition errors.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@noruzzamans
noruzzamans marked this pull request as ready for review November 19, 2026 02:50
@noruzzamans noruzzamans self-assigned this Nov 19, 2026
The rules.neon of phpstan-strict-rules ties twelve rules to
strictRules.numericOperandsInArithmeticOperators, but only ten were
registered, leaving out OperandInArithmeticUnaryMinusRule and
OperandInArithmeticUnaryPlusRule, which rules.neon enables only with
bleedingEdge. Since the documented local phpstan.neon sets the parameter
to false to avoid running the registered rules twice, it also turned off
the rules.neon copies of these two, so with bleedingEdge enabled a
unary operand such as -$bool went unchecked.

The code complies with both rules, so register them unconditionally
alongside the others.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: dd6ba841-fda3-4a15-8474-62f4e9285131
@westonruter

westonruter commented Nov 19, 2026 •

Copy link
Copy Markdown
Member
  • In a PHP file, introduce an invalid arithmetic operation such as 'invalid' + 5;. Run composer phpstan and verify plus.leftNonNumeric is reported.

This gave me a binaryOp.invalid error.

This seems to be better code to reproduce this:

function repro( bool $b, array $a ) {
	$x = $b + 5;
	$z = -$b;
}

In trunk there is no error, but in this branch it produces:

Only numeric types are allowed in +, bool given on the left side.  
🪪  plus.leftNonNumeric                                            
at lib/class-wp-entity-fields-registry-gutenberg.php:9             

Only numeric types are allowed in unary -, bool given.             
🪪  unaryMinus.nonNumeric                                          
at lib/class-wp-entity-fields-registry-gutenberg.php:10            

westonruter and others added 2 commits November 18, 2026 19:48
It was 141 characters on one line, while the comment blocks around it
wrap at 120.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: dd6ba841-fda3-4a15-8474-62f4e9285131
PHPStan merges the identical errors that two copies of a rule report,
so including rules.neon without disabling the rules phpstan.neon.dist
already registers does not report each error twice. It does run each
of those rules twice, which is the reason to disable them.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: dd6ba841-fda3-4a15-8474-62f4e9285131
@westonruter westonruter added the [Type] Code Quality Issues or PRs that relate to code quality label Nov 19, 2026
@westonruter
westonruter merged commit 039fb2e into WordPress:trunk Nov 19, 2026
95 of 97 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Nov 19, 2026
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
…dPress#87057)

* PHPStan: Enable numericOperandsInArithmeticOperators strict rule

* Register the unary minus and plus operand rules too

The rules.neon of phpstan-strict-rules ties twelve rules to
strictRules.numericOperandsInArithmeticOperators, but only ten were
registered, leaving out OperandInArithmeticUnaryMinusRule and
OperandInArithmeticUnaryPlusRule, which rules.neon enables only with
bleedingEdge. Since the documented local phpstan.neon sets the parameter
to false to avoid running the registered rules twice, it also turned off
the rules.neon copies of these two, so with bleedingEdge enabled a
unary operand such as -$bool went unchecked.

The code complies with both rules, so register them unconditionally
alongside the others.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>

* Shorten the numeric operands comment under rules

It was 141 characters on one line, while the comment blocks around it
wrap at 120.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>

* Correct why the registered strict rules are disabled locally

PHPStan merges the identical errors that two copies of a rule report,
so including rules.neon without disabling the rules phpstan.neon.dist
already registers does not report each error twice. It does run each
of those rules twice, which is the reason to disable them.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>

---------

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants