|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Wim Godden <[email protected]> |
|
10
|
|
|
* @copyright 2013 Cu.be Solutions bvba |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff. |
|
15
|
|
|
* |
|
16
|
|
|
* @category PHP |
|
17
|
|
|
* @package PHPCompatibility |
|
18
|
|
|
* @author Wim Godden <[email protected]> |
|
19
|
|
|
* @version 1.0.0 |
|
20
|
|
|
* @copyright 2013 Cu.be Solutions bvba |
|
21
|
|
|
*/ |
|
22
|
|
|
class PHPCompatibility_Sniffs_PHP_NewLanguageConstructsSniff extends PHPCompatibility_Sniff |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A list of new language constructs, not present in older versions. |
|
27
|
|
|
* |
|
28
|
|
|
* The array lists : version number with false (not present) or true (present). |
|
29
|
|
|
* If's sufficient to list the first version where the keyword appears. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array(string => array(string => int|string|null)) |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $newConstructs = array( |
|
34
|
|
|
'T_NS_SEPARATOR' => array( |
|
35
|
|
|
'5.2' => false, |
|
36
|
|
|
'5.3' => true, |
|
37
|
|
|
'description' => 'the \ operator (for namespaces)' |
|
38
|
|
|
), |
|
39
|
|
|
'T_POW' => array( |
|
40
|
|
|
'5.5' => false, |
|
41
|
|
|
'5.6' => true, |
|
42
|
|
|
'description' => 'power operator (**)' |
|
43
|
|
|
), // identified in PHPCS 1.5 as T_MULTIPLY + T_MULTIPLY |
|
44
|
|
|
'T_POW_EQUAL' => array( |
|
45
|
|
|
'5.5' => false, |
|
46
|
|
|
'5.6' => true, |
|
47
|
|
|
'description' => 'power assignment operator (**=)' |
|
48
|
|
|
), // identified in PHPCS 1.5 as T_MULTIPLY + T_MUL_EQUAL |
|
49
|
|
|
'T_ELLIPSIS' => array( |
|
50
|
|
|
'5.5' => false, |
|
51
|
|
|
'5.6' => true, |
|
52
|
|
|
'description' => 'variadic functions using ...' |
|
53
|
|
|
), |
|
54
|
|
|
'T_SPACESHIP' => array( |
|
55
|
|
|
'5.6' => false, |
|
56
|
|
|
'7.0' => true, |
|
57
|
|
|
'description' => 'spaceship operator (<=>)' |
|
58
|
|
|
), // identified in PHPCS 1.5 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN |
|
59
|
|
|
'T_COALESCE' => array( |
|
60
|
|
|
'5.6' => false, |
|
61
|
|
|
'7.0' => true, |
|
62
|
|
|
'description' => 'null coalescing operator (??)' |
|
63
|
|
|
), // identified in PHPCS 1.5 as T_INLINE_THEN + T_INLINE_THEN |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* A list of new language constructs which are not recognized in PHPCS 1.x. |
|
69
|
|
|
* |
|
70
|
|
|
* The array lists an alternative token to listen for. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array(string => int) |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $newConstructsPHPCSCompat = array( |
|
75
|
|
|
'T_POW' => T_MULTIPLY, |
|
76
|
|
|
'T_POW_EQUAL' => T_MUL_EQUAL, |
|
77
|
|
|
'T_SPACESHIP' => T_GREATER_THAN, |
|
78
|
|
|
'T_COALESCE' => T_INLINE_THEN, |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Translation table for PHPCS 1.x tokens. |
|
83
|
|
|
* |
|
84
|
|
|
* The 'before' index lists the token which would have to be directly before the |
|
85
|
|
|
* token found for it to be one of the new language constructs. |
|
86
|
|
|
* The 'real_token' index indicates which language construct was found in that case. |
|
87
|
|
|
* |
|
88
|
|
|
* {@internal 'before' was choosen rather than 'after' as that allowed for a 1-on-1 |
|
89
|
|
|
* translation list with the current tokens.}} |
|
90
|
|
|
* |
|
91
|
|
|
* @var array(string => array(string => string)) |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $PHPCSCompatTranslate = array( |
|
94
|
|
|
'T_MULTIPLY' => array( |
|
95
|
|
|
'before' => 'T_MULTIPLY', |
|
96
|
|
|
'real_token' => 'T_POW', |
|
97
|
|
|
), |
|
98
|
|
|
'T_MUL_EQUAL' => array( |
|
99
|
|
|
'before' => 'T_MULTIPLY', |
|
100
|
|
|
'real_token' => 'T_POW_EQUAL', |
|
101
|
|
|
), |
|
102
|
|
|
'T_GREATER_THAN' => array( |
|
103
|
|
|
'before' => 'T_IS_SMALLER_OR_EQUAL', |
|
104
|
|
|
'real_token' => 'T_SPACESHIP', |
|
105
|
|
|
), |
|
106
|
|
|
'T_INLINE_THEN' => array( |
|
107
|
|
|
'before' => 'T_INLINE_THEN', |
|
108
|
|
|
'real_token' => 'T_COALESCE', |
|
109
|
|
|
), |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function register() |
|
118
|
|
|
{ |
|
119
|
|
|
$tokens = array(); |
|
120
|
|
|
foreach ($this->newConstructs as $token => $versions) { |
|
121
|
|
|
if (defined($token)) { |
|
122
|
|
|
$tokens[] = constant($token); |
|
123
|
|
|
} |
|
124
|
|
|
else if(isset($this->newConstructsPHPCSCompat[$token])) { |
|
125
|
|
|
$tokens[] = $this->newConstructsPHPCSCompat[$token]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return $tokens; |
|
129
|
|
|
}//end register() |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
134
|
|
|
* |
|
135
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
136
|
|
|
* @param int $stackPtr The position of the current token in |
|
137
|
|
|
* the stack passed in $tokens. |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
142
|
|
|
{ |
|
143
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
144
|
|
|
$tokenType = $tokens[$stackPtr]['type']; |
|
145
|
|
|
|
|
146
|
|
|
// Translate pre-PHPCS 2.0 tokens for new constructs to the actual construct. |
|
147
|
|
|
if (isset($this->newConstructs[$tokenType]) === false) { |
|
148
|
|
|
if ( |
|
149
|
|
|
isset($this->PHPCSCompatTranslate[$tokenType]) |
|
150
|
|
|
&& |
|
151
|
|
|
$tokens[$stackPtr - 1]['type'] === $this->PHPCSCompatTranslate[$tokenType]['before'] |
|
152
|
|
|
) { |
|
153
|
|
|
$tokenType = $this->PHPCSCompatTranslate[$tokenType]['real_token']; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// If the translation did not yield one of the tokens we are looking for, bow out. |
|
158
|
|
|
if (isset($this->newConstructs[$tokenType]) === false) { |
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->addError($phpcsFile, $stackPtr, $tokenType); |
|
163
|
|
|
|
|
164
|
|
|
}//end process() |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Generates the error or warning for this sniff. |
|
169
|
|
|
* |
|
170
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
171
|
|
|
* @param int $stackPtr The position of the function |
|
172
|
|
|
* in the token array. |
|
173
|
|
|
* @param string $keywordName The name of the keyword. |
|
174
|
|
|
* |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function addError($phpcsFile, $stackPtr, $keywordName) |
|
178
|
|
|
{ |
|
179
|
|
|
$error = ''; |
|
180
|
|
|
|
|
181
|
|
|
$isError = false; |
|
182
|
|
|
foreach ($this->newConstructs[$keywordName] as $version => $present) { |
|
183
|
|
|
if ($this->supportsBelow($version)) { |
|
184
|
|
|
if ($present === false) { |
|
185
|
|
|
$isError = true; |
|
186
|
|
|
$error .= 'not present in PHP version ' . $version . ' or earlier'; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
if (strlen($error) > 0) { |
|
191
|
|
|
$error = $this->newConstructs[$keywordName]['description'] . ' is ' . $error; |
|
192
|
|
|
|
|
193
|
|
|
if ($isError === true) { |
|
194
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
195
|
|
|
} else { |
|
196
|
|
|
$phpcsFile->addWarning($error, $stackPtr); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
}//end addError() |
|
201
|
|
|
|
|
202
|
|
|
}//end class |
|
203
|
|
|
|
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.