Skip to content

Commit 27678e7

Browse files
authored
Merge pull request #534 from PHPCSStandards/feature/generic-disallowspaceindent-flag-heredocnowdoc-indent
Generic/DisallowSpaceIndent: flag heredoc/nowdoc closer using space indent
2 parents 1c4aa60 + 6d6cd7c commit 27678e7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

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

+14-2
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);
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

+11
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)