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