-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
Sniff basics | - |
---|---|
Fixable for PHP: | 5.3+ |
Sniff type: | Modernize |
Fixer type: | Risky |
Short description
PHP 5.3 introduced the nowdoc
syntax. When no variable extrapolation or escape sequences are needed, a nowdoc
should be used instead of a heredoc
.
Related PHPCompatibility sniff(s):
Newkeywords
PHP manual references:
-
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
-
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double (escape codes)
Example code:
Detect the following code pattern(s):
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
// This heredoc should not be touched by the sniff!
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
And fix these to:
$str = <<<'EOD'
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
// Second example should be untouched.
Notes for implementation of the sniff:
- Beware of escaped variables in heredocs
\$variables
. These will not be extrapolated. - Beware of escape sequences which necessitate the use of heredoc over nowdoc.
Prior art
- CS-fixer:
heredoc_to_nowdoc