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,73 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/
namespace WordPress\Sniffs\Functions;
use WordPress\AbstractFunctionRestrictionsSniff;
/**
* Restricts the usage of extract().
*
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#dont-extract
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.10.0 Previously this check was contained within WordPress_Sniffs_VIP_RestrictedFunctionsSniff.
* @since 0.13.0 Class name changed: this class is now namespaced.
*
* @deprecated 1.0.0 This sniff has been moved to the `PHP` category.
* This file remains for now to prevent BC breaks.
*/
class DontExtractSniff extends \WordPress\Sniffs\PHP\DontExtractSniff {
/**
* Keep track of whether the warnings have been thrown to prevent
* the messages being thrown for every token triggering the sniff.
*
* @since 1.0.0
*
* @var array
*/
private $thrown = array(
'DeprecatedSniff' => false,
'FoundPropertyForDeprecatedSniff' => false,
);
/**
* Don't use.
*
* @deprecated 1.0.0
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return void|int
*/
public function process_token( $stackPtr ) {
if ( false === $this->thrown['DeprecatedSniff'] ) {
$this->thrown['DeprecatedSniff'] = $this->phpcsFile->addWarning(
'The "WordPress.Functions.DontExtract" sniff has been renamed to "WordPress.PHP.DontExtract". Please update your custom ruleset.',
0,
'DeprecatedSniff'
);
}
if ( ! empty( $this->exclude )
&& false === $this->thrown['FoundPropertyForDeprecatedSniff']
) {
$this->thrown['FoundPropertyForDeprecatedSniff'] = $this->phpcsFile->addWarning(
'The "WordPress.Functions.DontExtract" sniff has been renamed to "WordPress.PHP.DontExtract". Please update your custom ruleset.',
0,
'FoundPropertyForDeprecatedSniff'
);
}
return parent::process_token( $stackPtr );
}
}
@@ -0,0 +1,91 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/
namespace WordPress\Sniffs\Functions;
use WordPress\Sniff;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Enforces no whitespace between the parenthesis of a function call without parameters.
*
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#space-usage
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.12.0
* @since 0.13.0 Class name changed: this class is now namespaced.
*/
class FunctionCallSignatureNoParamsSniff extends Sniff {
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
return Tokens::$functionNameTokens;
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int Integer stack pointer to skip the rest of the file.
*/
public function process_token( $stackPtr ) {
// Find the next non-empty token.
$openParenthesis = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
if ( \T_OPEN_PARENTHESIS !== $this->tokens[ $openParenthesis ]['code'] ) {
// Not a function call.
return;
}
if ( ! isset( $this->tokens[ $openParenthesis ]['parenthesis_closer'] ) ) {
// Not a function call.
return;
}
// Find the previous non-empty token.
$search = Tokens::$emptyTokens;
$search[] = \T_BITWISE_AND;
$previous = $this->phpcsFile->findPrevious( $search, ( $stackPtr - 1 ), null, true );
if ( \T_FUNCTION === $this->tokens[ $previous ]['code'] ) {
// It's a function definition, not a function call.
return;
}
$closer = $this->tokens[ $openParenthesis ]['parenthesis_closer'];
if ( ( $closer - 1 ) === $openParenthesis ) {
return;
}
$nextNonWhitespace = $this->phpcsFile->findNext( \T_WHITESPACE, ( $openParenthesis + 1 ), null, true );
if ( $nextNonWhitespace !== $closer ) {
// Function has params or comment between parenthesis.
return;
}
$fix = $this->phpcsFile->addFixableError(
'Function calls without parameters should have no spaces between the parenthesis.',
( $openParenthesis + 1 ),
'WhitespaceFound'
);
if ( true === $fix ) {
// If there is only whitespace between the parenthesis, it will just be the one token.
$this->phpcsFile->fixer->replaceToken( ( $openParenthesis + 1 ), '' );
}
}
}
@@ -0,0 +1,48 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/
namespace WordPress\Sniffs\Functions;
use WordPress\AbstractFunctionRestrictionsSniff;
/**
* Restricts usage of some functions.
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.3.0
* @since 0.13.0 Class name changed: this class is now namespaced.
*
* @deprecated 0.10.0 The functionality which used to be contained in this class has been moved to
* the WordPress_AbstractFunctionRestrictionsSniff class.
* This class is left here to prevent backward-compatibility breaks for
* custom sniffs extending the old class and references to this
* sniff from custom phpcs.xml files.
* @see \WordPress\AbstractFunctionRestrictionsSniff
*/
class FunctionRestrictionsSniff extends AbstractFunctionRestrictionsSniff {
/**
* Groups of functions to restrict.
*
* Example: groups => array(
* 'lambda' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Use anonymous functions instead please!',
* 'functions' => array( 'file_get_contents', 'create_function' ),
* )
* )
*
* @return array
*/
public function getGroups() {
return array();
}
}