Skip to content

Commit 837e223

Browse files
authored
Merge pull request #118 from magento-commerce/improvements/sniff-to-check-php-deprecated-function
Created Sniff to validate function without argument
2 parents 31e77f9 + dc7c3f4 commit 837e223

4 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 functions usage of which without passing arguments is deprecated.
15+
*/
16+
class FunctionsDeprecatedWithoutArgumentSniff implements Sniff
17+
{
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
private const WARNING_MESSAGE = 'Calling function %s() without argument is deprecated in PHP 8.1. '
24+
. 'Please pass the input to validate as the first argument of the function.';
25+
26+
/**
27+
* Warning violation code.
28+
*
29+
* @var string
30+
*/
31+
private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument';
32+
33+
/**
34+
* Deprecated functions without argument.
35+
*
36+
* @var array
37+
*/
38+
private const FUNCTIONS_LIST = [
39+
'mb_check_encoding',
40+
'get_class',
41+
'get_parent_class',
42+
'get_called_class'
43+
];
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function register(): array
49+
{
50+
return [
51+
T_OPEN_PARENTHESIS
52+
];
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function process(File $phpcsFile, $stackPtr): void
59+
{
60+
$closeParenthesisPtr = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr);
61+
$arguments = trim($phpcsFile->getTokensAsString($stackPtr + 1, $closeParenthesisPtr - $stackPtr - 1));
62+
63+
if ($arguments) {
64+
return;
65+
}
66+
67+
$functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1);
68+
69+
if (in_array($functionName, self::FUNCTIONS_LIST)) {
70+
$phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE);
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 validate PHP functions usage of which without passing arguments.
12+
*/
13+
class FunctionsDeprecatedWithoutArgument
14+
{
15+
/**
16+
* Test deprecation function.
17+
*
18+
* @return array
19+
*/
20+
public function testDeprecatedMethod(): array
21+
{
22+
return [
23+
mb_check_encoding(), // Calling without argument is deprecated.
24+
mb_check_encoding('test-argument', null),
25+
get_class(), // Calling without argument is deprecated.
26+
get_class(new FunctionsDeprecatedWithoutArgument()),
27+
get_parent_class(), // Calling without argument is deprecated.
28+
get_parent_class('test-argument'),
29+
get_called_class(), // Calling without argument is deprecated.
30+
get_called_class('test-argument')
31+
];
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Test to validate PHP functions usage of which without passing arguments.
14+
*/
15+
class FunctionsDeprecatedWithoutArgumentUnitTest 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(): array
29+
{
30+
return [
31+
23 => 1,
32+
25 => 1,
33+
27 => 1,
34+
29 => 1
35+
];
36+
}
37+
}

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.FunctionsDeprecatedWithoutArgument">
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)