Skip to content

Commit

Permalink
Merge pull request #163 from magento-commerce/develop
Browse files Browse the repository at this point in the history
Develop to master v19
  • Loading branch information
svera authored Mar 8, 2022
2 parents 1e62380 + 235640e commit 73d4423
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];

/**
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Functions;

/**
* Class to test validate PHP functions usage of which without passing arguments.
*/
class FunctionsDeprecatedWithoutArgument
{
/**
* Test deprecation function.
*
* @return array
*/
public function testDeprecatedMethod(): array
{
return [
mb_check_encoding(), // Calling without argument is deprecated.
mb_check_encoding('test-argument'),
get_class($this), // Calling without argument is deprecated.
get_class(new FunctionsDeprecatedWithoutArgument()),
get_parent_class($this), // Calling without argument is deprecated.
get_parent_class(new FunctionsDeprecatedWithoutArgument())
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function getWarningList(): array
return [
23 => 1,
25 => 1,
27 => 1,
29 => 1
27 => 1
];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"AFL-3.0"
],
"type": "phpcodesniffer-standard",
"version": "18",
"version": "19",
"require": {
"php": ">=7.3",
"webonyx/graphql-php": "^14.9",
Expand Down
15 changes: 9 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 73d4423

Please sign in to comment.