Skip to content

Commit

Permalink
Merge pull request #534 from PHPCSStandards/feature/generic-disallows…
Browse files Browse the repository at this point in the history
…paceindent-flag-heredocnowdoc-indent

Generic/DisallowSpaceIndent: flag heredoc/nowdoc closer using space indent
  • Loading branch information
jrfnl authored Jul 9, 2024
2 parents 1c4aa60 + 6d6cd7c commit 27678e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function process(File $phpcsFile, $stackPtr)
T_INLINE_HTML => true,
T_DOC_COMMENT_WHITESPACE => true,
T_COMMENT => true,
T_END_HEREDOC => true,
T_END_NOWDOC => true,
];

$eolLen = strlen($phpcsFile->eolChar);
Expand Down Expand Up @@ -202,8 +204,18 @@ public function process(File $phpcsFile, $stackPtr)
}
}//end if

$error = 'Tabs must be used to indent lines; spaces are not allowed';
$fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed');
$error = 'Tabs must be used to indent lines; spaces are not allowed';
$errorCode = 'SpacesUsed';

// Report, but don't auto-fix space 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) {
$padding = str_repeat("\t", $expectedTabs);
$padding .= str_repeat(' ', $expectedSpaces);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$heredoc = <<<"END"
a
b
c
END;

$nowdoc = <<<'END'
a
b
c
END;
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ public function getErrorList($testFile='')
15 => 1,
];

case 'DisallowSpaceIndentUnitTest.4.inc':
if (PHP_VERSION_ID >= 70300) {
return [
7 => 1,
13 => 1,
];
}

// PHP 7.2 or lower: PHP version which doesn't support flexible heredocs/nowdocs yet.
return [];

case 'DisallowSpaceIndentUnitTest.js':
return [3 => 1];

Expand Down

0 comments on commit 27678e7

Please sign in to comment.