Skip to content

Commit 6e5c5a7

Browse files
committed
ACP2E-1946: Move PHPCompatibility/PHPCompatibility sniffs into separate namespace
1 parent 6bd92be commit 6e5c5a7

File tree

9 files changed

+6837
-50
lines changed

9 files changed

+6837
-50
lines changed

Magento2/Helpers/Assert.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Helpers;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Util\Tokens;
12+
use PHPCSUtils\Tokens\Collections;
13+
14+
/**
15+
* phpcs:disable Magento2.Functions.StaticFunction.StaticFunction
16+
*/
17+
class Assert
18+
{
19+
/**
20+
* Checks whether the function call is a built-in function call.
21+
*
22+
* @param File $phpcsFile
23+
* @param int $stackPtr
24+
* @return bool
25+
*/
26+
public static function isBuiltinFunctionCall(File $phpcsFile, int $stackPtr): bool
27+
{
28+
$tokens = $phpcsFile->getTokens();
29+
$nextPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
30+
if ($nextPtr === false
31+
|| $tokens[$nextPtr]['code'] !== \T_OPEN_PARENTHESIS
32+
|| isset($tokens[$nextPtr]['parenthesis_owner'])
33+
) {
34+
return false;
35+
}
36+
37+
$prevPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
38+
$ignorePrevToken = [\T_NEW => true] + Collections::objectOperators();
39+
if (isset($ignorePrevToken[$tokens[$prevPtr]['code']])) {
40+
return false;
41+
}
42+
43+
if ($tokens[$prevPtr]['code'] === \T_NS_SEPARATOR) {
44+
$prevPrevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevPtr - 1), null, true);
45+
if ($tokens[$prevPrevToken]['code'] === \T_STRING || $tokens[$prevPrevToken]['code'] === \T_NAMESPACE
46+
) {
47+
return false;
48+
}
49+
}
50+
51+
return true;
52+
}
53+
}

Magento2/Sniffs/PHPCompatibility/DeprecatedEncodingsForMBStringFunctionsSniff.php

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento2\Sniffs\PHPCompatibility;
88

9+
use Magento2\Helpers\Assert;
910
use PHP_CodeSniffer\Files\File;
1011
use PHPCompatibility\Sniff;
1112
use PHP_CodeSniffer_Tokens as Tokens;
@@ -19,40 +20,12 @@
1920
*/
2021
class 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

Comments
 (0)