AbstractVariableSniff.php 7.4 KB

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