ForbiddenGlobalVariableVariableSniff.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\Variables;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect use of `global` with variable variables, support for which has been removed in PHP 7.0.
  16. *
  17. * PHP version 7.0
  18. *
  19. * @link https://wiki.php.net/rfc/uniform_variable_syntax#global_keyword_takes_only_simple_variables
  20. *
  21. * @since 7.0.0
  22. */
  23. class ForbiddenGlobalVariableVariableSniff extends Sniff
  24. {
  25. /**
  26. * Returns an array of tokens this test wants to listen for.
  27. *
  28. * @since 7.0.0
  29. *
  30. * @return array
  31. */
  32. public function register()
  33. {
  34. return array(\T_GLOBAL);
  35. }
  36. /**
  37. * Processes this test, when one of its tokens is encountered.
  38. *
  39. * @since 7.0.0
  40. *
  41. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  42. * @param int $stackPtr The position of the current token in the
  43. * stack passed in $tokens.
  44. *
  45. * @return void
  46. */
  47. public function process(File $phpcsFile, $stackPtr)
  48. {
  49. if ($this->supportsAbove('7.0') === false) {
  50. return;
  51. }
  52. $tokens = $phpcsFile->getTokens();
  53. $endOfStatement = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($stackPtr + 1));
  54. if ($endOfStatement === false) {
  55. // No semi-colon - live coding.
  56. return;
  57. }
  58. for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) {
  59. $errorThrown = false;
  60. $nextComma = $phpcsFile->findNext(\T_COMMA, $ptr, $endOfStatement, false, null, true);
  61. $varEnd = ($nextComma === false) ? $endOfStatement : $nextComma;
  62. $variable = $phpcsFile->findNext(\T_VARIABLE, $ptr, $varEnd);
  63. $varString = trim($phpcsFile->getTokensAsString($ptr, ($varEnd - $ptr)));
  64. $data = array($varString);
  65. if ($variable !== false) {
  66. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($variable - 1), $ptr, true);
  67. if ($prev !== false && $tokens[$prev]['type'] === 'T_DOLLAR') {
  68. $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($variable + 1), $varEnd, true);
  69. if ($next !== false
  70. && \in_array($tokens[$next]['code'], array(\T_OPEN_SQUARE_BRACKET, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === true
  71. ) {
  72. $phpcsFile->addError(
  73. 'Global with variable variables is not allowed since PHP 7.0. Found %s',
  74. $variable,
  75. 'Found',
  76. $data
  77. );
  78. $errorThrown = true;
  79. } else {
  80. $phpcsFile->addWarning(
  81. 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s',
  82. $variable,
  83. 'NonBareVariableFound',
  84. $data
  85. );
  86. $errorThrown = true;
  87. }
  88. }
  89. }
  90. if ($errorThrown === false) {
  91. $dollar = $phpcsFile->findNext(\T_DOLLAR, $ptr, $varEnd);
  92. if ($dollar !== false) {
  93. $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($dollar + 1), $varEnd, true);
  94. if ($tokens[$next]['code'] === \T_OPEN_CURLY_BRACKET) {
  95. $phpcsFile->addWarning(
  96. 'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s',
  97. $dollar,
  98. 'NonBareVariableFound',
  99. $data
  100. );
  101. }
  102. }
  103. }
  104. // Move the stack pointer forward to the next variable for multi-variable statements.
  105. if ($nextComma === false) {
  106. break;
  107. }
  108. $ptr = $nextComma;
  109. }
  110. }
  111. }