ArgumentFunctionsUsageSniff.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\FunctionUse;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect usage of `func_get_args()`, `func_get_arg()` and `func_num_args()` in invalid context.
  16. *
  17. * Checks for:
  18. * - Prior to PHP 5.3, these functions could not be used as a function call parameter.
  19. * - Calling these functions from the outermost scope of a file which has been included by
  20. * calling `include` or `require` from within a function in the calling file, worked
  21. * prior to PHP 5.3. As of PHP 5.3, this will generate a warning and will always return false/-1.
  22. * If the file was called directly or included in the global scope, calls to these
  23. * functions would already generate a warning prior to PHP 5.3.
  24. *
  25. * PHP version 5.3
  26. *
  27. * @link https://www.php.net/manual/en/migration53.incompatible.php
  28. *
  29. * @since 8.2.0
  30. */
  31. class ArgumentFunctionsUsageSniff extends Sniff
  32. {
  33. /**
  34. * The target functions for this sniff.
  35. *
  36. * @since 8.2.0
  37. *
  38. * @var array
  39. */
  40. protected $targetFunctions = array(
  41. 'func_get_args' => true,
  42. 'func_get_arg' => true,
  43. 'func_num_args' => true,
  44. );
  45. /**
  46. * Returns an array of tokens this test wants to listen for.
  47. *
  48. * @since 8.2.0
  49. *
  50. * @return array
  51. */
  52. public function register()
  53. {
  54. return array(\T_STRING);
  55. }
  56. /**
  57. * Processes this test, when one of its tokens is encountered.
  58. *
  59. * @since 8.2.0
  60. *
  61. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  62. * @param int $stackPtr The position of the current token in the
  63. * stack passed in $tokens.
  64. *
  65. * @return void
  66. */
  67. public function process(File $phpcsFile, $stackPtr)
  68. {
  69. $tokens = $phpcsFile->getTokens();
  70. $functionLc = strtolower($tokens[$stackPtr]['content']);
  71. if (isset($this->targetFunctions[$functionLc]) === false) {
  72. return;
  73. }
  74. // Next non-empty token should be the open parenthesis.
  75. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
  76. if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) {
  77. return;
  78. }
  79. $ignore = array(
  80. \T_DOUBLE_COLON => true,
  81. \T_OBJECT_OPERATOR => true,
  82. \T_FUNCTION => true,
  83. \T_NEW => true,
  84. );
  85. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  86. if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) {
  87. // Not a call to a PHP function.
  88. return;
  89. } elseif ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING) {
  90. // Namespaced function.
  91. return;
  92. }
  93. $data = $tokens[$stackPtr]['content'];
  94. /*
  95. * Check for use of the functions in the global scope.
  96. *
  97. * As PHPCS can not determine whether a file is included from within a function in
  98. * another file, so always throw a warning/error.
  99. */
  100. if ($phpcsFile->hasCondition($stackPtr, array(\T_FUNCTION, \T_CLOSURE)) === false) {
  101. $isError = false;
  102. $message = 'Use of %s() outside of a user-defined function is only supported if the file is included from within a user-defined function in another file prior to PHP 5.3.';
  103. if ($this->supportsAbove('5.3') === true) {
  104. $isError = true;
  105. $message .= ' As of PHP 5.3, it is no longer supported at all.';
  106. }
  107. $this->addMessage($phpcsFile, $message, $stackPtr, $isError, 'OutsideFunctionScope', $data);
  108. }
  109. /*
  110. * Check for use of the functions as a parameter in a function call.
  111. */
  112. if ($this->supportsBelow('5.2') === false) {
  113. return;
  114. }
  115. if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) {
  116. return;
  117. }
  118. $throwError = false;
  119. $closer = end($tokens[$stackPtr]['nested_parenthesis']);
  120. if (isset($tokens[$closer]['parenthesis_owner'])
  121. && $tokens[$tokens[$closer]['parenthesis_owner']]['type'] === 'T_CLOSURE'
  122. ) {
  123. $throwError = true;
  124. } else {
  125. $opener = key($tokens[$stackPtr]['nested_parenthesis']);
  126. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
  127. if ($tokens[$prevNonEmpty]['code'] !== \T_STRING) {
  128. return;
  129. }
  130. $prevPrevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true);
  131. if ($tokens[$prevPrevNonEmpty]['code'] === \T_FUNCTION) {
  132. return;
  133. }
  134. $throwError = true;
  135. }
  136. if ($throwError === false) {
  137. return;
  138. }
  139. $phpcsFile->addError(
  140. '%s() could not be used in parameter lists prior to PHP 5.3.',
  141. $stackPtr,
  142. 'InParameterList',
  143. $data
  144. );
  145. }
  146. }