Skip to content

Commit feea2ea

Browse files
committed
Add autofixing
1 parent 4c8716c commit feea2ea

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class FunctionsDeprecatedWithoutArgumentSniff implements Sniff
3131
private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument';
3232

3333
/**
34-
* Deprecated functions without argument.
34+
* Deprecated functions without argument
3535
*
3636
* @var array
3737
*/
38-
private const FUNCTIONS_LIST = [
39-
'mb_check_encoding',
40-
'get_class',
41-
'get_parent_class',
42-
'get_called_class'
38+
private const DEPRECATED_FUNCTIONS_AND_FIXES = [
39+
'mb_check_encoding' => 'null',
40+
'get_class' => '$this',
41+
'get_parent_class' => '$this',
42+
'get_called_class' => '$this'
4343
];
4444

4545
/**
@@ -66,8 +66,17 @@ public function process(File $phpcsFile, $stackPtr): void
6666

6767
$functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1);
6868

69-
if (in_array($functionName, self::FUNCTIONS_LIST)) {
70-
$phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE);
69+
if (in_array($functionName, array_keys(self::DEPRECATED_FUNCTIONS_AND_FIXES))) {
70+
if ($phpcsFile->addFixableWarning(
71+
sprintf(self::WARNING_MESSAGE, $functionName),
72+
$stackPtr,
73+
self::WARNING_CODE
74+
) === true) {
75+
$content = self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName];
76+
$phpcsFile->fixer->beginChangeset();
77+
$phpcsFile->fixer->addContentBefore($phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr), $content);
78+
$phpcsFile->fixer->endChangeset();
79+
}
7180
}
7281
}
7382
}
Lines changed: 33 additions & 0 deletions
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(null), // Calling without argument is deprecated.
24+
mb_check_encoding('test-argument', null),
25+
get_class($this), // Calling without argument is deprecated.
26+
get_class(new FunctionsDeprecatedWithoutArgument()),
27+
get_parent_class($this), // Calling without argument is deprecated.
28+
get_parent_class('test-argument'),
29+
get_called_class($this), // Calling without argument is deprecated.
30+
get_called_class('test-argument')
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)