Skip to content
Open
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
10 changes: 7 additions & 3 deletions src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ public function process(File $phpcsFile, int $stackPtr)
$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) {
$origContent = $tokens[$i]['content'];
if (isset($tokens[$i]['orig_content']) === true) {
$origContent = $tokens[$i]['orig_content'];
}

$content = str_replace(['\\$', '\\\\'], ['$', '\\'], $origContent);
if ($origContent !== $content) {
$phpcsFile->fixer->replaceToken($i, $content);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/*
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These two files should be kept in sync!
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/*
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These two files should be kept in sync!
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/*
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These two files should be kept in sync!
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/*
* Test file 1 and 2 mirror each other, with file 1 containing non-indented cross-version compatible heredoc/nowdoc syntax,
* while the code samples in file 2 use PHP 7.3+ flexible heredoc/nowdoc syntax.
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These two files should be kept in sync!
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
Expand Down
126 changes: 126 additions & 0 deletions src/Standards/Generic/Tests/Strings/UnnecessaryHeredocUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/*
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
some text
EOD;

$heredoc = <<<END
some $foo text
END;

$heredoc = <<<"END"
some {$foo[0]} text
END;

$heredoc = <<<END
{$foo?->bar}
END;

$heredoc = <<< "END"
some ${beers::softdrink}
END;

$heredoc = <<< END
{${$object->getName()}} text
END;

$heredoc = <<<"END"
some {${getName()}}
END;

$heredoc = <<<END
${substr('laruence', 0, 2)}
END;

$heredoc = <<<"END"
some {$foo['bar']->baz()()}
END;

$heredoc = <<<END
{$obj->values[3]->name} text
END;

$heredoc = <<<"END"
some ${$bar}
END;

$heredoc = <<<END
${foo->bar} text
END;

$heredoc = <<<"END"
${foo["${bar}"]} text
END;

$heredoc = <<<END
some ${foo["${bar[\'baz\']}"]}
END;

$heredoc = <<<"END"
${foo->{${'a'}}} text
END;

$heredoc = <<<END
some {$foo->{$baz[1]}}
END;

$heredoc = <<<END
some text
{${beers::$ale}}
some text
END;

$heredoc = <<<"END"
$people->john's wife greeted $people->robert.
END;

$heredoc = <<<END
Let's make sure it also works with this: {$arr[foo][3]}
END;

$heredoc = <<<END
Testing ${foo["${bar
['baz']
}"]} and more testing
END;

$heredoc = <<<"END"
Testing {${foo["${bar
['baz']
}"]}} and more testing
END;

$heredoc = <<<END
some text
END;

$heredoc = <<< "END"
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
@@ -0,0 +1,126 @@
<?php

/*
* Test files 1, 2, 4, 5 and 6 mirror each other for the test cases, with slight syntax variations.
* - File 1 contains non-indented cross-version compatible heredoc/nowdoc syntax with a non-indented heredoc/nowdoc body.
* - File 2 contains PHP 7.3+ flexible heredoc/nowdoc syntax with space indented heredoc/nowdoc body and scope closer.
* - File 4 contains non-indented cross-version compatible heredoc/nowdoc syntax with a space indented heredoc/nowdoc body.
* - File 5 contains non-indented cross-version compatible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body.
* - File 6 contains PHP 7.3+ flexible heredoc/nowdoc syntax with a tab indented heredoc/nowdoc body and scope closer.
*
* These files should be kept in sync!
*/

$nowdoc = <<<'EOD'
some text
EOD;

$heredoc = <<<END
some $foo text
END;

$heredoc = <<<"END"
some {$foo[0]} text
END;

$heredoc = <<<END
{$foo?->bar}
END;

$heredoc = <<< "END"
some ${beers::softdrink}
END;

$heredoc = <<< END
{${$object->getName()}} text
END;

$heredoc = <<<"END"
some {${getName()}}
END;

$heredoc = <<<END
${substr('laruence', 0, 2)}
END;

$heredoc = <<<"END"
some {$foo['bar']->baz()()}
END;

$heredoc = <<<END
{$obj->values[3]->name} text
END;

$heredoc = <<<"END"
some ${$bar}
END;

$heredoc = <<<END
${foo->bar} text
END;

$heredoc = <<<"END"
${foo["${bar}"]} text
END;

$heredoc = <<<END
some ${foo["${bar[\'baz\']}"]}
END;

$heredoc = <<<"END"
${foo->{${'a'}}} text
END;

$heredoc = <<<END
some {$foo->{$baz[1]}}
END;

$heredoc = <<<END
some text
{${beers::$ale}}
some text
END;

$heredoc = <<<"END"
$people->john's wife greeted $people->robert.
END;

$heredoc = <<<END
Let's make sure it also works with this: {$arr[foo][3]}
END;

$heredoc = <<<END
Testing ${foo["${bar
['baz']
}"]} and more testing
END;

$heredoc = <<<"END"
Testing {${foo["${bar
['baz']
}"]}} and more testing
END;

$heredoc = <<<'END'
some text
END;

$heredoc = <<< 'END'
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;
Loading