AbstractFunctionCallParameterSniff.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  11. use PHPCompatibility\Sniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Abstract class to use as a base for examining the parameter values passed to function calls.
  16. *
  17. * @since 8.2.0
  18. */
  19. abstract class AbstractFunctionCallParameterSniff extends Sniff
  20. {
  21. /**
  22. * Is the sniff looking for a function call or a method call ?
  23. *
  24. * Note: the child class may need to do additional checks to make sure that
  25. * the method called is of the right class/object.
  26. * Checking that is outside of the scope of this abstract sniff.
  27. *
  28. * @since 8.2.0
  29. *
  30. * @var bool False (default) if the sniff is looking for function calls.
  31. * True if the sniff is looking for method calls.
  32. */
  33. protected $isMethod = false;
  34. /**
  35. * Functions the sniff is looking for. Should be defined in the child class.
  36. *
  37. * @since 8.2.0
  38. *
  39. * @var array The only requirement for this array is that the top level
  40. * array keys are the names of the functions you're looking for.
  41. * Other than that, the array can have arbitrary content
  42. * depending on your needs.
  43. */
  44. protected $targetFunctions = array();
  45. /**
  46. * List of tokens which when they preceed the $stackPtr indicate that this
  47. * is not a function call.
  48. *
  49. * @since 8.2.0
  50. *
  51. * @var array
  52. */
  53. private $ignoreTokens = array(
  54. \T_DOUBLE_COLON => true,
  55. \T_OBJECT_OPERATOR => true,
  56. \T_FUNCTION => true,
  57. \T_NEW => true,
  58. \T_CONST => true,
  59. \T_USE => true,
  60. );
  61. /**
  62. * Returns an array of tokens this test wants to listen for.
  63. *
  64. * @since 8.2.0
  65. *
  66. * @return array
  67. */
  68. public function register()
  69. {
  70. // Handle case-insensitivity of function names.
  71. $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions);
  72. return array(\T_STRING);
  73. }
  74. /**
  75. * Processes this test, when one of its tokens is encountered.
  76. *
  77. * @since 8.2.0
  78. *
  79. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  80. * @param int $stackPtr The position of the current token in
  81. * the stack passed in $tokens.
  82. *
  83. * @return int|void Integer stack pointer to skip forward or void to continue
  84. * normal file processing.
  85. */
  86. public function process(File $phpcsFile, $stackPtr)
  87. {
  88. if ($this->bowOutEarly() === true) {
  89. return;
  90. }
  91. $tokens = $phpcsFile->getTokens();
  92. $function = $tokens[$stackPtr]['content'];
  93. $functionLc = strtolower($function);
  94. if (isset($this->targetFunctions[$functionLc]) === false) {
  95. return;
  96. }
  97. $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  98. if ($this->isMethod === true) {
  99. if ($tokens[$prevNonEmpty]['code'] !== \T_DOUBLE_COLON
  100. && $tokens[$prevNonEmpty]['code'] !== \T_OBJECT_OPERATOR
  101. ) {
  102. // Not a call to a PHP method.
  103. return;
  104. }
  105. } else {
  106. if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) {
  107. // Not a call to a PHP function.
  108. return;
  109. }
  110. if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR
  111. && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING
  112. ) {
  113. // Namespaced function.
  114. return;
  115. }
  116. }
  117. $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr);
  118. if (empty($parameters)) {
  119. return $this->processNoParameters($phpcsFile, $stackPtr, $function);
  120. } else {
  121. return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters);
  122. }
  123. }
  124. /**
  125. * Do a version check to determine if this sniff needs to run at all.
  126. *
  127. * @since 8.2.0
  128. *
  129. * If the check done in a child class is not specific to one PHP version,
  130. * this function should return `false`.
  131. *
  132. * @return bool
  133. */
  134. abstract protected function bowOutEarly();
  135. /**
  136. * Process the parameters of a matched function.
  137. *
  138. * This method has to be made concrete in child classes.
  139. *
  140. * @since 8.2.0
  141. *
  142. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  143. * @param int $stackPtr The position of the current token in the stack.
  144. * @param string $functionName The token content (function name) which was matched.
  145. * @param array $parameters Array with information about the parameters.
  146. *
  147. * @return int|void Integer stack pointer to skip forward or void to continue
  148. * normal file processing.
  149. */
  150. abstract public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters);
  151. /**
  152. * Process the function if no parameters were found.
  153. *
  154. * Defaults to doing nothing. Can be overloaded in child classes to handle functions
  155. * were parameters are expected, but none found.
  156. *
  157. * @since 8.2.0
  158. *
  159. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  160. * @param int $stackPtr The position of the current token in the stack.
  161. * @param string $functionName The token content (function name) which was matched.
  162. *
  163. * @return int|void Integer stack pointer to skip forward or void to continue
  164. * normal file processing.
  165. */
  166. public function processNoParameters(File $phpcsFile, $stackPtr, $functionName)
  167. {
  168. return;
  169. }
  170. }