1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
<?php /** * An AbstractScopeTest allows for tests that extend from this class to * listen for tokens within a particluar scope. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @link http://pear.php.net/package/PHP_CodeSniffer */
/** * An AbstractScopeTest allows for tests that extend from this class to * listen for tokens within a particluar scope. * * Below is a test that listens to methods that exist only within classes: * <code> * class ClassScopeTest extends PHP_CodeSniffer_Standards_AbstractScopeSniff * { * public function __construct() * { * parent::__construct(array(T_CLASS), array(T_FUNCTION)); * } * * protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $) * { * $className = $phpcsFile->getDeclarationName($currScope); * echo 'encountered a method within class '.$className; * } * } * </code> * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: 1.3.3 * @link http://pear.php.net/package/PHP_CodeSniffer */ abstract class PHP_CodeSniffer_Standards_AbstractScopeSniff implements PHP_CodeSniffer_Sniff {
/** * The token types that this test wishes to listen to within the scope. * * @var array() */ private $_tokens = array();
/** * The type of scope opener tokens that this test wishes to listen to. * * @var string */ private $_scopeTokens = array();
/** * The position in the tokens array that opened the current scope. * * @var array() */ protected $currScope = null;
/** * The current file being checked. * * @var string */ protected $currFile = '';
/** * True if this test should fire on tokens outside of the scope. * * @var boolean */ private $_listenOutside = false;
/** * Constructs a new AbstractScopeTest. * * @param array $scopeTokens The type of scope the test wishes to listen to. * @param array $tokens The tokens that the test wishes to listen to * within the scope. * @param boolean $listenOutside If true this test will also alert the * extending class when a token is found outside * the scope, by calling the * processTokenOutideScope method. * * @see PHP_CodeSniffer.getValidScopeTokeners() * @throws PHP_CodeSniffer_Test_Exception If the specified tokens array is empty. */ public function __construct( array $scopeTokens, array $tokens, $listenOutside=false ) { if (empty($scopeTokens) === true) { $error = 'The scope tokens list cannot be empty'; throw new PHP_CodeSniffer_Test_Exception($error); }
if (empty($tokens) === true) { $error = 'The tokens list cannot be empty'; throw new PHP_CodeSniffer_Test_Exception($error); }
$invalidScopeTokens = array_intersect($scopeTokens, $tokens); if (empty($invalidScopeTokens) === false) { $invalid = implode(', ', $invalidScopeTokens); $error = "Scope tokens [$invalid] cant be in the tokens array"; throw new PHP_CodeSniffer_Test_Exception($error); }
$this->_listenOutside = $listenOutside; $this->_scopeTokens = $scopeTokens; $this->_tokens = $tokens;
}//end __construct()
/** * The method that is called to register the tokens this test wishes to * listen to. * * DO NOT OVERRIDE THIS METHOD. Use the constructor of this class to register * for the desired tokens and scope. * * @return array(int) * @see __constructor() */ public final function register() { if ($this->_listenOutside === false) { return $this->_scopeTokens; } else { return array_merge($this->_scopeTokens, $this->_tokens); }
}//end register()
/** * Processes the tokens that this test is listening for. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position in the stack where this * token was found. * * @return void * @see processTokenWithinScope() */ public final function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $file = $phpcsFile->getFilename(); if ($this->currFile !== $file) { // We have changed files, so clean up. $this->currScope = null; $this->currFile = $file; }
$tokens = $phpcsFile->getTokens();
if (in_array($tokens[$stackPtr]['code'], $this->_scopeTokens) === true) { $this->currScope = $stackPtr; $phpcsFile->addTokenListener($this, $this->_tokens); } else if ($this->currScope !== null && isset($tokens[$this->currScope]['scope_closer']) === true && $stackPtr > $tokens[$this->currScope]['scope_closer'] ) { $this->currScope = null; if ($this->_listenOutside === true) { // This is a token outside the current scope, so notify the // extender as they wish to know about this. $this->processTokenOutsideScope($phpcsFile, $stackPtr); } else { // Don't remove the listener if the extender wants to know about // tokens that live outside the current scope. $phpcsFile->removeTokenListener($this, $this->_tokens); } } else if ($this->currScope !== null) { $this->processTokenWithinScope($phpcsFile, $stackPtr, $this->currScope); } else { $this->processTokenOutsideScope($phpcsFile, $stackPtr); }
}//end process()
/** * Processes a token that is found within the scope that this test is * listening to. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position in the stack where this * token was found. * @param int $currScope The position in the tokens array that * opened the scope that this test is * listening for. * * @return void */ protected abstract function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope );
/** * Processes a token that is found within the scope that this test is * listening to. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position in the stack where this * token was found. * * @return void */ protected function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { return;
}//end processTokenOutsideScope()
}//end class
?>
|