Skip to content

Fix additional type safety issues in sniffs and traits#50

Merged
dereuromark merged 1 commit intomasterfrom
bugfix/type-safety-and-logic-fixes
Mar 27, 2026
Merged

Fix additional type safety issues in sniffs and traits#50
dereuromark merged 1 commit intomasterfrom
bugfix/type-safety-and-logic-fixes

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

Summary

Follow-up to PR #49, fixing additional type safety issues discovered during a second code review.

Changes

File Issue Fixed
DocBlockReturnVoidSniff.php !$endIndex check should be === false
DocBlockReturnNullSniff.php Missing false check on findNext() result before using in loop
ArrayDeclarationSniff.php Three !$nextTokenIndex checks should be === false
ImplicitCastSpacingSniff.php Missing false checks before arithmetic and array access
UseStatementsTrait.php Missing false check before using $semicolonIndex - 1
NamespaceTrait.php Missing false check before array access $tokens[$next]

Why This Matters

When findNext() / findPrevious() returns false (not found):

  • !$var treats both false and 0 as falsy - but 0 is a valid token index
  • $var - 1 on false becomes -1 (since false0 in arithmetic)
  • $tokens[$var] on false accesses $tokens[0] (the <?php token)

These bugs are theoretical (token 0 is always <?php), but fixing them improves code correctness.

- DocBlockReturnVoidSniff: Use === false check for findNext result
- DocBlockReturnNullSniff: Add missing false check before loop
- ArrayDeclarationSniff: Fix three !$var checks on findNext results
- ImplicitCastSpacingSniff: Add false checks before arithmetic operations
- UseStatementsTrait: Add false check before using semicolonIndex
- NamespaceTrait: Add false check before array access
@dereuromark dereuromark merged commit 7c887fb into master Mar 27, 2026
4 checks passed
@dereuromark dereuromark deleted the bugfix/type-safety-and-logic-fixes branch March 27, 2026 08:38
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.

1 participant