基础代码
This commit is contained in:
+231
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
|
||||
|
||||
use PHPCompatibility\AbstractNewFeatureSniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
|
||||
/**
|
||||
* Warns for non-magic behaviour of magic methods prior to becoming magic.
|
||||
*
|
||||
* PHP version 5.0+
|
||||
*
|
||||
* @link https://www.php.net/manual/en/language.oop5.magic.php
|
||||
* @link https://wiki.php.net/rfc/closures#additional_goodyinvoke
|
||||
* @link https://wiki.php.net/rfc/debug-info
|
||||
*
|
||||
* @since 7.0.4
|
||||
* @since 7.1.0 Now extends the `AbstractNewFeatureSniff` instead of the base `Sniff` class.
|
||||
*/
|
||||
class NewMagicMethodsSniff extends AbstractNewFeatureSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* A list of new magic methods, not considered magic in older versions.
|
||||
*
|
||||
* Method names in the array should be all *lowercase*.
|
||||
* The array lists : version number with false (not magic) or true (magic).
|
||||
* If's sufficient to list the first version where the method became magic.
|
||||
*
|
||||
* @since 7.0.4
|
||||
*
|
||||
* @var array(string => array(string => bool|string))
|
||||
*/
|
||||
protected $newMagicMethods = array(
|
||||
'__construct' => array(
|
||||
'4.4' => false,
|
||||
'5.0' => true,
|
||||
),
|
||||
'__destruct' => array(
|
||||
'4.4' => false,
|
||||
'5.0' => true,
|
||||
),
|
||||
'__get' => array(
|
||||
'4.4' => false,
|
||||
'5.0' => true,
|
||||
),
|
||||
|
||||
'__isset' => array(
|
||||
'5.0' => false,
|
||||
'5.1' => true,
|
||||
),
|
||||
'__unset' => array(
|
||||
'5.0' => false,
|
||||
'5.1' => true,
|
||||
),
|
||||
'__set_state' => array(
|
||||
'5.0' => false,
|
||||
'5.1' => true,
|
||||
),
|
||||
|
||||
'__callstatic' => array(
|
||||
'5.2' => false,
|
||||
'5.3' => true,
|
||||
),
|
||||
'__invoke' => array(
|
||||
'5.2' => false,
|
||||
'5.3' => true,
|
||||
),
|
||||
|
||||
'__debuginfo' => array(
|
||||
'5.5' => false,
|
||||
'5.6' => true,
|
||||
),
|
||||
|
||||
// Special case - only became properly magical in 5.2.0,
|
||||
// before that it was only called for echo and print.
|
||||
'__tostring' => array(
|
||||
'5.1' => false,
|
||||
'5.2' => true,
|
||||
'message' => 'The method %s() was not truly magical in PHP version %s and earlier. The associated magic functionality will only be called when directly combined with echo or print.',
|
||||
),
|
||||
|
||||
'__serialize' => array(
|
||||
'7.3' => false,
|
||||
'7.4' => true,
|
||||
),
|
||||
'__unserialize' => array(
|
||||
'7.3' => false,
|
||||
'7.4' => true,
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 7.0.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(\T_FUNCTION);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 7.0.4
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$functionName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
$functionNameLc = strtolower($functionName);
|
||||
|
||||
if (isset($this->newMagicMethods[$functionNameLc]) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->inClassScope($phpcsFile, $stackPtr, false) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$itemInfo = array(
|
||||
'name' => $functionName,
|
||||
'nameLc' => $functionNameLc,
|
||||
);
|
||||
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the relevant sub-array for a specific item from a multi-dimensional array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array Version and other information about the item.
|
||||
*/
|
||||
public function getItemArray(array $itemInfo)
|
||||
{
|
||||
return $this->newMagicMethods[$itemInfo['nameLc']];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of the non-PHP-version array keys used in a sub-array.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getNonVersionArrayKeys()
|
||||
{
|
||||
return array('message');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the relevant detail (version) information for use in an error message.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param array $itemArray Version and other information about the item.
|
||||
* @param array $itemInfo Base information about the item.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorInfo(array $itemArray, array $itemInfo)
|
||||
{
|
||||
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
|
||||
$errorInfo['error'] = false; // Warning, not error.
|
||||
$errorInfo['message'] = '';
|
||||
|
||||
if (empty($itemArray['message']) === false) {
|
||||
$errorInfo['message'] = $itemArray['message'];
|
||||
}
|
||||
|
||||
return $errorInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the error message template for this sniff.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getErrorMsgTemplate()
|
||||
{
|
||||
return 'The method %s() was not magical in PHP version %s and earlier. The associated magic functionality will not be invoked.';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allow for concrete child classes to filter the error message before it's passed to PHPCS.
|
||||
*
|
||||
* @since 7.1.0
|
||||
*
|
||||
* @param string $error The error message which was created.
|
||||
* @param array $itemInfo Base information about the item this error message applies to.
|
||||
* @param array $errorInfo Detail information about an item this error message applies to.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
|
||||
{
|
||||
if ($errorInfo['message'] !== '') {
|
||||
$error = $errorInfo['message'];
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
|
||||
|
||||
use PHPCompatibility\Sniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
|
||||
/**
|
||||
* Detect declaration of the magic `__autoload()` method.
|
||||
*
|
||||
* This method has been deprecated in PHP 7.2 in favour of `spl_autoload_register()`.
|
||||
*
|
||||
* PHP version 7.2
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration72.deprecated.php#migration72.deprecated.__autoload-method
|
||||
* @link https://wiki.php.net/rfc/deprecations_php_7_2#autoload
|
||||
* @link https://www.php.net/manual/en/function.autoload.php
|
||||
*
|
||||
* @since 8.1.0
|
||||
* @since 9.0.0 Renamed from `DeprecatedMagicAutoloadSniff` to `RemovedMagicAutoloadSniff`.
|
||||
*/
|
||||
class RemovedMagicAutoloadSniff extends Sniff
|
||||
{
|
||||
/**
|
||||
* Scopes to look for when testing using validDirectScope.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $checkForScopes = array(
|
||||
'T_CLASS' => true,
|
||||
'T_ANON_CLASS' => true,
|
||||
'T_INTERFACE' => true,
|
||||
'T_TRAIT' => true,
|
||||
'T_NAMESPACE' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(\T_FUNCTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 8.1.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
if ($this->supportsAbove('7.2') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$funcName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
|
||||
if (strtolower($funcName) !== '__autoload') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->validDirectScope($phpcsFile, $stackPtr, $this->checkForScopes) !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$phpcsFile->addWarning('Use of __autoload() function is deprecated since PHP 7.2', $stackPtr, 'Found');
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
|
||||
|
||||
use PHPCompatibility\Sniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
|
||||
/**
|
||||
* Detect declaration of a namespaced function called `assert()`.
|
||||
*
|
||||
* As of PHP 7.3, a compile-time deprecation warning will be thrown when a function
|
||||
* called `assert()` is declared. In PHP 8 this will become a compile-error.
|
||||
*
|
||||
* Methods are unaffected.
|
||||
* Global, non-namespaced, `assert()` function declarations were always a fatal
|
||||
* "function already declared" error, so not the concern of this sniff.
|
||||
*
|
||||
* PHP version 7.3
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration73.deprecated.php#migration73.deprecated.core.assert
|
||||
* @link https://wiki.php.net/rfc/deprecations_php_7_3#defining_a_free-standing_assert_function
|
||||
* @link https://www.php.net/manual/en/function.assert.php
|
||||
*
|
||||
* @since 9.0.0
|
||||
*/
|
||||
class RemovedNamespacedAssertSniff extends Sniff
|
||||
{
|
||||
/**
|
||||
* Scopes in which an `assert` function can be declared without issue.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $scopes = array(
|
||||
\T_CLASS,
|
||||
\T_INTERFACE,
|
||||
\T_TRAIT,
|
||||
\T_CLOSURE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// Enrich the scopes list.
|
||||
if (\defined('T_ANON_CLASS')) {
|
||||
$this->scopes[] = \T_ANON_CLASS;
|
||||
}
|
||||
|
||||
return array(\T_FUNCTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
if ($this->supportsAbove('7.3') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$funcName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
|
||||
if (strtolower($funcName) !== 'assert') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($phpcsFile->hasCondition($stackPtr, $this->scopes) === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->determineNamespace($phpcsFile, $stackPtr) === '') {
|
||||
// Not a namespaced function declaration. This may be a parse error, but not our concern.
|
||||
return;
|
||||
}
|
||||
|
||||
$phpcsFile->addWarning('Declaring a free-standing function called assert() is deprecated since PHP 7.3.', $stackPtr, 'Found');
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
|
||||
|
||||
use PHPCompatibility\Sniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* Detect declarations of PHP 4 style constructors which are deprecated as of PHP 7.0.0.
|
||||
*
|
||||
* PHP 4 style constructors - methods that have the same name as the class they are defined in -
|
||||
* are deprecated as of PHP 7.0.0, and will be removed in the future.
|
||||
* PHP 7 will emit `E_DEPRECATED` if a PHP 4 constructor is the only constructor defined
|
||||
* within a class. Classes that implement a `__construct()` method are unaffected.
|
||||
*
|
||||
* Note: Methods with the same name as the class they are defined in _within a namespace_
|
||||
* are not recognized as constructors anyway and therefore outside the scope of this sniff.
|
||||
*
|
||||
* PHP version 7.0
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration70.deprecated.php#migration70.deprecated.php4-constructors
|
||||
* @link https://wiki.php.net/rfc/remove_php4_constructors
|
||||
* @link https://www.php.net/manual/en/language.oop5.decon.php
|
||||
*
|
||||
* @since 7.0.0
|
||||
* @since 7.0.8 This sniff now throws a warning instead of an error as the functionality is
|
||||
* only deprecated (for now).
|
||||
* @since 9.0.0 Renamed from `DeprecatedPHP4StyleConstructorsSniff` to `RemovedPHP4StyleConstructorsSniff`.
|
||||
*/
|
||||
class RemovedPHP4StyleConstructorsSniff extends Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @since 7.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
\T_CLASS,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @since 7.0.0
|
||||
* @since 7.0.8 The message is downgraded from error to warning as - for now - support
|
||||
* for PHP4-style constructors is just deprecated, not yet removed.
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
if ($this->supportsAbove('7.0') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->determineNamespace($phpcsFile, $stackPtr) !== '') {
|
||||
/*
|
||||
* Namespaced methods with the same name as the class are treated as
|
||||
* regular methods, so we can bow out if we're in a namespace.
|
||||
*
|
||||
* Note: the exception to this is PHP 5.3.0-5.3.2. This is currently
|
||||
* not dealt with.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$class = $tokens[$stackPtr];
|
||||
|
||||
if (isset($class['scope_closer']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
|
||||
if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_STRING) {
|
||||
// Anonymous class in combination with PHPCS 2.3.x.
|
||||
return;
|
||||
}
|
||||
|
||||
$scopeCloser = $class['scope_closer'];
|
||||
$className = $tokens[$nextNonEmpty]['content'];
|
||||
|
||||
if (empty($className) || \is_string($className) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nextFunc = $stackPtr;
|
||||
$classNameLc = strtolower($className);
|
||||
$newConstructorFound = false;
|
||||
$oldConstructorFound = false;
|
||||
$oldConstructorPos = -1;
|
||||
while (($nextFunc = $phpcsFile->findNext(array(\T_FUNCTION, \T_DOC_COMMENT_OPEN_TAG), ($nextFunc + 1), $scopeCloser)) !== false) {
|
||||
// Skip over docblocks.
|
||||
if ($tokens[$nextFunc]['code'] === \T_DOC_COMMENT_OPEN_TAG) {
|
||||
$nextFunc = $tokens[$nextFunc]['comment_closer'];
|
||||
continue;
|
||||
}
|
||||
|
||||
$functionScopeCloser = $nextFunc;
|
||||
if (isset($tokens[$nextFunc]['scope_closer'])) {
|
||||
// Normal (non-interface, non-abstract) method.
|
||||
$functionScopeCloser = $tokens[$nextFunc]['scope_closer'];
|
||||
}
|
||||
|
||||
$funcName = $phpcsFile->getDeclarationName($nextFunc);
|
||||
if (empty($funcName) || \is_string($funcName) === false) {
|
||||
$nextFunc = $functionScopeCloser;
|
||||
continue;
|
||||
}
|
||||
|
||||
$funcNameLc = strtolower($funcName);
|
||||
|
||||
if ($funcNameLc === '__construct') {
|
||||
$newConstructorFound = true;
|
||||
}
|
||||
|
||||
if ($funcNameLc === $classNameLc) {
|
||||
$oldConstructorFound = true;
|
||||
$oldConstructorPos = $nextFunc;
|
||||
}
|
||||
|
||||
// If both have been found, no need to continue looping through the functions.
|
||||
if ($newConstructorFound === true && $oldConstructorFound === true) {
|
||||
break;
|
||||
}
|
||||
|
||||
$nextFunc = $functionScopeCloser;
|
||||
}
|
||||
|
||||
if ($newConstructorFound === false && $oldConstructorFound === true) {
|
||||
$phpcsFile->addWarning(
|
||||
'Use of deprecated PHP4 style class constructor is not supported since PHP 7.',
|
||||
$oldConstructorPos,
|
||||
'Found'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPCompatibility, an external standard for PHP_CodeSniffer.
|
||||
*
|
||||
* @package PHPCompatibility
|
||||
* @copyright 2012-2019 PHPCompatibility Contributors
|
||||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
|
||||
* @link https://github.com/PHPCompatibility/PHPCompatibility
|
||||
*/
|
||||
|
||||
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
|
||||
|
||||
use Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff as PHPCS_CamelCapsFunctionNameSniff;
|
||||
use PHP_CodeSniffer_File as File;
|
||||
use PHP_CodeSniffer_Standards_AbstractScopeSniff as PHPCS_AbstractScopeSniff;
|
||||
use PHP_CodeSniffer_Tokens as Tokens;
|
||||
|
||||
/**
|
||||
* All function and method names starting with double underscore are reserved by PHP.
|
||||
*
|
||||
* PHP version All
|
||||
*
|
||||
* {@internal Extends an upstream sniff to benefit from the properties contained therein.
|
||||
* The properties are lists of valid PHP magic function and method names, which
|
||||
* should be ignored for the purposes of this sniff.
|
||||
* As this sniff is not PHP version specific, we don't need access to the utility
|
||||
* methods in the PHPCompatibility\Sniff, so extending the upstream sniff is fine.
|
||||
* As the upstream sniff checks the same (and more, but we don't need the rest),
|
||||
* the logic in this sniff is largely the same as used upstream.
|
||||
* Extending the upstream sniff instead of including it via the ruleset, however,
|
||||
* prevents hard to debug issues of errors not being reported from the upstream sniff
|
||||
* if this library is used in combination with other rulesets.}
|
||||
*
|
||||
* @link https://www.php.net/manual/en/language.oop5.magic.php
|
||||
*
|
||||
* @since 8.2.0 This was previously, since 7.0.3, checked by the upstream sniff.
|
||||
* @since 9.3.2 The sniff will now ignore functions marked as `@deprecated` by design.
|
||||
*/
|
||||
class ReservedFunctionNamesSniff extends PHPCS_CamelCapsFunctionNameSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Overload the constructor to work round various PHPCS cross-version compatibility issues.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$scopeTokens = array(\T_CLASS, \T_INTERFACE, \T_TRAIT);
|
||||
if (\defined('T_ANON_CLASS')) {
|
||||
$scopeTokens[] = \T_ANON_CLASS;
|
||||
}
|
||||
|
||||
// Call the grand-parent constructor directly.
|
||||
PHPCS_AbstractScopeSniff::__construct($scopeTokens, array(\T_FUNCTION), true);
|
||||
|
||||
// Make sure debuginfo is included in the array. Upstream only includes it since 2.5.1.
|
||||
$this->magicMethods['debuginfo'] = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Processes the tokens within the scope.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being processed.
|
||||
* @param int $stackPtr The position where this token was
|
||||
* found.
|
||||
* @param int $currScope The position of the current scope.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
/*
|
||||
* Determine if this is a function which needs to be examined.
|
||||
* The `processTokenWithinScope()` is called for each valid scope a method is in,
|
||||
* so for nested classes, we need to make sure we only examine the token for
|
||||
* the lowest level valid scope.
|
||||
*/
|
||||
$conditions = $tokens[$stackPtr]['conditions'];
|
||||
end($conditions);
|
||||
$deepestScope = key($conditions);
|
||||
if ($deepestScope !== $currScope) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isFunctionDeprecated($phpcsFile, $stackPtr) === true) {
|
||||
/*
|
||||
* Deprecated functions don't have to comply with the naming conventions,
|
||||
* otherwise functions deprecated in favour of a function with a compliant
|
||||
* name would still trigger an error.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
if ($methodName === null) {
|
||||
// Ignore closures.
|
||||
return;
|
||||
}
|
||||
|
||||
// Is this a magic method. i.e., is prefixed with "__" ?
|
||||
if (preg_match('|^__[^_]|', $methodName) > 0) {
|
||||
$magicPart = strtolower(substr($methodName, 2));
|
||||
if (isset($this->magicMethods[$magicPart]) === false
|
||||
&& isset($this->methodsDoubleUnderscore[$magicPart]) === false
|
||||
) {
|
||||
$className = '[anonymous class]';
|
||||
$scopeNextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($currScope + 1), null, true);
|
||||
if ($scopeNextNonEmpty !== false && $tokens[$scopeNextNonEmpty]['code'] === \T_STRING) {
|
||||
$className = $tokens[$scopeNextNonEmpty]['content'];
|
||||
}
|
||||
|
||||
$phpcsFile->addWarning(
|
||||
'Method name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.',
|
||||
$stackPtr,
|
||||
'MethodDoubleUnderscore',
|
||||
array($className . '::' . $methodName)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Processes the tokens outside the scope.
|
||||
*
|
||||
* @since 8.2.0
|
||||
*
|
||||
* @param \PHP_CodeSniffer_File $phpcsFile The file being processed.
|
||||
* @param int $stackPtr The position where this token was
|
||||
* found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
if ($this->isFunctionDeprecated($phpcsFile, $stackPtr) === true) {
|
||||
/*
|
||||
* Deprecated functions don't have to comply with the naming conventions,
|
||||
* otherwise functions deprecated in favour of a function with a compliant
|
||||
* name would still trigger an error.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
$functionName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
if ($functionName === null) {
|
||||
// Ignore closures.
|
||||
return;
|
||||
}
|
||||
|
||||
// Is this a magic function. i.e., it is prefixed with "__".
|
||||
if (preg_match('|^__[^_]|', $functionName) > 0) {
|
||||
$magicPart = strtolower(substr($functionName, 2));
|
||||
if (isset($this->magicFunctions[$magicPart]) === false) {
|
||||
$phpcsFile->addWarning(
|
||||
'Function name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.',
|
||||
$stackPtr,
|
||||
'FunctionDoubleUnderscore',
|
||||
array($functionName)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check whether a function has been marked as deprecated via a @deprecated tag
|
||||
* in the function docblock.
|
||||
*
|
||||
* @since 9.3.2
|
||||
*
|
||||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
||||
* @param int $stackPtr The position of a T_FUNCTION
|
||||
* token in the stack.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isFunctionDeprecated(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$find = Tokens::$methodPrefixes;
|
||||
$find[] = \T_WHITESPACE;
|
||||
|
||||
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
|
||||
if ($tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG) {
|
||||
// Function doesn't have a doc comment or is using the wrong type of comment.
|
||||
return false;
|
||||
}
|
||||
|
||||
$commentStart = $tokens[$commentEnd]['comment_opener'];
|
||||
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
|
||||
if ($tokens[$tag]['content'] === '@deprecated') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user