Skip to content

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

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Sniffs\Functions;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Sniff to validate PHP functions usage of which without passing arguments is deprecated.
*/
class FunctionsDeprecatedWithoutArgumentSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
private $warningMessage = 'Calling function %s() without argument is deprecated in PHP 8.1. '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.';

/**
* Warning violation code.
*
* @var string
*/
private $warningCode = 'FunctionsDeprecatedWithoutArgument';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private $warningCode = 'FunctionsDeprecatedWithoutArgument';
private const WARNING_CODE = 'FunctionsDeprecatedWithoutArgument';


/**
* Deprecated functions without argument.
*
* @var array
*/
private $deprecatedFunctions = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private $deprecatedFunctions = [
private const FUNCTIONS_LIST = [

'mb_check_encoding',
'get_class',
'get_parent_class',
'get_called_class'
];

/**
* @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 (in_array($functionName, $this->deprecatedFunctions)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (in_array($functionName, $this->deprecatedFunctions)) {
if (in_array($functionName, self::FUNCTIONS_LIST)) {

$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode);
$phpcsFile->addWarning(sprintf(self::WARNING_MESSAGE, $functionName), $stackPtr, self::WARNING_CODE);

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

namespace Magento2\Tests\Functions;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Test to validate PHP functions usage of which without passing arguments.
*/
class FunctionsDeprecatedWithoutArgumentUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList(): array
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList(): array
{
return [
23 => 1,
25 => 1,
27 => 1,
29 => 1
];
}
}
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@
<exclude-pattern>*Test.php</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Magento2.Functions.FunctionsDeprecatedWithoutArgument">
<severity>7</severity>
<type>warning</type>
<exclude-pattern>*\.xml$</exclude-pattern>
</rule>
<rule ref="Magento2.Namespaces.ImportsFromTestNamespace">
<severity>8</severity>
<type>warning</type>
Expand Down