Skip to content

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

Description

@jrfnl
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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions