Introducing coding standards validation.

This commit is contained in:
Marcos Schratzenstaller
2019-04-22 17:28:46 -03:00
committed by Nathan Rice
parent 6e40c921ab
commit fd504d41ac
1624 changed files with 190339 additions and 262 deletions
@@ -0,0 +1,194 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\NewMagicMethodsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
use PHPCompatibility\AbstractNewFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\NewMagicMethodsSniff.
*
* Warns for non-magic behaviour of magic methods prior to becoming magic.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
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.
*
* @var array(string => array(string => int|string|null))
*/
protected $newMagicMethods = array(
'__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.',
),
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_FUNCTION);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @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.
*
* @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.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array('message');
}
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @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.
*
* @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.
*
* @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;
}
}
@@ -0,0 +1,80 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedMagicAutoloadSniff.
*
* PHP version 7.2
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedMagicAutoloadSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
*/
class RemovedMagicAutoloadSniff extends Sniff
{
/**
* Scopes to look for when testing using validDirectScope
*
* @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.
*
* @return array
*/
public function register()
{
return array(T_FUNCTION);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @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');
}
}
@@ -0,0 +1,92 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedNamespacedAssertSniff.
*
* PHP version 7.3
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* Removed Namespaced 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.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class RemovedNamespacedAssertSniff extends Sniff
{
/**
* Scopes in which an `assert` function can be declared without issue.
*
* @var array
*/
private $scopes = array(
T_CLASS,
T_INTERFACE,
T_TRAIT,
T_CLOSURE,
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @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.
*
* @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. Parse error, but not our concern.
return;
}
$phpcsFile->addWarning('Declaring a free-standing function called assert() is deprecated since PHP 7.3.', $stackPtr, 'Found');
}
}
@@ -0,0 +1,135 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedPHP4StyleConstructorsSniff.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Koen Eelen <koen.eelen@cu.be>
*/
namespace PHPCompatibility\Sniffs\FunctionNameRestrictions;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\RemovedPHP4StyleConstructorsSniff.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Koen Eelen <koen.eelen@cu.be>
*/
class RemovedPHP4StyleConstructorsSniff extends Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_CLASS,
T_INTERFACE,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @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(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
$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'
);
}
}
}
@@ -0,0 +1,144 @@
<?php
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\ReservedFunctionNamesSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
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;
/**
* \PHPCompatibility\Sniffs\FunctionNameRestrictions\ReservedFunctionNamesSniff.
*
* All function and method names starting with double underscore are reserved by PHP.
*
* {@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.}}
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class ReservedFunctionNamesSniff extends PHPCS_CamelCapsFunctionNameSniff
{
/**
* Overload the constructor to work round various PHPCS cross-version compatibility issues.
*/
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.
*
* @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;
}
$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.
*
* @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)
{
$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)
);
}
}
}
}