diff --git a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php index 331e918e..e410c4d9 100644 --- a/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php +++ b/Magento2/Sniffs/Functions/FunctionsDeprecatedWithoutArgumentSniff.php @@ -31,15 +31,14 @@ class FunctionsDeprecatedWithoutArgumentSniff implements Sniff private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument'; /** - * Deprecated functions without argument. + * Deprecated functions without argument https://wiki.php.net/rfc/deprecations_php_8_1 * * @var array */ - private const FUNCTIONS_LIST = [ - 'mb_check_encoding', - 'get_class', - 'get_parent_class', - 'get_called_class' + private const DEPRECATED_FUNCTIONS_AND_FIXES = [ + 'mb_check_encoding' => false, + 'get_class' => '$this', + 'get_parent_class' => '$this' ]; /** @@ -66,8 +65,30 @@ public function process(File $phpcsFile, $stackPtr): void $functionName = $phpcsFile->getTokensAsString($phpcsFile->findPrevious(T_STRING, $stackPtr), 1); - if (in_array($functionName, self::FUNCTIONS_LIST)) { - $phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE); + if (!isset(self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName])) { + return; + } + + if (self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName] === false) { + $phpcsFile->addWarning( + sprintf(self::WARNING_MESSAGE, $functionName), + $stackPtr, + self::WARNING_CODE + ); + return; + } + + $fix = $phpcsFile->addFixableWarning( + sprintf(self::WARNING_MESSAGE, $functionName), + $stackPtr, + self::WARNING_CODE + ); + + if ($fix === true) { + $content = self::DEPRECATED_FUNCTIONS_AND_FIXES[$functionName]; + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->addContentBefore($phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr), $content); + $phpcsFile->fixer->endChangeset(); } } } diff --git a/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc b/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc index 63b449b9..7bdfeb89 100644 --- a/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc +++ b/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc @@ -21,13 +21,11 @@ class FunctionsDeprecatedWithoutArgument { return [ mb_check_encoding(), // Calling without argument is deprecated. - mb_check_encoding('test-argument', null), + mb_check_encoding('test-argument'), get_class(), // Calling without argument is deprecated. get_class(new FunctionsDeprecatedWithoutArgument()), get_parent_class(), // Calling without argument is deprecated. - get_parent_class('test-argument'), - get_called_class(), // Calling without argument is deprecated. - get_called_class('test-argument') + get_parent_class(new FunctionsDeprecatedWithoutArgument()) ]; } } diff --git a/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc.fixed b/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc.fixed new file mode 100644 index 00000000..1f4f20d9 --- /dev/null +++ b/Magento2/Tests/Functions/FunctionsDeprecatedWithoutArgumentUnitTest.inc.fixed @@ -0,0 +1,31 @@ + 1, 25 => 1, - 27 => 1, - 29 => 1 + 27 => 1 ]; } } diff --git a/composer.json b/composer.json index aa896857..b1bb3527 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "AFL-3.0" ], "type": "phpcodesniffer-standard", - "version": "18", + "version": "19", "require": { "php": ">=7.3", "webonyx/graphql-php": "^14.9", diff --git a/composer.lock b/composer.lock index d38bc239..a6150286 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d7ee7044cc0bf8d83a035a641253afac", + "content-hash": "aa79ce9c9f280386c7881e40e49a59e4", "packages": [ { "name": "phpcompatibility/php-compatibility", @@ -402,6 +402,9 @@ "require": { "php": "^7.1 || ^8.0" }, + "replace": { + "myclabs/deep-copy": "self.version" + }, "require-dev": { "doctrine/collections": "^1.0", "doctrine/common": "^2.6", @@ -2253,12 +2256,12 @@ } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -2421,5 +2424,5 @@ "ext-dom": "*" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.1.0" }