-
Notifications
You must be signed in to change notification settings - Fork 160
Created Sniff to validate function without argument #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created Sniff to validate function without argument #325
Conversation
2778553
to
1a9b4a4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request @anzin , please see my comments
if ($testFile === 'DeprecatedFunctionUnitTest.inc') { | ||
return [ | ||
23 => 1 | ||
]; | ||
} | ||
|
||
return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be simplified considering there is only one test file?
if ($testFile === 'DeprecatedFunctionUnitTest.inc') { | |
return [ | |
23 => 1 | |
]; | |
} | |
return []; | |
return [ | |
23 => 1 | |
]; |
* @var array | ||
*/ | ||
private $deprecatedFunctions = [ | ||
'mb_check_encoding' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the following functions should be added to the list:
- get_class
- get_parent_class
- get_called_class
According to https://wiki.php.net/rfc/deprecations_php_8_1
/** | ||
* Sniff to validate PHP deprecated function. | ||
*/ | ||
class DeprecatedFunctionSniff implements Sniff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the following name would better describe the purpose of this sniff
class DeprecatedFunctionSniff implements Sniff | |
class FunctionsDeprecatedWithoutArgumentSniff implements Sniff |
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Sniff to validate PHP deprecated function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Sniff to validate PHP deprecated function. | |
* Sniff to validate PHP functions usage of which without passing arguments is deprecated. |
|
||
if (!$argumentString) { | ||
$warningMessage = sprintf( | ||
'Calling function %s() without argument is deprecated in PHP 8.1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please provide a suggestion on how to fix this issue in the message?
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good approach for phpcs tests (that also matches the design of this static testing library) is utilizing the iteration that is already done by the library instead of registering T_OPEN_TAG to process every file and performing an additional custom parsing/iteration of the file.
Please see an example of the implementation where each invoked relevant token is analyzed:
/**
* Deprecated functions without argument.
*
* @var array
*/
private $deprecatedFunctions = [
'mb_check_encoding' => 'Calling function mb_check_encoding() without argument is deprecated in PHP 8.1. '
. 'Please pass the input to check for encoding as the first argument of the function.'
];
/**
* @inheritdoc
*/
public function register(): array
{
return [
T_OPEN_PARENTHESIS
];
}
/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr): void
{
$closeParenthesisPtr = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr);
$arguments = trim($phpcsFile->getTokensAsString($stackPtr + 1, $closeParenthesisPtr - $stackPtr - 1));
if ($arguments) {
return;
}
$functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1);
if (isset($this->deprecatedFunctions[$functionName])) {
$phpcsFile->addWarning($this->deprecatedFunctions[$functionName], $stackPtr, 'FunctionDeprecatedWithoutArguments');
}
}
ac3333f
to
c895716
Compare
Hello @sivaschenko , thanks for your review, I fixed code by you recommendation, please check if every think fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update @anzin ! The implementation looks great!
The final thing that I'd recommend to improve the sniff is using constants instead of variables for the values that are not supposed to be changed/updated in runtime.
* | ||
* @var array | ||
*/ | ||
private $deprecatedFunctions = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private $deprecatedFunctions = [ | |
private const FUNCTIONS_LIST = [ |
* | ||
* @var string | ||
*/ | ||
private $warningCode = 'FunctionsDeprecatedWithoutArgument'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private $warningCode = 'FunctionsDeprecatedWithoutArgument'; | |
private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument'; |
* | ||
* @var string | ||
*/ | ||
private $warningMessage = 'Calling function %s() without argument is deprecated in PHP 8.1. ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private $warningMessage = 'Calling function %s() without argument is deprecated in PHP 8.1. ' | |
private const WARNING_MESSAGE = 'Calling function %s() without argument is deprecated in PHP 8.1. ' |
|
||
$functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1); | ||
|
||
if (in_array($functionName, $this->deprecatedFunctions)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (in_array($functionName, $this->deprecatedFunctions)) { | |
if (in_array($functionName, self::FUNCTIONS_LIST)) { |
$functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1); | ||
|
||
if (in_array($functionName, $this->deprecatedFunctions)) { | ||
$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode); | |
$phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE); |
Hello @sivaschenko, I fixed code by you recommendation, please check if every think fine. |
@magento import pr to magento-commerce/magento-coding-standard |
@sivaschenko an error occurred during the Pull Request import. |
Description (*)
Added sniff for check deprecated function without argument.
Addresses backward incompatible change in PHP 8.1: https://wiki.php.net/rfc/deprecations_php_8_1
Fixed Issues (if relevant)
Magento2.Functions.DeprecatedFunction
intomagento-coding-standard
magento2#34547Contribution checklist (*)