Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@
class UnnecessaryHeredocSniff implements Sniff
{

/**
* Escape chars which are supported in heredocs, but not in nowdocs.
*
* @var array<string>
*/
private $escapeChars = [
// Octal sequences.
'\0',
'\1',
'\2',
'\3',
'\4',
'\5',
'\6',
'\7',

// Various whitespace and the escape char.
'\n',
'\r',
'\t',
'\v',
'\e',
'\f',

// Hex and unicode sequences.
'\x',
'\u',
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -81,14 +110,33 @@ public function process(File $phpcsFile, $stackPtr)

$phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'no');

// Check for escape sequences which aren't supported in nowdocs.
foreach ($this->escapeChars as $testChar) {
if (strpos($body, $testChar) !== false) {
return;
}
}

$warning = 'Detected heredoc without interpolation or expressions. Use nowdoc syntax instead';

$fix = $phpcsFile->addFixableWarning($warning, $stackPtr, 'Found');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

$identifier = trim(ltrim($tokens[$stackPtr]['content'], '<'));
$replaceWith = "'".trim($identifier, '"')."'";
$replacement = str_replace($identifier, $replaceWith, $tokens[$stackPtr]['content']);
$phpcsFile->fixer->replaceToken($stackPtr, $replacement);

for ($i = ($stackPtr + 1); $i < $closer; $i++) {
$content = $tokens[$i]['content'];
$content = str_replace(['\\$', '\\\\'], ['$', '\\'], $content);
if ($tokens[$i]['content'] !== $content) {
$phpcsFile->fixer->replaceToken($i, $content);
}
}

$phpcsFile->fixer->endChangeset();
}

}//end process()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,19 @@ END;
$heredoc = <<< "END"
some text
some \$text
some text
some text \\ including a backslash
END;

$heredoc = <<<EOD
<?php
echo 'The below line contains escape characters and should be recognized as needing heredoc';
echo "aa\xC3\xC3 \xC3\xB8aa";
EOD;

echo <<<EOT
This should print a capital 'A': \x41
EOT;

echo <<<EOT
Here we should have a tab and 2 'A's: \t \101 \u{41}
EOT;
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ END;

$heredoc = <<< 'END'
some text
some \$text
some text
some $text
some text \ including a backslash
END;

$heredoc = <<<EOD
<?php
echo 'The below line contains escape characters and should be recognized as needing heredoc';
echo "aa\xC3\xC3 \xC3\xB8aa";
EOD;

echo <<<EOT
This should print a capital 'A': \x41
EOT;

echo <<<EOT
Here we should have a tab and 2 'A's: \t \101 \u{41}
EOT;
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,19 @@ $heredoc = <<<END
$heredoc = <<< "END"
some text
some \$text
some text
some text \\ including a backslash
END;

$heredoc = <<<EOD
<?php
echo 'The below line contains escape characters and should be recognized as needing heredoc';
echo "aa\xC3\xC3 \xC3\xB8aa";
EOD;

echo <<<EOT
This should print a capital 'A': \x41
EOT;

echo <<<EOT
Here we should have a tab and 2 'A's: \t \101 \u{41}
EOT;
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ $heredoc = <<<'END'

$heredoc = <<< 'END'
some text
some \$text
some text
some $text
some text \ including a backslash
END;

$heredoc = <<<EOD
<?php
echo 'The below line contains escape characters and should be recognized as needing heredoc';
echo "aa\xC3\xC3 \xC3\xB8aa";
EOD;

echo <<<EOT
This should print a capital 'A': \x41
EOT;

echo <<<EOT
Here we should have a tab and 2 'A's: \t \101 \u{41}
EOT;