Drupal investigation

AbstractScopeSniff.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * An AbstractScopeTest allows for tests that extend from this class to
  4. * listen for tokens within a particular scope.
  5. *
  6. * PHP version 5
  7. *
  8. * @category PHP
  9. * @package PHP_CodeSniffer
  10. * @author Greg Sherwood <gsherwood@squiz.net>
  11. * @author Marc McIntyre <mmcintyre@squiz.net>
  12. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  13. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * An AbstractScopeTest allows for tests that extend from this class to
  18. * listen for tokens within a particular scope.
  19. *
  20. * Below is a test that listens to methods that exist only within classes:
  21. * <code>
  22. * class ClassScopeTest extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  23. * {
  24. * public function __construct()
  25. * {
  26. * parent::__construct(array(T_CLASS), array(T_FUNCTION));
  27. * }
  28. *
  29. * protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $)
  30. * {
  31. * $className = $phpcsFile->getDeclarationName($currScope);
  32. * echo 'encountered a method within class '.$className;
  33. * }
  34. * }
  35. * </code>
  36. *
  37. * @category PHP
  38. * @package PHP_CodeSniffer
  39. * @author Greg Sherwood <gsherwood@squiz.net>
  40. * @author Marc McIntyre <mmcintyre@squiz.net>
  41. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  42. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  43. * @version Release: @package_version@
  44. * @link http://pear.php.net/package/PHP_CodeSniffer
  45. */
  46. abstract class PHP_CodeSniffer_Standards_AbstractScopeSniff implements PHP_CodeSniffer_Sniff
  47. {
  48. /**
  49. * The token types that this test wishes to listen to within the scope.
  50. *
  51. * @var array
  52. */
  53. private $_tokens = array();
  54. /**
  55. * The type of scope opener tokens that this test wishes to listen to.
  56. *
  57. * @var string
  58. */
  59. private $_scopeTokens = array();
  60. /**
  61. * True if this test should fire on tokens outside of the scope.
  62. *
  63. * @var boolean
  64. */
  65. private $_listenOutside = false;
  66. /**
  67. * Constructs a new AbstractScopeTest.
  68. *
  69. * @param array $scopeTokens The type of scope the test wishes to listen to.
  70. * @param array $tokens The tokens that the test wishes to listen to
  71. * within the scope.
  72. * @param boolean $listenOutside If true this test will also alert the
  73. * extending class when a token is found outside
  74. * the scope, by calling the
  75. * processTokenOutsideScope method.
  76. *
  77. * @see PHP_CodeSniffer.getValidScopeTokeners()
  78. * @throws PHP_CodeSniffer_Exception If the specified tokens array is empty.
  79. */
  80. public function __construct(
  81. array $scopeTokens,
  82. array $tokens,
  83. $listenOutside=false
  84. ) {
  85. if (empty($scopeTokens) === true) {
  86. $error = 'The scope tokens list cannot be empty';
  87. throw new PHP_CodeSniffer_Exception($error);
  88. }
  89. if (empty($tokens) === true) {
  90. $error = 'The tokens list cannot be empty';
  91. throw new PHP_CodeSniffer_Exception($error);
  92. }
  93. $invalidScopeTokens = array_intersect($scopeTokens, $tokens);
  94. if (empty($invalidScopeTokens) === false) {
  95. $invalid = implode(', ', $invalidScopeTokens);
  96. $error = "Scope tokens [$invalid] can't be in the tokens array";
  97. throw new PHP_CodeSniffer_Exception($error);
  98. }
  99. $this->_listenOutside = $listenOutside;
  100. $this->_scopeTokens = array_flip($scopeTokens);
  101. $this->_tokens = $tokens;
  102. }//end __construct()
  103. /**
  104. * The method that is called to register the tokens this test wishes to
  105. * listen to.
  106. *
  107. * DO NOT OVERRIDE THIS METHOD. Use the constructor of this class to register
  108. * for the desired tokens and scope.
  109. *
  110. * @return int[]
  111. * @see __constructor()
  112. */
  113. public final function register()
  114. {
  115. return $this->_tokens;
  116. }//end register()
  117. /**
  118. * Processes the tokens that this test is listening for.
  119. *
  120. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  121. * @param int $stackPtr The position in the stack where this
  122. * token was found.
  123. *
  124. * @return void
  125. * @see processTokenWithinScope()
  126. */
  127. public final function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  128. {
  129. $tokens = $phpcsFile->getTokens();
  130. $foundScope = false;
  131. foreach ($tokens[$stackPtr]['conditions'] as $scope => $code) {
  132. if (isset($this->_scopeTokens[$code]) === true) {
  133. $this->processTokenWithinScope($phpcsFile, $stackPtr, $scope);
  134. $foundScope = true;
  135. }
  136. }
  137. if ($this->_listenOutside === true && $foundScope === false) {
  138. $this->processTokenOutsideScope($phpcsFile, $stackPtr);
  139. }
  140. }//end process()
  141. /**
  142. * Processes a token that is found within the scope that this test is
  143. * listening to.
  144. *
  145. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  146. * @param int $stackPtr The position in the stack where this
  147. * token was found.
  148. * @param int $currScope The position in the tokens array that
  149. * opened the scope that this test is
  150. * listening for.
  151. *
  152. * @return void
  153. */
  154. protected abstract function processTokenWithinScope(
  155. PHP_CodeSniffer_File $phpcsFile,
  156. $stackPtr,
  157. $currScope
  158. );
  159. /**
  160. * Processes a token that is found outside the scope that this test is
  161. * listening to.
  162. *
  163. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  164. * @param int $stackPtr The position in the stack where this
  165. * token was found.
  166. *
  167. * @return void
  168. */
  169. protected function processTokenOutsideScope(
  170. PHP_CodeSniffer_File $phpcsFile,
  171. $stackPtr
  172. ) {
  173. }//end processTokenOutsideScope()
  174. }//end class