explicit_string_variable.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. =================================
  2. Rule ``explicit_string_variable``
  3. =================================
  4. Converts implicit variables into explicit ones in double-quoted strings or
  5. heredoc syntax.
  6. Description
  7. -----------
  8. The reasoning behind this rule is the following:
  9. - When there are two valid ways of doing the same thing, using both is
  10. confusing, there should be a coding standard to follow.
  11. - PHP manual marks ``"$var"`` syntax as implicit and ``"{$var}"`` syntax as
  12. explicit: explicit code should always be preferred.
  13. - Explicit syntax allows word concatenation inside strings, e.g.
  14. ``"{$var}IsAVar"``, implicit doesn't.
  15. - Explicit syntax is easier to detect for IDE/editors and therefore has
  16. colors/highlight with higher contrast, which is easier to read.
  17. Backtick operator is skipped because it is harder to handle; you can use
  18. ``backtick_to_shell_exec`` fixer to normalize backticks to strings.
  19. Examples
  20. --------
  21. Example #1
  22. ~~~~~~~~~~
  23. .. code-block:: diff
  24. --- Original
  25. +++ New
  26. <?php
  27. -$a = "My name is $name !";
  28. -$b = "I live in $state->country !";
  29. -$c = "I have $farm[0] chickens !";
  30. +$a = "My name is {$name} !";
  31. +$b = "I live in {$state->country} !";
  32. +$c = "I have {$farm[0]} chickens !";
  33. Rule sets
  34. ---------
  35. The rule is part of the following rule set:
  36. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
  37. References
  38. ----------
  39. - Fixer class: `PhpCsFixer\\Fixer\\StringNotation\\ExplicitStringVariableFixer <./../../../src/Fixer/StringNotation/ExplicitStringVariableFixer.php>`_
  40. - Test class: `PhpCsFixer\\Tests\\Fixer\\StringNotation\\ExplicitStringVariableFixerTest <./../../../tests/Fixer/StringNotation/ExplicitStringVariableFixerTest.php>`_
  41. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.