6
6
7
7
namespace Magento2 \Sniffs \PHPCompatibility ;
8
8
9
+ use Magento2 \Helpers \Assert ;
9
10
use PHP_CodeSniffer \Files \File ;
10
11
use PHPCompatibility \Sniff ;
11
12
use PHP_CodeSniffer_Tokens as Tokens ;
19
20
*/
20
21
class DeprecatedEncodingsForMBStringFunctionsSniff extends Sniff
21
22
{
22
- /**
23
- * List of tokens which when they precede the $stackPtr indicate that this
24
- * is not a function call.
25
- *
26
- * @var array
27
- */
28
- private $ ignoreTokens = [
29
- \T_DOUBLE_COLON => true ,
30
- \T_OBJECT_OPERATOR => true ,
31
- \T_FUNCTION => true ,
32
- \T_NEW => true ,
33
- \T_CONST => true ,
34
- \T_USE => true ,
35
- ];
36
-
37
- /**
38
- * Text string tokens to examine.
39
- *
40
- * @var array
41
- */
42
- private $ textStringTokens = [
43
- \T_CONSTANT_ENCAPSED_STRING => true ,
44
- \T_DOUBLE_QUOTED_STRING => true ,
45
- \T_INLINE_HTML => true ,
46
- \T_HEREDOC => true ,
47
- \T_NOWDOC => true ,
48
- ];
49
-
50
23
/**
51
24
* A list of MBString functions that emits deprecation notice
52
25
*
53
26
* @var \int[][]
54
27
*/
55
- protected $ targetFunctions = [
28
+ private $ targetFunctions = [
56
29
'mb_check_encoding ' => [2 ],
57
30
'mb_chr ' => [2 ],
58
31
'mb_convert_case ' => [3 ],
@@ -124,21 +97,7 @@ public function process(File $phpcsFile, $stackPtr)
124
97
$ function = $ tokens [$ stackPtr ]['content ' ];
125
98
$ functionLc = strtolower ($ function );
126
99
127
- if (strpos ($ functionLc , 'mb_ ' ) !== 0 ) {
128
- return ;
129
- }
130
-
131
- $ prevNonEmpty = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ stackPtr - 1 ), null , true );
132
-
133
- if (isset ($ this ->ignoreTokens [$ tokens [$ prevNonEmpty ]['code ' ]]) === true ) {
134
- // Not a call to a PHP function.
135
- return ;
136
- }
137
-
138
- if ($ tokens [$ prevNonEmpty ]['code ' ] === \T_NS_SEPARATOR
139
- && $ tokens [$ prevNonEmpty - 1 ]['code ' ] === \T_STRING
140
- ) {
141
- // Namespaced function.
100
+ if (!isset ($ this ->targetFunctions [$ functionLc ]) || !Assert::isBuiltinFunctionCall ($ phpcsFile , $ stackPtr )) {
142
101
return ;
143
102
}
144
103
@@ -161,7 +120,7 @@ public function process(File $phpcsFile, $stackPtr)
161
120
public function processParameter (File $ phpcsFile , string $ function , int $ index , array $ parameter )
162
121
{
163
122
$ functionLc = strtolower ($ function );
164
- if (!isset ( $ this -> targetFunctions [ $ functionLc ]) || ! in_array ($ index , $ this ->targetFunctions [$ functionLc ])) {
123
+ if (!in_array ($ index , $ this ->targetFunctions [$ functionLc ])) {
165
124
return ;
166
125
}
167
126
@@ -175,6 +134,14 @@ public function processParameter(File $phpcsFile, string $function, int $index,
175
134
$ items = [$ parameter ];
176
135
}
177
136
137
+ $ textStringTokens = [
138
+ \T_CONSTANT_ENCAPSED_STRING => true ,
139
+ \T_DOUBLE_QUOTED_STRING => true ,
140
+ \T_INLINE_HTML => true ,
141
+ \T_HEREDOC => true ,
142
+ \T_NOWDOC => true ,
143
+ ];
144
+
178
145
foreach ($ items as $ item ) {
179
146
for ($ i = $ item ['start ' ]; $ i <= $ item ['end ' ]; $ i ++) {
180
147
if ($ tokens [$ i ]['code ' ] === \T_STRING
@@ -184,7 +151,7 @@ public function processParameter(File $phpcsFile, string $function, int $index,
184
151
break ;
185
152
}
186
153
187
- if (isset ($ this -> textStringTokens [$ tokens [$ i ]['code ' ]]) === true ) {
154
+ if (isset ($ textStringTokens [$ tokens [$ i ]['code ' ]]) === true ) {
188
155
$ encoding = $ this ->stripQuotes (strtolower (trim ($ tokens [$ i ]['content ' ])));
189
156
$ encodings = array_flip (explode (', ' , $ encoding ));
190
157
if (count (array_intersect_key ($ encodings , $ this ->messages )) > 0 ) {
0 commit comments