Skip to content

PSR2.ControlStructures.SwitchDeclaration takes phpcs:ignore as content of case body #3296

Closed
@umherirrender

Description

@umherirrender

Describe the bug
Using the sniff PSR2.ControlStructures.SwitchDeclaration brings in an edge case for the error code TerminatingComment when trying to ignore it for a case token, it generates an error for another case token

Code sample

<?php

$statusValue = mt_rand();

switch ( $statusValue ) {
	case 0:
	// phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
	case 2:
		$some = $code;

	case 3:
		$other = $code;
		break;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="PSR2.ControlStructures.SwitchDeclaration" />
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. With the existing phpcs:ignore its reported an error for the "case 0:" line (which is not expected from my point of view). Without the phpcs:ignore its reported an error for "case 2:" line (which is expected)

Workaround
Adding the phpcs:ignore behind "case 2:" works or another phpcs:ignore for "case 0:"

Expected behavior
The use of phpcs:ignore (or other phpcs comments) should not trigger the error code

Versions (please complete the following information):

  • OS: Windows 10
  • PHP: 8.0
  • PHPCS: 3.6.0
  • Standard: -

Additional context
From my understand of the code here, it just checks for T_COMMENT, which does not include phpcs:ignore comments like this, not sure if Tokens::$commentTokens is what you want here instead

[Off Topic: When first looking at the line number I was assuming the comment was required between the case before the reported line and the reported case, but that was completly wrong - but my switch/case was very long and that's why I missed the right location to add the missing comment on the first spot]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions