Skip to content

Generic/InlineControlStructure: fix unchecked findNext() return value#1387

Merged
jrfnl merged 1 commit into
PHPCSStandards:4.xfrom
rodrigoprimo:findnext-return-value
Mar 6, 2026
Merged

Generic/InlineControlStructure: fix unchecked findNext() return value#1387
jrfnl merged 1 commit into
PHPCSStandards:4.xfrom
rodrigoprimo:findnext-return-value

Conversation

@rodrigoprimo
Copy link
Copy Markdown
Contributor

Description

On line 67, findNext() searches for the first non-empty token after a T_ELSE to detect the else if pattern. When else is the last non-empty token in the file (live coding/parse error), findNext() returns false. This false value was used directly as an array index on line 68 ($tokens[$next]), which silently becomes $tokens[0], reading the T_OPEN_TAG token instead. The bug was silent because the code would eventually bail out at line 81-83.

This commit adds a $next === false check, which also allows the sniff to bail out earlier for an else at end of file instead of continuing to do unnecessary work before reaching the existing live coding bail-out further down.

Existing test file InlineControlStructureUnitTest.12.inc already covers this scenario.

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.

On line 67, `findNext()` searches for the first non-empty token after
a `T_ELSE` to detect the `else if` pattern. When `else` is the last
non-empty token in the file (live coding/parse error), `findNext()`
returns `false`. This `false` value was used directly as an array
index on line 68 (`$tokens[$next]`), which silently becomes
`$tokens[0]`, reading the `T_OPEN_TAG` token instead. The bug was silent
because the code would eventually bail out at line 81-83.

This commit adds a `$next === false` check, which also allows the
sniff to bail out earlier for an `else` at end of file instead of
continuing to do unnecessary work before reaching the existing live
coding bail-out further down.

Existing test file InlineControlStructureUnitTest.12.inc already
covers this scenario.
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.

Thanks @rodrigoprimo ! Makes perfect sense.

@jrfnl jrfnl merged commit 9fdbd6e into PHPCSStandards:4.x Mar 6, 2026
55 checks passed
@jrfnl jrfnl deleted the findnext-return-value branch March 6, 2026 15:05
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