From 9127ff1f2b2b84a5115251e4d4551921f5ab6902 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Jul 2024 01:42:23 +0200 Subject: [PATCH] Generic/DisallowTabIndent: do not auto-fix heredoc/nowdoc closer indent Follow up on squizlabs/PHP_CodeSniffer 3640, which added the `T_END_HEREDOC` and `T_END_NOWDOC` tokens to the list of tokens to examine and was included in the 3.7.2 release. When a flexible heredoc/nowdoc is used, the indentation type of the heredoc/nowdoc content and the indentation type of the heredoc/nowdoc closer must be consistent, so auto-fixing the indentation of the closer, without also changing the indentation of the contents (from tabs to spaces) would cause a parse error. See: https://3v4l.org/7OF3M I do believe indentation tabs should still be flagged, but the auto-fixer should be disabled. To allow people to disregard tab indentation for heredoc/nowdoc closers completely, the error now also has a more specific error code - ` TabsUsedHeredocCloser` -, which allows for excluding it in a ruleset. While the change in the error code _could_ be considered a breaking change, the fact that the potential for a parse error hasn't been reported as a bug so far, leads me to believe this is not a frequently encountered situation, so the impact of the higher specificity for the error code should be minimal. --- .../Sniffs/WhiteSpace/DisallowTabIndentSniff.php | 8 ++++++++ .../DisallowTabIndentUnitTest.3.inc.fixed | 13 ------------- 2 files changed, 8 insertions(+), 13 deletions(-) delete mode 100644 src/Standards/Generic/Tests/WhiteSpace/DisallowTabIndentUnitTest.3.inc.fixed diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php index e230916ca3..cc2b6f8987 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php @@ -169,6 +169,14 @@ public function process(File $phpcsFile, $stackPtr) continue; } + // Report, but don't auto-fix tab identation for a PHP 7.3+ flexible heredoc/nowdoc closer. + // Auto-fixing this would cause parse errors as the indentation of the heredoc/nowdoc contents + // needs to use the same type of indentation. Also see: https://3v4l.org/7OF3M . + if ($tokens[$i]['code'] === T_END_HEREDOC || $tokens[$i]['code'] === T_END_NOWDOC) { + $phpcsFile->addError($error, $i, $errorCode . 'HeredocCloser'); + continue; + } + $fix = $phpcsFile->addFixableError($error, $i, $errorCode); if ($fix === true) { if (isset($tokens[$i]['orig_content']) === true) { diff --git a/src/Standards/Generic/Tests/WhiteSpace/DisallowTabIndentUnitTest.3.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/DisallowTabIndentUnitTest.3.inc.fixed deleted file mode 100644 index 622088ad29..0000000000 --- a/src/Standards/Generic/Tests/WhiteSpace/DisallowTabIndentUnitTest.3.inc.fixed +++ /dev/null @@ -1,13 +0,0 @@ -