66
77namespace Magento2 \Sniffs \PHPCompatibility ;
88
9+ use Magento2 \Helpers \Assert ;
910use PHP_CodeSniffer \Files \File ;
1011use PHPCompatibility \Sniff ;
1112use PHP_CodeSniffer_Tokens as Tokens ;
1920 */
2021class DeprecatedEncodingsForMBStringFunctionsSniff extends Sniff
2122{
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-
5023 /**
5124 * A list of MBString functions that emits deprecation notice
5225 *
5326 * @var \int[][]
5427 */
55- protected $ targetFunctions = [
28+ private $ targetFunctions = [
5629 'mb_check_encoding ' => [2 ],
5730 'mb_chr ' => [2 ],
5831 'mb_convert_case ' => [3 ],
@@ -124,21 +97,7 @@ public function process(File $phpcsFile, $stackPtr)
12497 $ function = $ tokens [$ stackPtr ]['content ' ];
12598 $ functionLc = strtolower ($ function );
12699
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 )) {
142101 return ;
143102 }
144103
@@ -161,7 +120,7 @@ public function process(File $phpcsFile, $stackPtr)
161120 public function processParameter (File $ phpcsFile , string $ function , int $ index , array $ parameter )
162121 {
163122 $ functionLc = strtolower ($ function );
164- if (!isset ( $ this -> targetFunctions [ $ functionLc ]) || ! in_array ($ index , $ this ->targetFunctions [$ functionLc ])) {
123+ if (!in_array ($ index , $ this ->targetFunctions [$ functionLc ])) {
165124 return ;
166125 }
167126
@@ -175,6 +134,14 @@ public function processParameter(File $phpcsFile, string $function, int $index,
175134 $ items = [$ parameter ];
176135 }
177136
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+
178145 foreach ($ items as $ item ) {
179146 for ($ i = $ item ['start ' ]; $ i <= $ item ['end ' ]; $ i ++) {
180147 if ($ tokens [$ i ]['code ' ] === \T_STRING
@@ -184,7 +151,7 @@ public function processParameter(File $phpcsFile, string $function, int $index,
184151 break ;
185152 }
186153
187- if (isset ($ this -> textStringTokens [$ tokens [$ i ]['code ' ]]) === true ) {
154+ if (isset ($ textStringTokens [$ tokens [$ i ]['code ' ]]) === true ) {
188155 $ encoding = $ this ->stripQuotes (strtolower (trim ($ tokens [$ i ]['content ' ])));
189156 $ encodings = array_flip (explode (', ' , $ encoding ));
190157 if (count (array_intersect_key ($ encodings , $ this ->messages )) > 0 ) {
0 commit comments