Skip to content

Commit 2778553

Browse files
committed
Created Sniff to validate function without argument
1 parent 7d40225 commit 2778553

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Sniffs\Functions;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
/**
14+
* Sniff to validate PHP deprecated function.
15+
*/
16+
class DeprecatedFunctionSniff implements Sniff
17+
{
18+
/**
19+
* Deprecated functions without argument.
20+
*
21+
* @var array
22+
*/
23+
private $deprecatedFunctions = [
24+
'mb_check_encoding'
25+
];
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function register(): array
31+
{
32+
return [
33+
T_OPEN_TAG
34+
];
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function process(File $phpcsFile, $stackPtr): void
41+
{
42+
$tokens = $phpcsFile->getTokens();
43+
44+
foreach ($this->deprecatedFunctions as $deprecatedFunction) {
45+
$deprecatedFunctionKeys = array_keys(array_column($tokens, 'content'), $deprecatedFunction);
46+
47+
foreach ($deprecatedFunctionKeys as $deprecatedFunctionKey) {
48+
$openParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $deprecatedFunctionKey);
49+
$closeParenthesis = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $openParenthesis);
50+
$argumentString = trim(
51+
$phpcsFile->getTokensAsString($openParenthesis + 1, $closeParenthesis - $openParenthesis - 1)
52+
);
53+
54+
if (!$argumentString) {
55+
$warningMessage = sprintf(
56+
'Calling function %s() without argument is deprecated in PHP 8.1',
57+
$deprecatedFunction
58+
);
59+
$phpcsFile->addWarning($warningMessage, $deprecatedFunctionKey, 'DeprecatedFunction');
60+
}
61+
}
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Tests\Functions;
9+
10+
/**
11+
* Class to test PHP deprecated function.
12+
*/
13+
class DeprecatedFunction
14+
{
15+
/**
16+
* Test deprecation function.
17+
*
18+
* @return bool
19+
*/
20+
public function testDeprecatedMethod(): bool
21+
{
22+
// Warning: function is deprecated without argument.
23+
$testMethodParam = mb_check_encoding();
24+
25+
if ($testMethodParam) {
26+
// The function is work correct.
27+
$testMethodParam = mb_check_encoding('test-argument', null);
28+
}
29+
30+
return $testMethodParam;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Tests\Functions;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Sniff to validate PHP deprecated function.
14+
*/
15+
class DeprecatedFunctionUnitTest extends AbstractSniffUnitTest
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function getErrorList(): array
21+
{
22+
return [];
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getWarningList($testFile = ''): array
29+
{
30+
if ($testFile === 'DeprecatedFunctionUnitTest.inc') {
31+
return [
32+
23 => 1
33+
];
34+
}
35+
36+
return [];
37+
}
38+
}

Magento2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@
251251
<exclude-pattern>*Test.php</exclude-pattern>
252252
<exclude-pattern>*/tests/*</exclude-pattern>
253253
</rule>
254+
<rule ref="Magento2.Functions.DeprecatedFunction">
255+
<severity>7</severity>
256+
<type>warning</type>
257+
<exclude-pattern>*\.xml$</exclude-pattern>
258+
</rule>
254259
<rule ref="Magento2.Namespaces.ImportsFromTestNamespace">
255260
<severity>8</severity>
256261
<type>warning</type>

0 commit comments

Comments
 (0)