Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jrfnl opened this issue Jul 16, 2018 · 0 comments
Open

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

jrfnl opened this issue Jul 16, 2018 · 0 comments

Comments

@jrfnl
Copy link
Member

jrfnl commented Jul 16, 2018

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:

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant