Skip to content

Generic.Strings.UnnecessaryHeredoc incorrectly converts tabs into spaces in heredoc #1320

Closed

Description

@anomiex

Describe the bug

If --tab-width is set, the fixer for Generic.Strings.UnnecessaryHeredoc incorrectly converts tabs into spaces in lines edited in the heredoc.

Code sample

Note this file must be indented using tabs, not spaces.

<?php

$var = <<<EOF
	Normal line
	Test \$escaped var
	Normal line
EOF;

Custom ruleset

N/A

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Run phpcbf -s --tab-width=8 --standard=Generic --sniffs=Generic.Strings.UnnecessaryHeredoc test.php
  3. Examine the file.

Expected behavior

File content is

<?php

$var = <<<'EOF'
	Normal line
	Test $escaped var
	Normal line
EOF;

Actual behavior

File content is

<?php

$var = <<<'EOF'
	Normal line
        Test $escaped var
	Normal line
EOF;

i.e. the line "Test $escaped var" is indented with 8 spaces rather than one tab.

Versions (please complete the following information)

Operating System Debian unstable
PHP version 8.4.11
PHP_CodeSniffer version 4.x-dev 4067971. Also reproduces with 3.13.5, 3.13.3, and 3.13.2.
Standard Generic
Install type Composer local

Additional context

Add any other context about the problem here.

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.

Activity

  1. added this to the 4.0.2 milestone on Nov 11, 2025
  2. self-assigned this
    on Nov 11, 2025
  3. jrfnl commented on Nov 11, 2025

    @jrfnl
    Member

    @anomiex Thanks for the clear and reproducible bug report.

    This bug could actually result in parse errors in a file if PHP 7.3+ flexible heredoc syntax is used (see: https://3v4l.org/vpD1u), so thanks for catching this!

    (It's the reason why I've held back a sniff which is specifically about heredoc/nowdoc indentation - I still need to finish that one at some point -, I just missed that it also came into play in this sniff.)

    PR #1321 should fix this. Testing appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions