Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
umherirrender opened this issue Apr 12, 2021 · 1 comment

Comments

@umherirrender
Copy link

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]

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Apr 16, 2021
@gsherwood gsherwood added this to the 3.6.1 milestone Apr 16, 2021
@gsherwood gsherwood changed the title PSR2.ControlStructures.SwitchDeclaration.TerminatingComment takes phpcs:ignore as content of case body PSR2.ControlStructures.SwitchDeclaration takes phpcs:ignore as content of case body Apr 16, 2021
gsherwood added a commit that referenced this issue Apr 16, 2021
gsherwood added a commit that referenced this issue Apr 16, 2021
@gsherwood
Copy link
Member

Thanks for reporting this. The sniff now ignored all comments, including the special PHPCS ones. The fix will be in 3.6.1.

PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

No branches or pull requests

2 participants