|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewTypeCastsSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* @category PHP |
|
6
|
|
|
* @package PHPCompatibility |
|
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
|
11
|
|
|
|
|
12
|
|
|
use PHPCompatibility\AbstractNewFeatureSniff; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewTypeCastsSniff. |
|
16
|
|
|
* |
|
17
|
|
|
* @category PHP |
|
18
|
|
|
* @package PHPCompatibility |
|
19
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class NewTypeCastsSniff extends AbstractNewFeatureSniff |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A list of new type casts, not present in older versions. |
|
26
|
|
|
* |
|
27
|
|
|
* The array lists : version number with false (not present) or true (present). |
|
28
|
|
|
* If's sufficient to list the first version where the keyword appears. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array(string => array(string => int|string|null)) |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $newTypeCasts = array( |
|
33
|
|
|
'T_UNSET_CAST' => array( |
|
34
|
|
|
'4.4' => false, |
|
35
|
|
|
'5.0' => true, |
|
36
|
|
|
'description' => 'The unset cast', |
|
37
|
|
|
), |
|
38
|
|
|
'T_BINARY_CAST' => array( |
|
39
|
|
|
'5.2.0' => false, |
|
40
|
|
|
'5.2.1' => true, |
|
41
|
|
|
'description' => 'The binary cast', |
|
42
|
|
|
), |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function register() |
|
52
|
|
|
{ |
|
53
|
|
|
$tokens = array(); |
|
54
|
|
|
foreach ($this->newTypeCasts as $token => $versions) { |
|
55
|
|
|
if (defined($token)) { |
|
56
|
|
|
$tokens[] = constant($token); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* Work around tokenizer issues. |
|
62
|
|
|
* |
|
63
|
|
|
* - (binary) cast is incorrectly tokenized as T_STRING_CAST by PHP and PHPCS. |
|
64
|
|
|
* - b"something" binary cast is incorrectly tokenized as T_CONSTANT_ENCAPSED_STRING by PHP and PHPCS. |
|
65
|
|
|
* |
|
66
|
|
|
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1574 |
|
67
|
|
|
*/ |
|
68
|
|
|
$tokens[] = T_STRING_CAST; |
|
69
|
|
|
$tokens[] = T_CONSTANT_ENCAPSED_STRING; |
|
70
|
|
|
|
|
71
|
|
|
return $tokens; |
|
72
|
|
|
}//end register() |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
79
|
|
|
* @param int $stackPtr The position of the current token in |
|
80
|
|
|
* the stack passed in $tokens. |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
85
|
|
|
{ |
|
86
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
87
|
|
|
$tokenType = $tokens[$stackPtr]['type']; |
|
88
|
|
|
|
|
89
|
|
|
// Detect incorrectly tokenized binary casts. |
|
90
|
|
|
if (isset($this->newTypeCasts[$tokenType]) === false) { |
|
91
|
|
|
$tokenContent = $tokens[$stackPtr]['content']; |
|
92
|
|
|
switch ($tokenType) { |
|
93
|
|
|
case 'T_STRING_CAST': |
|
94
|
|
|
if (preg_match('`^\(\s*binary\s*\)$`i', $tokenContent) !== 1) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$tokenType = 'T_BINARY_CAST'; |
|
99
|
|
|
break; |
|
100
|
|
|
case 'T_CONSTANT_ENCAPSED_STRING': |
|
101
|
|
|
if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') { |
|
102
|
|
|
$tokenType = 'T_BINARY_CAST'; |
|
103
|
|
|
} else { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
break; |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// If the translation did not yield one of the tokens we are looking for, bow out. |
|
112
|
|
|
if (isset($this->newTypeCasts[$tokenType]) === false) { |
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$itemInfo = array( |
|
117
|
|
|
'name' => $tokenType, |
|
118
|
|
|
'content' => $tokens[$stackPtr]['content'], |
|
119
|
|
|
); |
|
120
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
121
|
|
|
|
|
122
|
|
|
}//end process() |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $itemInfo Base information about the item. |
|
129
|
|
|
* |
|
130
|
|
|
* @return array Version and other information about the item. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getItemArray(array $itemInfo) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->newTypeCasts[$itemInfo['name']]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get an array of the non-PHP-version array keys used in a sub-array. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function getNonVersionArrayKeys() |
|
144
|
|
|
{ |
|
145
|
|
|
return array('description'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Retrieve the relevant detail (version) information for use in an error message. |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $itemArray Version and other information about the item. |
|
153
|
|
|
* @param array $itemInfo Base information about the item. |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getErrorInfo(array $itemArray, array $itemInfo) |
|
158
|
|
|
{ |
|
159
|
|
|
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
160
|
|
|
$errorInfo['description'] = $itemArray['description']; |
|
161
|
|
|
|
|
162
|
|
|
return $errorInfo; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Filter the error message before it's passed to PHPCS. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $error The error message which was created. |
|
170
|
|
|
* @param array $itemInfo Base information about the item this error message applied to. |
|
171
|
|
|
* @param array $errorInfo Detail information about an item this error message applied to. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function filterErrorMsg($error, array $itemInfo, array $errorInfo) |
|
176
|
|
|
{ |
|
177
|
|
|
return $error . '. Found: %s'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Filter the error data before it's passed to PHPCS. |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $data The error data array which was created. |
|
185
|
|
|
* @param array $itemInfo Base information about the item this error message applied to. |
|
186
|
|
|
* @param array $errorInfo Detail information about an item this error message applied to. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
191
|
|
|
{ |
|
192
|
|
|
$data[0] = $errorInfo['description']; |
|
193
|
|
|
$data[] = $itemInfo['content']; |
|
194
|
|
|
return $data; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
}//end class |
|
199
|
|
|
|