comparison vendor/squizlabs/php_codesniffer/src/Sniffs/AbstractScopeSniff.php @ 4:a9cd425dd02b

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