Skip to content

ScopeIndent autofixer can make unwanted changes when PHP open/close tags are excluded #1391

@Daimona

Description

@Daimona

Describe the bug

Given:

  • You have a ruleset with the Generic.WhiteSpace.ScopeIndent sniff enabled
  • The sniff is configured so that T_OPEN_TAG and T_CLOSE_TAG are in ignoreIndentationTokens
  • You have any other sniff enabled that provides autofixing
  • You have a file that contains PHP open/close tags (and whose indentation is perhaps incorrect, but that's beyond the point)
  • The ScopeIndent sniff is disabled for that file
  • The other sniff finds an autofixable issue on that file

Then, phpcs won't report anything from ScopeIndent, but phpcbf will still make unwanted indentation changes.

Code sample

<?php

// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact

use \stdClass;

class Page {
    public function flush() {
?><!DOCTYPE html>
<html dir="ltr" lang="en-US">
<?php
    if ( rand() ) {
        echo 'foo';
    }
?>
</html>
<?php
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
        <rule ref="PSR12.Files.ImportStatement" />
        <rule ref="Generic.WhiteSpace.ScopeIndent">
                <properties>
                        <property name="ignoreIndentationTokens" type="array">
                                <element value="T_OPEN_TAG" />
                                <element value="T_CLOSE_TAG" />
                        </property>
                </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 -s test.php
  3. Notice how ScopeIndent does not report anything, only the other sniff:
----------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------
 5 | ERROR | [x] Import statements must not begin with a leading backslash (PSR12.Files.ImportStatement.LeadingSlash)
----------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------------------
  1. Run phpcbf test.php
  2. Notice how it fixed not only the leading backslash, but also indentation:
5c5
< use \stdClass;
---
> use stdClass;
13c13
<         echo 'foo';
---
>     echo 'foo';

Expected behavior

The ScopeIndent autofixer should not make unwanted changes when no issues are reported.

Versions (please complete the following information)

Operating System Ubuntu 24.04
PHP version 8.5.2
PHP_CodeSniffer version 4.x (commit 8b1c267)
Standard custom
Install type composer local

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