From 1a9b4a48285cc913e648d8f6c8b877e51b87fde8 Mon Sep 17 00:00:00 2001 From: "andrii.zinkevych" Date: Fri, 5 Nov 2021 11:14:46 +0100 Subject: [PATCH 1/3] Created Sniff to validate function without argument --- .../Functions/DeprecatedFunctionSniff.php | 64 +++++++++++++++++++ .../Functions/DeprecatedFunctionUnitTest.inc | 32 ++++++++++ .../Functions/DeprecatedFunctionUnitTest.php | 38 +++++++++++ Magento2/ruleset.xml | 5 ++ 4 files changed, 139 insertions(+) create mode 100644 Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php create mode 100644 Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc create mode 100644 Magento2/Tests/Functions/DeprecatedFunctionUnitTest.php diff --git a/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php b/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php new file mode 100644 index 00000000..c573b8e5 --- /dev/null +++ b/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php @@ -0,0 +1,64 @@ +getTokens(); + + foreach ($this->deprecatedFunctions as $deprecatedFunction) { + $deprecatedFunctionKeys = array_keys(array_column($tokens, 'content'), $deprecatedFunction); + + foreach ($deprecatedFunctionKeys as $deprecatedFunctionKey) { + $openParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $deprecatedFunctionKey); + $closeParenthesis = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $openParenthesis); + $argumentString = trim( + $phpcsFile->getTokensAsString($openParenthesis + 1, $closeParenthesis - $openParenthesis - 1) + ); + + if (!$argumentString) { + $warningMessage = sprintf( + 'Calling function %s() without argument is deprecated in PHP 8.1', + $deprecatedFunction + ); + $phpcsFile->addWarning($warningMessage, $deprecatedFunctionKey, 'DeprecatedFunction'); + } + } + } + } +} diff --git a/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc b/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc new file mode 100644 index 00000000..08465f2a --- /dev/null +++ b/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc @@ -0,0 +1,32 @@ + 1 + ]; + } + + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 5ea68ff3..1a19e962 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -251,6 +251,11 @@ *Test.php */tests/* + + 7 + warning + *\.xml$ + 8 warning From c8957167a3c4cfbc3de08a1311145253e2e76e78 Mon Sep 17 00:00:00 2001 From: "andrii.zinkevych" Date: Fri, 5 Nov 2021 14:51:57 +0100 Subject: [PATCH 2/3] Fixed code by recommendation --- .../Functions/DeprecatedFunctionSniff.php | 64 ---------------- ...unctionsDeprecatedWithoutArgumentSniff.php | 73 +++++++++++++++++++ .../Functions/DeprecatedFunctionUnitTest.inc | 32 -------- ...tionsDeprecatedWithoutArgumentUnitTest.inc | 33 +++++++++ ...ionsDeprecatedWithoutArgumentUnitTest.php} | 19 +++-- Magento2/ruleset.xml | 2 +- 6 files changed, 116 insertions(+), 107 deletions(-) delete mode 100644 Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php create mode 100644 Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php delete mode 100644 Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc create mode 100644 Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc rename Magento2/Tests/Functions/{DeprecatedFunctionUnitTest.php => FunctionsDeprecatedWithoutArgumentUnitTest.php} (55%) diff --git a/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php b/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php deleted file mode 100644 index c573b8e5..00000000 --- a/Magento2/Sniffs/Functions/DeprecatedFunctionSniff.php +++ /dev/null @@ -1,64 +0,0 @@ -getTokens(); - - foreach ($this->deprecatedFunctions as $deprecatedFunction) { - $deprecatedFunctionKeys = array_keys(array_column($tokens, 'content'), $deprecatedFunction); - - foreach ($deprecatedFunctionKeys as $deprecatedFunctionKey) { - $openParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $deprecatedFunctionKey); - $closeParenthesis = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $openParenthesis); - $argumentString = trim( - $phpcsFile->getTokensAsString($openParenthesis + 1, $closeParenthesis - $openParenthesis - 1) - ); - - if (!$argumentString) { - $warningMessage = sprintf( - 'Calling function %s() without argument is deprecated in PHP 8.1', - $deprecatedFunction - ); - $phpcsFile->addWarning($warningMessage, $deprecatedFunctionKey, 'DeprecatedFunction'); - } - } - } - } -} diff --git a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php new file mode 100644 index 00000000..13d53fc3 --- /dev/null +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -0,0 +1,73 @@ +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 (in_array($functionName, $this->deprecatedFunctions)) { + $phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode); + } + } +} diff --git a/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc b/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc deleted file mode 100644 index 08465f2a..00000000 --- a/Magento2/Tests/Functions/DeprecatedFunctionUnitTest.inc +++ /dev/null @@ -1,32 +0,0 @@ - 1 - ]; - } - - return []; + return [ + 23 => 1, + 25 => 1, + 27 => 1, + 29 => 1 + ]; } } diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 1a19e962..12114bdb 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -251,7 +251,7 @@ *Test.php */tests/* - + 7 warning *\.xml$ From dc7c3f4ff211aaa150d4e6e3bb46b59c62328f6f Mon Sep 17 00:00:00 2001 From: "andrii.zinkevych" Date: Sun, 7 Nov 2021 17:36:44 +0100 Subject: [PATCH 3/3] Fixed code by recommendation, added constants --- .../FunctionsDeprecatedWithoutArgumentSniff.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php index 13d53fc3..331e918e 100644 --- a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -20,7 +20,7 @@ class FunctionsDeprecatedWithoutArgumentSniff implements Sniff * * @var string */ - 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. ' . 'Please pass the input to validate as the first argument of the function.'; /** @@ -28,14 +28,14 @@ class FunctionsDeprecatedWithoutArgumentSniff implements Sniff * * @var string */ - private $warningCode = 'FunctionsDeprecatedWithoutArgument'; + private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument'; /** * Deprecated functions without argument. * * @var array */ - private $deprecatedFunctions = [ + private const FUNCTIONS_LIST = [ 'mb_check_encoding', 'get_class', 'get_parent_class', @@ -66,8 +66,8 @@ public function process(File $phpcsFile, $stackPtr): void $functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1); - if (in_array($functionName, $this->deprecatedFunctions)) { - $phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode); + if (in_array($functionName, self::FUNCTIONS_LIST)) { + $phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE); } } }