NewUniformVariableSyntaxSniff.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * The interpretation of variable variables has changed in PHP 7.0.
  16. *
  17. * PHP version 7.0
  18. *
  19. * @link https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.indirect
  20. * @link https://wiki.php.net/rfc/uniform_variable_syntax
  21. *
  22. * @since 7.1.2
  23. * @since 9.0.0 Renamed from `VariableVariablesSniff` to `NewUniformVariableSyntaxSniff`.
  24. */
  25. class NewUniformVariableSyntaxSniff extends Sniff
  26. {
  27. /**
  28. * Returns an array of tokens this test wants to listen for.
  29. *
  30. * @since 7.1.2
  31. *
  32. * @return array
  33. */
  34. public function register()
  35. {
  36. return array(\T_VARIABLE);
  37. }
  38. /**
  39. * Processes this test, when one of its tokens is encountered.
  40. *
  41. * @since 7.1.2
  42. *
  43. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  44. * @param int $stackPtr The position of the current token
  45. * in the stack passed in $tokens.
  46. *
  47. * @return void
  48. */
  49. public function process(File $phpcsFile, $stackPtr)
  50. {
  51. if ($this->supportsAbove('7.0') === false) {
  52. return;
  53. }
  54. $tokens = $phpcsFile->getTokens();
  55. // Verify that the next token is a square open bracket. If not, bow out.
  56. $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
  57. if ($nextToken === false || $tokens[$nextToken]['code'] !== \T_OPEN_SQUARE_BRACKET || isset($tokens[$nextToken]['bracket_closer']) === false) {
  58. return;
  59. }
  60. // The previous non-empty token has to be a $, -> or ::.
  61. $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
  62. if ($prevToken === false || \in_array($tokens[$prevToken]['code'], array(\T_DOLLAR, \T_OBJECT_OPERATOR, \T_DOUBLE_COLON), true) === false) {
  63. return;
  64. }
  65. // For static object calls, it only applies when this is a function call.
  66. if ($tokens[$prevToken]['code'] === \T_DOUBLE_COLON) {
  67. $hasBrackets = $tokens[$nextToken]['bracket_closer'];
  68. while (($hasBrackets = $phpcsFile->findNext(Tokens::$emptyTokens, ($hasBrackets + 1), null, true, null, true)) !== false) {
  69. if ($tokens[$hasBrackets]['code'] === \T_OPEN_SQUARE_BRACKET) {
  70. if (isset($tokens[$hasBrackets]['bracket_closer'])) {
  71. $hasBrackets = $tokens[$hasBrackets]['bracket_closer'];
  72. continue;
  73. } else {
  74. // Live coding.
  75. return;
  76. }
  77. } elseif ($tokens[$hasBrackets]['code'] === \T_OPEN_PARENTHESIS) {
  78. // Caught!
  79. break;
  80. } else {
  81. // Not a function call, so bow out.
  82. return;
  83. }
  84. }
  85. // Now let's also prevent false positives when used with self and static which still work fine.
  86. $classToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true, null, true);
  87. if ($classToken !== false) {
  88. if ($tokens[$classToken]['code'] === \T_STATIC || $tokens[$classToken]['code'] === \T_SELF) {
  89. return;
  90. } elseif ($tokens[$classToken]['code'] === \T_STRING && $tokens[$classToken]['content'] === 'self') {
  91. return;
  92. }
  93. }
  94. }
  95. $phpcsFile->addError(
  96. 'Indirect access to variables, properties and methods will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.',
  97. $stackPtr,
  98. 'Found'
  99. );
  100. }
  101. }