Skip to content

Generic/InlineControlStructure: remove unreachable if condition#1386

Merged
jrfnl merged 1 commit into
PHPCSStandards:4.xfrom
rodrigoprimo:inline-control-structure-remove-unreacheable-line
Mar 7, 2026
Merged

Generic/InlineControlStructure: remove unreachable if condition#1386
jrfnl merged 1 commit into
PHPCSStandards:4.xfrom
rodrigoprimo:inline-control-structure-remove-unreacheable-line

Conversation

@rodrigoprimo
Copy link
Copy Markdown
Contributor

Description

This PR is a follow-up to #1144 and removes an unreachable if condition.

Commit 29d0be3 changed the InlineControlStructure sniff to make it bail early for all control structures without a body (before the sniff did that only for while and for).

Before commit 29d0be3, $nextNonEmpty could be false as there were some scenarios where there would be no non-empty tokens after the $stackPtr or after the parenthesis_closer. This is the case for the else without a body in InlineControlStructureUnitTest.2.inc and the if without a body in InlineControlStructureUnitTest.5.inc.

After commit 29d0be3, $nextNonEmpty cannot be false anymore, as cases where there are no non-empty tokens are handled before this variable is set. This happens either in line 83 when $nextTokenIndex is set or line 98 when $nextTokenIndex is not set (a control structure that is not T_ELSE or T_DO and is missing the parenthesis closer).

Suggested changelog entry

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • I have opened a sister-PR in the documentation repository to update the Wiki.

Commit e1b9595 changed the
InlineControlStructure sniff to make it bail early for all control
structures without a body (before the sniff did that only for `while`
and `for`).

Before commit e1b9595, `$nextNonEmpty`
could be `false` as there were some scenarios where there would be no
non-empty tokens after the `$stackPtr` or after the `parenthesis_closer`.
This is the case for the `else` without a body in
InlineControlStructureUnitTest.2.inc and the `if` without a body in
InlineControlStructureUnitTest.5.inc.

After commit e1b9595, `$nextNonEmpty`
cannot be `false` anymore as cases where there are no non-empty tokens
are handled before this variable is set. This happens either in line 83
when `$nextTokenIndex` is set or line 98 when `$nextTokenIndex` is not
set (a control structure that is not `T_ELSE` or `T_DO` and is missing
the parenthesis closer).
Copy link
Copy Markdown
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks @rodrigoprimo !

@jrfnl jrfnl merged commit e79fdb7 into PHPCSStandards:4.x Mar 7, 2026
55 checks passed
@jrfnl jrfnl deleted the inline-control-structure-remove-unreacheable-line branch March 7, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants