PHPCompatibility / PHPModernizer

External PHPCS standard with auto-fixers to modernize legacy codebases
GNU Lesser General Public License v3.0
10 stars 0 forks source link

New sniff: change heredoc to nowdoc if no variables are used #11

Open jrfnl opened 6 years ago

jrfnl commented 6 years ago
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):

PHP manual references:

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:

Prior art