class UnnecessaryHeredocSniff
Hierarchy
- class \PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryHeredocSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of UnnecessaryHeredocSniff
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ Strings/ UnnecessaryHeredocSniff.php, line 15
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\StringsView source
class UnnecessaryHeredocSniff implements Sniff {
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
return [
T_START_HEREDOC,
];
}
//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// Just to be safe. Shouldn't be possible as in that case, the opener shouldn't be tokenized
// to T_START_HEREDOC by PHP.
return;
// @codeCoverageIgnore
}
$closer = $tokens[$stackPtr]['scope_closer'];
$body = '';
// Collect all the tokens within the heredoc body.
for ($i = $stackPtr + 1; $i < $closer; $i++) {
$body .= $tokens[$i]['content'];
}
$tokenizedBody = token_get_all(sprintf("<?php <<<EOD\n%s\nEOD;\n?>", $body));
foreach ($tokenizedBody as $ptr => $bodyToken) {
if (is_array($bodyToken) === false) {
continue;
}
if ($bodyToken[0] === T_DOLLAR_OPEN_CURLY_BRACES || $bodyToken[0] === T_VARIABLE) {
// Contains interpolation or expression.
$phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'yes');
return;
}
if ($bodyToken[0] === T_CURLY_OPEN && is_array($tokenizedBody[$ptr + 1]) === false && $tokenizedBody[$ptr + 1] === '$') {
// Contains interpolation or expression.
$phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'yes');
return;
}
}
//end foreach
$phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'no');
$warning = 'Detected heredoc without interpolation or expressions. Use nowdoc syntax instead';
$fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'Found');
if ($fix === true) {
$identifier = trim(ltrim($tokens[$stackPtr]['content'], '<'));
$replaceWith = "'" . trim($identifier, '"') . "'";
$replacement = str_replace($identifier, $replaceWith, $tokens[$stackPtr]['content']);
$phpcsFile->fixer
->replaceToken($stackPtr, $replacement);
}
}
//end process()
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
UnnecessaryHeredocSniff::process | public | function | Processes this test, when one of its tokens is encountered. | Overrides Sniff::process |
UnnecessaryHeredocSniff::register | public | function | Returns an array of tokens this test wants to listen for. | Overrides Sniff::register |