Skip to content

Commit 27714f5

Browse files
committed
Generic/DisallowSpaceIndent: flag heredoc/nowdoc closer using tab indent
This is the same fix for the `Generic.WhiteSpace.DisallowSpaceIndent` sniff as was previously made in squizlabs/PHP_CodeSniffer 3640 and 533 for the sister-sniff `Generic.WhiteSpace.DisallowTabIndent`. --- Since PHP 7.3, heredoc/nowdoc closers may be indented. This indent can use either tabs or spaces and the indent is included in the `T_END_HEREDOC`/`T_END_NOWDOC` token contents as received from the PHP native tokenizer. However, these tokens were not included in the tokens to look at for the `Generic.WhiteSpace.DisallowSpaceIndent` sniff, which could lead to false negatives. Fixed now, includes tests. And along the same lines as per 533: * The error for space indentation of heredoc/nowdoc closers is not auto-fixable to prevent the fixer creating parse errors as the indentation of the _contents_ of the heredoc/nowdoc has to be the same as for the closer. * The error for space indentation of heredoc/nowdoc closers has its own error code to allow for selectively ignoring the indentation of heredoc/nowdoc closers.
1 parent 71326b4 commit 27714f5

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function process(File $phpcsFile, $stackPtr)
7878
T_INLINE_HTML => true,
7979
T_DOC_COMMENT_WHITESPACE => true,
8080
T_COMMENT => true,
81+
T_END_HEREDOC => true,
82+
T_END_NOWDOC => true,
8183
];
8284

8385
$eolLen = strlen($phpcsFile->eolChar);
@@ -202,8 +204,18 @@ public function process(File $phpcsFile, $stackPtr)
202204
}
203205
}//end if
204206

205-
$error = 'Tabs must be used to indent lines; spaces are not allowed';
206-
$fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed');
207+
$error = 'Tabs must be used to indent lines; spaces are not allowed';
208+
$errorCode = 'SpacesUsed';
209+
210+
// Report, but don't auto-fix space identation for a PHP 7.3+ flexible heredoc/nowdoc closer.
211+
// Auto-fixing this would cause parse errors as the indentation of the heredoc/nowdoc contents
212+
// needs to use the same type of indentation. Also see: https://3v4l.org/7OF3M .
213+
if ($tokens[$i]['code'] === T_END_HEREDOC || $tokens[$i]['code'] === T_END_NOWDOC) {
214+
$phpcsFile->addError($error, $i, $errorCode.'HeredocCloser');
215+
continue;
216+
}
217+
218+
$fix = $phpcsFile->addFixableError($error, $i, $errorCode);
207219
if ($fix === true) {
208220
$padding = str_repeat("\t", $expectedTabs);
209221
$padding .= str_repeat(' ', $expectedSpaces);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$heredoc = <<<"END"
4+
a
5+
b
6+
c
7+
END;
8+
9+
$nowdoc = <<<'END'
10+
a
11+
b
12+
c
13+
END;

src/Standards/Generic/Tests/WhiteSpace/DisallowSpaceIndentUnitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ public function getErrorList($testFile='')
102102
15 => 1,
103103
];
104104

105+
case 'DisallowSpaceIndentUnitTest.4.inc':
106+
if (PHP_VERSION_ID >= 70300) {
107+
return [
108+
7 => 1,
109+
13 => 1,
110+
];
111+
}
112+
113+
// PHP 7.2 or lower: PHP version which doesn't support flexible heredocs/nowdocs yet.
114+
return [];
115+
105116
case 'DisallowSpaceIndentUnitTest.js':
106117
return [3 => 1];
107118

0 commit comments

Comments
 (0)