Skip to content

PSR2/SwitchDeclaration: remove dead code, bow out on parse errors - #1489

Open
ntdiary wants to merge 1 commit into
PHPCSStandards:4.xfrom
ntdiary:issue-1472-psr2-error-message-followup
Open

PSR2/SwitchDeclaration: remove dead code, bow out on parse errors#1489
ntdiary wants to merge 1 commit into
PHPCSStandards:4.xfrom
ntdiary:issue-1472-psr2-error-message-followup

Conversation

@ntdiary

@ntdiary ntdiary commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Description

  • Bow out when a CASE/DEFAULT token is missing a scope opener or closer, avoiding undefined array key access.
  • Remove the unreachable WrongOpener . $type fallback error message. The tokenizer can initially set a CASE/DEFAULT scope opener to T_COLON, T_SEMICOLON, T_CLOSE_TAG. The only exception is a braced block after the colon (case x: { ... }), where processAdditional() later reassigns the opener to T_OPEN_CURLY_BRACKET.
  • Cache strtoupper($type) in $typeUc instead of calling it repeatedly.

Tests: rename the existing case file to SwitchDeclarationUnitTest.1.inc and add parse-error case files (.2.inc, .3.inc) verifying the sniff stays silent on broken switch statements.

Suggested changelog entry

  • PSR2.ControlStructures.SwitchDeclarationSniff: prevent Undefined array key warnings when the sniff encounters a CASE/DEFAULT statement without a scope opener or closer (parse error / live coding).

Related issues/external references

Fixes #1472

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.

- Bow out when a CASE/DEFAULT token is missing a scope opener or closer,
  avoiding undefined array key access.
- Remove the unreachable `WrongOpener . $type` fallback error message.
  The tokenizer can initially set a CASE/DEFAULT scope opener to `T_COLON`,
  `T_SEMICOLON`, `T_CLOSE_TAG`. The only exception is a braced block after
  the colon (`case x: { ... }`), where `processAdditional()` later
  reassigns the opener to `T_OPEN_CURLY_BRACKET`.
- Cache `strtoupper($type)` in `$typeUc` instead of calling it repeatedly.

Tests: rename the existing case file to SwitchDeclarationUnitTest.1.inc and
add parse-error case files (.2.inc, .3.inc) verifying the sniff stays
silent on broken switch statements.
@ntdiary
ntdiary force-pushed the issue-1472-psr2-error-message-followup branch from aee4292 to 69c4e4f Compare August 27, 2026 01:21
@ntdiary
ntdiary marked this pull request as ready for review August 27, 2026 01:35
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.

Follow-up improvements for error message

1 participant