PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/php/PHP_CodeSniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php

http://github.com/jonswar/perl-code-tidyall
PHP | 239 lines | 130 code | 40 blank | 69 comment | 22 complexity | 63ca5a46347c0734e32292a8bb20a54a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, 0BSD, MIT
  1. <?php
  2. /**
  3. * Discourages the use of alias functions.
  4. *
  5. * Alias functions are kept in PHP for compatibility
  6. * with older versions. Can be used to forbid the use of any function.
  7. *
  8. * @author Greg Sherwood <gsherwood@squiz.net>
  9. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  10. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  11. */
  12. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15. class ForbiddenFunctionsSniff implements Sniff
  16. {
  17. /**
  18. * A list of forbidden functions with their alternatives.
  19. *
  20. * The value is NULL if no alternative exists. IE, the
  21. * function should just not be used.
  22. *
  23. * @var array<string, string|null>
  24. */
  25. public $forbiddenFunctions = [
  26. 'sizeof' => 'count',
  27. 'delete' => 'unset',
  28. ];
  29. /**
  30. * A cache of forbidden function names, for faster lookups.
  31. *
  32. * @var string[]
  33. */
  34. protected $forbiddenFunctionNames = [];
  35. /**
  36. * If true, forbidden functions will be considered regular expressions.
  37. *
  38. * @var boolean
  39. */
  40. protected $patternMatch = false;
  41. /**
  42. * If true, an error will be thrown; otherwise a warning.
  43. *
  44. * @var boolean
  45. */
  46. public $error = true;
  47. /**
  48. * Returns an array of tokens this test wants to listen for.
  49. *
  50. * @return array
  51. */
  52. public function register()
  53. {
  54. // Everyone has had a chance to figure out what forbidden functions
  55. // they want to check for, so now we can cache out the list.
  56. $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
  57. if ($this->patternMatch === true) {
  58. foreach ($this->forbiddenFunctionNames as $i => $name) {
  59. $this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
  60. }
  61. return [T_STRING];
  62. }
  63. // If we are not pattern matching, we need to work out what
  64. // tokens to listen for.
  65. $hasHaltCompiler = false;
  66. $string = '<?php ';
  67. foreach ($this->forbiddenFunctionNames as $name) {
  68. if ($name === '__halt_compiler') {
  69. $hasHaltCompiler = true;
  70. } else {
  71. $string .= $name.'();';
  72. }
  73. }
  74. if ($hasHaltCompiler === true) {
  75. $string .= '__halt_compiler();';
  76. }
  77. $register = [];
  78. $tokens = token_get_all($string);
  79. array_shift($tokens);
  80. foreach ($tokens as $token) {
  81. if (is_array($token) === true) {
  82. $register[] = $token[0];
  83. }
  84. }
  85. $this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
  86. $this->forbiddenFunctions = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
  87. return array_unique($register);
  88. }//end register()
  89. /**
  90. * Processes this test, when one of its tokens is encountered.
  91. *
  92. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  93. * @param int $stackPtr The position of the current token in
  94. * the stack passed in $tokens.
  95. *
  96. * @return void
  97. */
  98. public function process(File $phpcsFile, $stackPtr)
  99. {
  100. $tokens = $phpcsFile->getTokens();
  101. $ignore = [
  102. T_DOUBLE_COLON => true,
  103. T_OBJECT_OPERATOR => true,
  104. T_FUNCTION => true,
  105. T_CONST => true,
  106. T_PUBLIC => true,
  107. T_PRIVATE => true,
  108. T_PROTECTED => true,
  109. T_AS => true,
  110. T_NEW => true,
  111. T_INSTEADOF => true,
  112. T_NS_SEPARATOR => true,
  113. T_IMPLEMENTS => true,
  114. ];
  115. $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  116. // If function call is directly preceded by a NS_SEPARATOR it points to the
  117. // global namespace, so we should still catch it.
  118. if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
  119. $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
  120. if ($tokens[$prevToken]['code'] === T_STRING) {
  121. // Not in the global namespace.
  122. return;
  123. }
  124. }
  125. if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
  126. // Not a call to a PHP function.
  127. return;
  128. }
  129. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  130. if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
  131. // Not a call to a PHP function.
  132. return;
  133. }
  134. if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
  135. // Not a call to a PHP function.
  136. return;
  137. }
  138. $function = strtolower($tokens[$stackPtr]['content']);
  139. $pattern = null;
  140. if ($this->patternMatch === true) {
  141. $count = 0;
  142. $pattern = preg_replace(
  143. $this->forbiddenFunctionNames,
  144. $this->forbiddenFunctionNames,
  145. $function,
  146. 1,
  147. $count
  148. );
  149. if ($count === 0) {
  150. return;
  151. }
  152. // Remove the pattern delimiters and modifier.
  153. $pattern = substr($pattern, 1, -2);
  154. } else {
  155. if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
  156. return;
  157. }
  158. }//end if
  159. $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
  160. }//end process()
  161. /**
  162. * Generates the error or warning for this sniff.
  163. *
  164. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  165. * @param int $stackPtr The position of the forbidden function
  166. * in the token array.
  167. * @param string $function The name of the forbidden function.
  168. * @param string $pattern The pattern used for the match.
  169. *
  170. * @return void
  171. */
  172. protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
  173. {
  174. $data = [$function];
  175. $error = 'The use of function %s() is ';
  176. if ($this->error === true) {
  177. $type = 'Found';
  178. $error .= 'forbidden';
  179. } else {
  180. $type = 'Discouraged';
  181. $error .= 'discouraged';
  182. }
  183. if ($pattern === null) {
  184. $pattern = strtolower($function);
  185. }
  186. if ($this->forbiddenFunctions[$pattern] !== null
  187. && $this->forbiddenFunctions[$pattern] !== 'null'
  188. ) {
  189. $type .= 'WithAlternative';
  190. $data[] = $this->forbiddenFunctions[$pattern];
  191. $error .= '; use %s() instead';
  192. }
  193. if ($this->error === true) {
  194. $phpcsFile->addError($error, $stackPtr, $type, $data);
  195. } else {
  196. $phpcsFile->addWarning($error, $stackPtr, $type, $data);
  197. }
  198. }//end addError()
  199. }//end class