ForbiddenVariableNamesInClosureUseSniff.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\FunctionDeclarations;
  11. use PHPCompatibility\Sniff;
  12. use PHPCompatibility\PHPCSHelper;
  13. use PHP_CodeSniffer_File as File;
  14. use PHP_CodeSniffer_Tokens as Tokens;
  15. /**
  16. * Detect variable names forbidden to be used in closure `use` statements.
  17. *
  18. * Variables bound to a closure via the `use` construct cannot use the same name
  19. * as any superglobals, `$this`, or any parameter since PHP 7.1.
  20. *
  21. * PHP version 7.1
  22. *
  23. * @link https://www.php.net/manual/en/migration71.incompatible.php#migration71.incompatible.lexical-names
  24. * @link https://www.php.net/manual/en/functions.anonymous.php
  25. *
  26. * @since 7.1.4
  27. */
  28. class ForbiddenVariableNamesInClosureUseSniff extends Sniff
  29. {
  30. /**
  31. * Returns an array of tokens this test wants to listen for.
  32. *
  33. * @since 7.1.4
  34. *
  35. * @return array
  36. */
  37. public function register()
  38. {
  39. return array(\T_USE);
  40. }
  41. /**
  42. * Processes this test, when one of its tokens is encountered.
  43. *
  44. * @since 7.1.4
  45. *
  46. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  47. * @param int $stackPtr The position of the current token
  48. * in the stack passed in $tokens.
  49. *
  50. * @return void
  51. */
  52. public function process(File $phpcsFile, $stackPtr)
  53. {
  54. if ($this->supportsAbove('7.1') === false) {
  55. return;
  56. }
  57. $tokens = $phpcsFile->getTokens();
  58. // Verify this use statement is used with a closure - if so, it has to have parenthesis before it.
  59. $previousNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
  60. if ($previousNonEmpty === false || $tokens[$previousNonEmpty]['code'] !== \T_CLOSE_PARENTHESIS
  61. || isset($tokens[$previousNonEmpty]['parenthesis_opener']) === false
  62. ) {
  63. return;
  64. }
  65. // ... and (a variable within) parenthesis after it.
  66. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
  67. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
  68. return;
  69. }
  70. if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) {
  71. // Live coding.
  72. return;
  73. }
  74. $closurePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($tokens[$previousNonEmpty]['parenthesis_opener'] - 1), null, true);
  75. if ($closurePtr === false || $tokens[$closurePtr]['code'] !== \T_CLOSURE) {
  76. return;
  77. }
  78. // Get the parameters declared by the closure.
  79. $closureParams = PHPCSHelper::getMethodParameters($phpcsFile, $closurePtr);
  80. $errorMsg = 'Variables bound to a closure via the use construct cannot use the same name as superglobals, $this, or a declared parameter since PHP 7.1. Found: %s';
  81. for ($i = ($nextNonEmpty + 1); $i < $tokens[$nextNonEmpty]['parenthesis_closer']; $i++) {
  82. if ($tokens[$i]['code'] !== \T_VARIABLE) {
  83. continue;
  84. }
  85. $variableName = $tokens[$i]['content'];
  86. if ($variableName === '$this') {
  87. $phpcsFile->addError($errorMsg, $i, 'FoundThis', array($variableName));
  88. continue;
  89. }
  90. if (isset($this->superglobals[$variableName]) === true) {
  91. $phpcsFile->addError($errorMsg, $i, 'FoundSuperglobal', array($variableName));
  92. continue;
  93. }
  94. // Check whether it is one of the parameters declared by the closure.
  95. if (empty($closureParams) === false) {
  96. foreach ($closureParams as $param) {
  97. if ($param['name'] === $variableName) {
  98. $phpcsFile->addError($errorMsg, $i, 'FoundShadowParam', array($variableName));
  99. continue 2;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }