NewHeredocSniff.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2019 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Sniffs\InitialValue;
  11. use PHPCompatibility\Sniffs\InitialValue\NewConstantScalarExpressionsSniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect a heredoc being used to set an initial value.
  16. *
  17. * As of PHP 5.3.0, it's possible to initialize static variables, class properties
  18. * and constants declared using the `const` keyword, using the Heredoc syntax.
  19. * And while not documented, heredoc initialization can now also be used for function param defaults.
  20. * See: https://3v4l.org/JVH8W
  21. *
  22. * These heredocs (still) cannot contain variables. That's, however, outside the scope of the
  23. * PHPCompatibility library until such time as there is a PHP version in which this would be accepted.
  24. *
  25. * PHP version 5.3
  26. *
  27. * @link https://www.php.net/manual/en/migration53.new-features.php
  28. * @link https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
  29. *
  30. * @since 7.1.4
  31. * @since 8.2.0 Now extends the NewConstantScalarExpressionsSniff instead of the base Sniff class.
  32. * @since 9.0.0 Renamed from `NewHeredocInitializeSniff` to `NewHeredocSniff`.
  33. */
  34. class NewHeredocSniff extends NewConstantScalarExpressionsSniff
  35. {
  36. /**
  37. * Error message.
  38. *
  39. * @since 8.2.0
  40. *
  41. * @var string
  42. */
  43. const ERROR_PHRASE = 'Initializing %s using the Heredoc syntax was not supported in PHP 5.2 or earlier';
  44. /**
  45. * Partial error phrases to be used in combination with the error message constant.
  46. *
  47. * @since 8.2.0
  48. *
  49. * @var array
  50. */
  51. protected $errorPhrases = array(
  52. 'const' => 'constants',
  53. 'property' => 'class properties',
  54. 'staticvar' => 'static variables',
  55. 'default' => 'default parameter values',
  56. );
  57. /**
  58. * Do a version check to determine if this sniff needs to run at all.
  59. *
  60. * @since 8.2.0
  61. *
  62. * @return bool
  63. */
  64. protected function bowOutEarly()
  65. {
  66. return ($this->supportsBelow('5.2') !== true);
  67. }
  68. /**
  69. * Is a value declared and does the declared value not contain an heredoc ?
  70. *
  71. * @since 8.2.0
  72. *
  73. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  74. * @param int $stackPtr The position of the current token in the
  75. * stack passed in $tokens.
  76. * @param int $end The end of the value definition.
  77. *
  78. * @return bool True if no heredoc (or assignment) is found, false otherwise.
  79. */
  80. protected function isValidAssignment(File $phpcsFile, $stackPtr, $end)
  81. {
  82. $tokens = $phpcsFile->getTokens();
  83. $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), $end, true);
  84. if ($next === false || $tokens[$next]['code'] !== \T_EQUAL) {
  85. // No value assigned.
  86. return true;
  87. }
  88. return ($phpcsFile->findNext(\T_START_HEREDOC, ($next + 1), $end, false, null, true) === false);
  89. }
  90. }