Skip to content

Stacked case Statements with Comments in Between Incorrectly Flagged Indentation #1460

Description

@Ouroboros2460

Describe the bug

For multiple cases grouped together with the same contents, if you put comments in the middle of the different cases, the indentation gets flagged by Generic.WhiteSpace.ScopeIndent.Incorrect

Code sample

<?php

$tmp = 1;
switch ($tmp)
{
	case 1:
	// This should not get flagged
	case 2:
		echo 'Test';
		break;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
	<!--      Whitespace       -->
	<rule ref="Generic.WhiteSpace.DisallowSpaceIndent" />
	<rule ref="Generic.WhiteSpace.ScopeIndent">
		<properties>
			<property name="indent" value="4"/>
			<property name="tabIndent" value="true"/>
		</properties>
	</rule>
</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. See error message displayed
Line indented incorrectly; expected at least 2 tabs, found 1
(Generic.WhiteSpace.ScopeIndent.Incorrect)

Expected behavior

No error should be shown when there are empty contents for the case and a comment there.

Versions (please complete the following information)

Operating System macOS Tahoe 26.5.1
PHP version 8.5
PHP_CodeSniffer version 4.0.1
Standard custom
Install type Composer local

Additional context

N/A

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the 4.x branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions