Skip to content

Commit 89ee097

Browse files
committed
Squiz/Heredoc: make sniff more modular
Until now, the sniff would blindly forbid both heredoc and nowdoc syntax. With the change as proposed in this PR, the sniff still does so, but now uses different error codes for encountered heredocs vs nowdocs, which allows for selectively ignoring one or the other, either from the ruleset or inline. Includes updating the error message to be more specific for each error code as well. Fixes squizlabs/PHP_CodeSniffer 2318
1 parent b63cedf commit 89ee097

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Standards/Squiz/Sniffs/PHP/HeredocSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ public function register()
4242
*/
4343
public function process(File $phpcsFile, $stackPtr)
4444
{
45-
$error = 'Use of heredoc and nowdoc syntax ("<<<") is not allowed; use standard strings or inline HTML instead';
46-
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
45+
$tokens = $phpcsFile->getTokens();
46+
47+
$codePrefix = 'Heredoc';
48+
$data = ['heredoc'];
49+
if ($tokens[$stackPtr]['code'] === T_START_NOWDOC) {
50+
$codePrefix = 'Nowdoc';
51+
$data = ['nowdoc'];
52+
}
53+
54+
$data[] = trim($tokens[$stackPtr]['content']);
55+
56+
$error = 'Use of %s syntax (%s) is not allowed; use standard strings or inline HTML instead';
57+
$phpcsFile->addError($error, $stackPtr, $codePrefix.'NotAllowed', $data);
4758

4859
}//end process()
4960

0 commit comments

Comments
 (0)