1 | <?php |
||
19 | class PHPCompatibility_Sniffs_PHP_NewKeywordsSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|||
20 | { |
||
21 | |||
22 | /** |
||
23 | * A list of new keywords, not present in older versions. |
||
24 | * |
||
25 | * The array lists : version number with false (not present) or true (present). |
||
26 | * If's sufficient to list the last version which did not contain the keyword. |
||
27 | * |
||
28 | * Description will be used as part of the error message. |
||
29 | * Condition is the name of a callback method within this class or the parent class |
||
30 | * which checks whether the token complies with a certain condition. |
||
31 | * The callback function will be passed the $phpcsFile and the $stackPtr. |
||
32 | * The callback function should return `true` if the condition is met and the |
||
33 | * error should *not* be thrown. |
||
34 | * |
||
35 | * @var array(string => array(string => int|string|null)) |
||
36 | */ |
||
37 | protected $newKeywords = array( |
||
38 | 'T_HALT_COMPILER' => array( |
||
39 | '5.0' => false, |
||
40 | '5.1' => true, |
||
41 | 'description' => '"__halt_compiler" keyword', |
||
42 | ), |
||
43 | 'T_CONST' => array( |
||
44 | '5.2' => false, |
||
45 | '5.3' => true, |
||
46 | 'description' => '"const" keyword', |
||
47 | 'condition' => 'isClassConstant', // Keyword is only new when not in class context. |
||
48 | ), |
||
49 | 'T_CALLABLE' => array( |
||
50 | '5.3' => false, |
||
51 | '5.4' => true, |
||
52 | 'description' => '"callable" keyword', |
||
53 | 'content' => 'callable', |
||
54 | ), |
||
55 | 'T_DIR' => array( |
||
56 | '5.2' => false, |
||
57 | '5.3' => true, |
||
58 | 'description' => '__DIR__ magic constant', |
||
59 | 'content' => '__DIR__', |
||
60 | ), |
||
61 | 'T_GOTO' => array( |
||
62 | '5.2' => false, |
||
63 | '5.3' => true, |
||
64 | 'description' => '"goto" keyword', |
||
65 | 'content' => 'goto', |
||
66 | ), |
||
67 | 'T_INSTEADOF' => array( |
||
68 | '5.3' => false, |
||
69 | '5.4' => true, |
||
70 | 'description' => '"insteadof" keyword (for traits)', |
||
71 | 'content' => 'insteadof', |
||
72 | ), |
||
73 | 'T_NAMESPACE' => array( |
||
74 | '5.2' => false, |
||
75 | '5.3' => true, |
||
76 | 'description' => '"namespace" keyword', |
||
77 | 'content' => 'namespace', |
||
78 | ), |
||
79 | 'T_NS_C' => array( |
||
80 | '5.2' => false, |
||
81 | '5.3' => true, |
||
82 | 'description' => '__NAMESPACE__ magic constant', |
||
83 | 'content' => '__NAMESPACE__', |
||
84 | ), |
||
85 | 'T_USE' => array( |
||
86 | '5.2' => false, |
||
87 | '5.3' => true, |
||
88 | 'description' => '"use" keyword (for traits/namespaces/anonymous functions)', |
||
89 | ), |
||
90 | 'T_TRAIT' => array( |
||
91 | '5.3' => false, |
||
92 | '5.4' => true, |
||
93 | 'description' => '"trait" keyword', |
||
94 | 'content' => 'trait', |
||
95 | ), |
||
96 | 'T_TRAIT_C' => array( |
||
97 | '5.3' => false, |
||
98 | '5.4' => true, |
||
99 | 'description' => '__TRAIT__ magic constant', |
||
100 | 'content' => '__TRAIT__', |
||
101 | ), |
||
102 | // The specifics for distinguishing between 'yield' and 'yield from' are dealt |
||
103 | // with in the translation logic. |
||
104 | // This token has to be placed above the `T_YIELD` token in this array to allow for this. |
||
105 | 'T_YIELD_FROM' => array( |
||
106 | '5.6' => false, |
||
107 | '7.0' => true, |
||
108 | 'description' => '"yield from" keyword (for generators)', |
||
109 | 'content' => 'yield', |
||
110 | ), |
||
111 | 'T_YIELD' => array( |
||
112 | '5.4' => false, |
||
113 | '5.5' => true, |
||
114 | 'description' => '"yield" keyword (for generators)', |
||
115 | 'content' => 'yield', |
||
116 | ), |
||
117 | 'T_FINALLY' => array( |
||
118 | '5.4' => false, |
||
119 | '5.5' => true, |
||
120 | 'description' => '"finally" keyword (in exception handling)', |
||
121 | 'content' => 'finally', |
||
122 | ), |
||
123 | ); |
||
124 | |||
125 | /** |
||
126 | * Translation table for T_STRING tokens. |
||
127 | * |
||
128 | * Will be set up from the register() method. |
||
129 | * |
||
130 | * @var array(string => string) |
||
131 | */ |
||
132 | protected $translateContentToToken = array(); |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Returns an array of tokens this test wants to listen for. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | public function register() |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Processes this test, when one of its tokens is encountered. |
||
169 | * |
||
170 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
171 | * @param int $stackPtr The position of the current token in |
||
172 | * the stack passed in $tokens. |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
262 | * |
||
263 | * @param array $itemInfo Base information about the item. |
||
264 | * |
||
265 | * @return array Version and other information about the item. |
||
266 | */ |
||
267 | public function getItemArray(array $itemInfo) |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function getNonVersionArrayKeys() |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Retrieve the relevant detail (version) information for use in an error message. |
||
290 | * |
||
291 | * @param array $itemArray Version and other information about the item. |
||
292 | * @param array $itemInfo Base information about the item. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
304 | |||
305 | |||
306 | /** |
||
307 | * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
||
308 | * |
||
309 | * @param array $data The error data array which was created. |
||
310 | * @param array $itemInfo Base information about the item this error message applied to. |
||
311 | * @param array $errorInfo Detail information about an item this error message applied to. |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
||
320 | |||
321 | |||
322 | }//end class |
||
323 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.