Skip to content

Commit ac3333f

Browse files
committed
Fixed code by recommendation
1 parent 1a9b4a4 commit ac3333f

5 files changed

+115
-106
lines changed

Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php

-64
This file was deleted.
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 $warningMessage = '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 $warningCode = 'DeprecatedFunction';
32+
33+
/**
34+
* Deprecated functions without argument.
35+
*
36+
* @var array
37+
*/
38+
private $deprecatedFunctions = [
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, $this->deprecatedFunctions)) {
70+
$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode);
71+
}
72+
}
73+
}

Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc

-32
This file was deleted.
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+
}

Magento2/Tests/Functions/DeprecatedFunctionUnitTest.php renamed to Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1111

1212
/**
13-
* Sniff to validate PHP deprecated function.
13+
* Test to validate PHP functions usage of which without passing arguments.
1414
*/
15-
class DeprecatedFunctionUnitTest extends AbstractSniffUnitTest
15+
class FunctionsDeprecatedWithoutArgumentUnitTest extends AbstractSniffUnitTest
1616
{
1717
/**
1818
* @inheritdoc
@@ -25,14 +25,13 @@ public function getErrorList(): array
2525
/**
2626
* @inheritdoc
2727
*/
28-
public function getWarningList($testFile = ''): array
28+
public function getWarningList(): array
2929
{
30-
if ($testFile === 'DeprecatedFunctionUnitTest.inc') {
31-
return [
32-
23 => 1
33-
];
34-
}
35-
36-
return [];
30+
return [
31+
23 => 1,
32+
25 => 1,
33+
27 => 1,
34+
29 => 1
35+
];
3736
}
3837
}

0 commit comments

Comments
 (0)