Skip to content

Fix OrChainIdenticalComparisonToInArrayRule#5881

Merged
staabm merged 1 commit into
phpstan:2.2.xfrom
staabm:or
Jun 15, 2026
Merged

Fix OrChainIdenticalComparisonToInArrayRule#5881
staabm merged 1 commit into
phpstan:2.2.xfrom
staabm:or

Conversation

@staabm

@staabm staabm commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

why is this change required?

The original line was self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left) — a self-contradictory X && !X that is always false. That first branch never executed, so the rule could only ever match the second branch (subject on the left, value on the right, e.g. $var === 'foo'). Yoda-style comparisons where the literal is on the left ('foo' === $var) fell through and returned null, so those chains were silently never reported or fixed. Changing the second check to $comparison->right makes the first branch correctly handle "value on the left".

Added a failing-without-the-fix test

  • tests/PHPStan/Build/data/or-chain-identical-comparison.php: two new yoda-style chains — 'foo' === $var || 'bar' === $var and a mixed 'foo' === $var || $var === 'bar'.
  • ...php.fixed: their expected in_array() rewrites.
  • OrChainIdenticalComparisonToInArrayRuleTest: added the two expected errors at lines 21 and 25.

Verification

  • With the fix: both testRule and testFix pass.
  • I temporarily reverted the source to the buggy ->left version and confirmed both tests fail (the line-21/25 errors are missing and the fixer leaves the yoda chains unchanged), then restored the fix.
  • make phpstan: green.

extracted from #5880

@staabm staabm requested a review from VincentLanglet June 15, 2026 15:00
@staabm staabm merged commit 240121d into phpstan:2.2.x Jun 15, 2026
387 of 389 checks passed
@staabm staabm deleted the or branch June 15, 2026 16:06
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