annotate vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/AbstractScopeSniff.php @ 8:50b0d041100e

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