Drupal investigation

AbstractVariableSniff.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * A class to find T_VARIABLE tokens.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  16. $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
  17. throw new PHP_CodeSniffer_Exception($error);
  18. }
  19. /**
  20. * A class to find T_VARIABLE tokens.
  21. *
  22. * This class can distinguish between normal T_VARIABLE tokens, and those tokens
  23. * that represent class members. If a class member is encountered, then the
  24. * processMemberVar method is called so the extending class can process it. If
  25. * the token is found to be a normal T_VARIABLE token, then processVariable is
  26. * called.
  27. *
  28. * @category PHP
  29. * @package PHP_CodeSniffer
  30. * @author Greg Sherwood <gsherwood@squiz.net>
  31. * @author Marc McIntyre <mmcintyre@squiz.net>
  32. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  33. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  34. * @version Release: @package_version@
  35. * @link http://pear.php.net/package/PHP_CodeSniffer
  36. */
  37. abstract class PHP_CodeSniffer_Standards_AbstractVariableSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  38. {
  39. /**
  40. * The end token of the current function that we are in.
  41. *
  42. * @var int
  43. */
  44. private $_endFunction = -1;
  45. /**
  46. * TRUE if a function is currently open.
  47. *
  48. * @var boolean
  49. */
  50. private $_functionOpen = false;
  51. /**
  52. * The current PHP_CodeSniffer file that we are processing.
  53. *
  54. * @var PHP_CodeSniffer_File
  55. */
  56. protected $currentFile = null;
  57. /**
  58. * Constructs an AbstractVariableTest.
  59. */
  60. public function __construct()
  61. {
  62. $scopes = array(
  63. T_CLASS,
  64. T_ANON_CLASS,
  65. T_TRAIT,
  66. T_INTERFACE,
  67. );
  68. $listen = array(
  69. T_FUNCTION,
  70. T_VARIABLE,
  71. T_DOUBLE_QUOTED_STRING,
  72. T_HEREDOC,
  73. );
  74. parent::__construct($scopes, $listen, true);
  75. }//end __construct()
  76. /**
  77. * Processes the token in the specified PHP_CodeSniffer_File.
  78. *
  79. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
  80. * token was found.
  81. * @param int $stackPtr The position where the token was found.
  82. * @param array $currScope The current scope opener token.
  83. *
  84. * @return void
  85. */
  86. protected final function processTokenWithinScope(
  87. PHP_CodeSniffer_File $phpcsFile,
  88. $stackPtr,
  89. $currScope
  90. ) {
  91. if ($this->currentFile !== $phpcsFile) {
  92. $this->currentFile = $phpcsFile;
  93. $this->_functionOpen = false;
  94. $this->_endFunction = -1;
  95. }
  96. $tokens = $phpcsFile->getTokens();
  97. if ($stackPtr > $this->_endFunction) {
  98. $this->_functionOpen = false;
  99. }
  100. if ($tokens[$stackPtr]['code'] === T_FUNCTION
  101. && $this->_functionOpen === false
  102. ) {
  103. $this->_functionOpen = true;
  104. $methodProps = $phpcsFile->getMethodProperties($stackPtr);
  105. // If the function is abstract, or is in an interface,
  106. // then set the end of the function to it's closing semicolon.
  107. if ($methodProps['is_abstract'] === true
  108. || $tokens[$currScope]['code'] === T_INTERFACE
  109. ) {
  110. $this->_endFunction
  111. = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr);
  112. } else {
  113. if (isset($tokens[$stackPtr]['scope_closer']) === false) {
  114. $error = 'Possible parse error: non-abstract method defined as abstract';
  115. $phpcsFile->addWarning($error, $stackPtr);
  116. return;
  117. }
  118. $this->_endFunction = $tokens[$stackPtr]['scope_closer'];
  119. }
  120. }//end if
  121. if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
  122. || $tokens[$stackPtr]['code'] === T_HEREDOC
  123. ) {
  124. // Check to see if this string has a variable in it.
  125. $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
  126. if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
  127. $this->processVariableInString($phpcsFile, $stackPtr);
  128. }
  129. return;
  130. }
  131. if ($this->_functionOpen === true) {
  132. if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
  133. $this->processVariable($phpcsFile, $stackPtr);
  134. }
  135. } else {
  136. // What if we assign a member variable to another?
  137. // ie. private $_count = $this->_otherCount + 1;.
  138. $this->processMemberVar($phpcsFile, $stackPtr);
  139. }
  140. }//end processTokenWithinScope()
  141. /**
  142. * Processes the token outside the scope in the file.
  143. *
  144. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
  145. * token was found.
  146. * @param int $stackPtr The position where the token was found.
  147. *
  148. * @return void
  149. */
  150. protected final function processTokenOutsideScope(
  151. PHP_CodeSniffer_File $phpcsFile,
  152. $stackPtr
  153. ) {
  154. $tokens = $phpcsFile->getTokens();
  155. // These variables are not member vars.
  156. if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
  157. $this->processVariable($phpcsFile, $stackPtr);
  158. } else if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING
  159. || $tokens[$stackPtr]['code'] === T_HEREDOC
  160. ) {
  161. // Check to see if this string has a variable in it.
  162. $pattern = '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
  163. if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
  164. $this->processVariableInString($phpcsFile, $stackPtr);
  165. }
  166. }
  167. }//end processTokenOutsideScope()
  168. /**
  169. * Called to process class member vars.
  170. *
  171. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
  172. * token was found.
  173. * @param int $stackPtr The position where the token was found.
  174. *
  175. * @return void
  176. */
  177. abstract protected function processMemberVar(
  178. PHP_CodeSniffer_File $phpcsFile,
  179. $stackPtr
  180. );
  181. /**
  182. * Called to process normal member vars.
  183. *
  184. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
  185. * token was found.
  186. * @param int $stackPtr The position where the token was found.
  187. *
  188. * @return void
  189. */
  190. abstract protected function processVariable(
  191. PHP_CodeSniffer_File $phpcsFile,
  192. $stackPtr
  193. );
  194. /**
  195. * Called to process variables found in double quoted strings or heredocs.
  196. *
  197. * Note that there may be more than one variable in the string, which will
  198. * result only in one call for the string or one call per line for heredocs.
  199. *
  200. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
  201. * token was found.
  202. * @param int $stackPtr The position where the double quoted
  203. * string was found.
  204. *
  205. * @return void
  206. */
  207. abstract protected function processVariableInString(
  208. PHP_CodeSniffer_File
  209. $phpcsFile,
  210. $stackPtr
  211. );
  212. }//end class