-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. ' | ||||||
. 'Please pass the input to validate as the first argument of the function.'; | ||||||
|
||||||
/** | ||||||
* Warning violation code. | ||||||
* | ||||||
* @var string | ||||||
*/ | ||||||
private $warningCode = 'FunctionsDeprecatedWithoutArgument'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/** | ||||||
* Deprecated functions without argument. | ||||||
* | ||||||
* @var array | ||||||
*/ | ||||||
private $deprecatedFunctions = [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'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)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$phpcsFile->addWarning(sprintf($this->warningMessage, $functionName), $stackPtr, $this->warningCode); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
} |
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 | ||
]; | ||
} | ||
} |
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.