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,104 @@
<?php
/**
* \PHPCompatibility\Sniffs\Operators\ForbiddenNegativeBitshift.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim@cu.be>
*/
namespace PHPCompatibility\Sniffs\Operators;
use PHPCompatibility\Sniff;
use PHPCompatibility\PHPCSHelper;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\Operators\ForbiddenNegativeBitshift.
*
* Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim@cu.be>
*/
class ForbiddenNegativeBitshiftSniff extends Sniff
{
/**
* Potential end tokens for which the end pointer has to be set back by one.
*
* {@internal The PHPCS `findEndOfStatement()` method is not completely consistent
* in how it returns the statement end. This is just a simple way to bypass
* the inconsistency for our purposes.}}
*
* @var array
*/
private $inclusiveStopPoints = array(
T_COLON => true,
T_COMMA => true,
T_DOUBLE_ARROW => true,
T_SEMICOLON => true,
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_SL,
T_SL_EQUAL,
T_SR,
T_SR_EQUAL,
);
}
/**
* 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;
}
$tokens = $phpcsFile->getTokens();
// Determine the start and end of the part of the statement we need to examine.
$start = ($stackPtr + 1);
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $start, null, true);
if ($next !== false && $tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
$start = ($next + 1);
}
$end = PHPCSHelper::findEndOfStatement($phpcsFile, $start);
if (isset($this->inclusiveStopPoints[$tokens[$end]['code']]) === true) {
--$end;
}
if ($this->isNegativeNumber($phpcsFile, $start, $end, true) !== true) {
// Not a negative number or undetermined.
return;
}
$phpcsFile->addError(
'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0. Found: %s',
$stackPtr,
'Found',
array($phpcsFile->getTokensAsString($start, ($end - $start + 1)))
);
}
}
@@ -0,0 +1,294 @@
<?php
/**
* \PHPCompatibility\Sniffs\Operators\NewOperatorsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2013 Cu.be Solutions bvba
*/
namespace PHPCompatibility\Sniffs\Operators;
use PHPCompatibility\AbstractNewFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* \PHPCompatibility\Sniffs\Operators\NewOperatorsSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
* @copyright 2013 Cu.be Solutions bvba
*/
class NewOperatorsSniff extends AbstractNewFeatureSniff
{
/**
* A list of new operators, not present in older versions.
*
* The array lists : version number with false (not present) or true (present).
* If's sufficient to list the first version where the keyword appears.
*
* @var array(string => array(string => int|string|null))
*/
protected $newOperators = array(
'T_POW' => array(
'5.5' => false,
'5.6' => true,
'description' => 'power operator (**)',
), // Identified in PHP < 5.6 icw PHPCS < 2.4.0 as T_MULTIPLY + T_MULTIPLY.
'T_POW_EQUAL' => array(
'5.5' => false,
'5.6' => true,
'description' => 'power assignment operator (**=)',
), // Identified in PHP < 5.6 icw PHPCS < 2.6.0 as T_MULTIPLY + T_MUL_EQUAL.
'T_SPACESHIP' => array(
'5.6' => false,
'7.0' => true,
'description' => 'spaceship operator (<=>)',
), // Identified in PHP < 7.0 icw PHPCS < 2.5.1 as T_IS_SMALLER_OR_EQUAL + T_GREATER_THAN.
'T_COALESCE' => array(
'5.6' => false,
'7.0' => true,
'description' => 'null coalescing operator (??)',
), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN.
/*
* Was slated for 7.2, but still not implemented. PHPCS however does already tokenize it.
* @link https://wiki.php.net/rfc/null_coalesce_equal_operator
*/
'T_COALESCE_EQUAL' => array(
'7.3' => false,
'7.4' => true,
'description' => 'null coalesce equal operator (??=)',
), // Identified in PHP < 7.0 icw PHPCS < 2.6.2 as T_INLINE_THEN + T_INLINE_THEN + T_EQUAL and between PHPCS 2.6.2 and PHPCS 2.8.1 as T_COALESCE + T_EQUAL.
);
/**
* A list of new operators which are not recognized in older PHPCS versions.
*
* The array lists an alternative token to listen for.
*
* @var array(string => int)
*/
protected $newOperatorsPHPCSCompat = array(
'T_POW' => T_MULTIPLY,
'T_POW_EQUAL' => T_MUL_EQUAL,
'T_SPACESHIP' => T_GREATER_THAN,
'T_COALESCE' => T_INLINE_THEN,
'T_COALESCE_EQUAL' => T_EQUAL,
);
/**
* Token translation table for older PHPCS versions.
*
* The 'before' index lists the token which would have to be directly before the
* token found for it to be one of the new operators.
* The 'real_token' index indicates which operator was found in that case.
*
* If the token combination has multi-layer complexity, such as is the case
* with T_COALESCE(_EQUAL), a 'callback' index is added instead pointing to a
* separate function which can determine whether this is the targetted token across
* PHP and PHPCS versions.
*
* {@internal 'before' was chosen rather than 'after' as that allowed for a 1-on-1
* translation list with the current tokens.}}
*
* @var array(string => array(string => string))
*/
protected $PHPCSCompatTranslate = array(
'T_MULTIPLY' => array(
'before' => 'T_MULTIPLY',
'real_token' => 'T_POW',
),
'T_MUL_EQUAL' => array(
'before' => 'T_MULTIPLY',
'real_token' => 'T_POW_EQUAL',
),
'T_GREATER_THAN' => array(
'before' => 'T_IS_SMALLER_OR_EQUAL',
'real_token' => 'T_SPACESHIP',
),
'T_INLINE_THEN' => array(
'callback' => 'isTCoalesce',
'real_token' => 'T_COALESCE',
),
'T_EQUAL' => array(
'callback' => 'isTCoalesceEqual',
'real_token' => 'T_COALESCE_EQUAL',
),
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
$tokens = array();
foreach ($this->newOperators as $token => $versions) {
if (defined($token)) {
$tokens[] = constant($token);
} elseif (isset($this->newOperatorsPHPCSCompat[$token])) {
$tokens[] = $this->newOperatorsPHPCSCompat[$token];
}
}
return $tokens;
}
/**
* 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)
{
$tokens = $phpcsFile->getTokens();
$tokenType = $tokens[$stackPtr]['type'];
// Translate older PHPCS token combis for new operators to the actual operator.
if (isset($this->newOperators[$tokenType]) === false) {
if (isset($this->PHPCSCompatTranslate[$tokenType])
&& ((isset($this->PHPCSCompatTranslate[$tokenType]['before'], $tokens[$stackPtr - 1]) === true
&& $tokens[$stackPtr - 1]['type'] === $this->PHPCSCompatTranslate[$tokenType]['before'])
|| (isset($this->PHPCSCompatTranslate[$tokenType]['callback']) === true
&& call_user_func(array($this, $this->PHPCSCompatTranslate[$tokenType]['callback']), $tokens, $stackPtr) === true))
) {
$tokenType = $this->PHPCSCompatTranslate[$tokenType]['real_token'];
}
} elseif ($tokenType === 'T_COALESCE') {
// Make sure that T_COALESCE is not confused with T_COALESCE_EQUAL.
if (isset($tokens[($stackPtr + 1)]) !== false && $tokens[($stackPtr + 1)]['code'] === T_EQUAL) {
// Ignore as will be dealt with via the T_EQUAL token.
return;
}
}
// If the translation did not yield one of the tokens we are looking for, bow out.
if (isset($this->newOperators[$tokenType]) === false) {
return;
}
$itemInfo = array(
'name' => $tokenType,
);
$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->newOperators[$itemInfo['name']];
}
/**
* Get an array of the non-PHP-version array keys used in a sub-array.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array('description');
}
/**
* 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['description'] = $itemArray['description'];
return $errorInfo;
}
/**
* Allow for concrete child classes to filter the error data before it's passed to PHPCS.
*
* @param array $data The error data array 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 array
*/
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
{
$data[0] = $errorInfo['description'];
return $data;
}
/**
* Callback function to determine whether a T_EQUAL token is really a T_COALESCE_EQUAL token.
*
* @param array $tokens The token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function isTCoalesceEqual($tokens, $stackPtr)
{
if ($tokens[$stackPtr]['code'] !== T_EQUAL || isset($tokens[($stackPtr - 1)]) === false) {
// Function called for wrong token or token has no predecesor.
return false;
}
if ($tokens[($stackPtr - 1)]['type'] === 'T_COALESCE') {
return true;
}
if ($tokens[($stackPtr - 1)]['type'] === 'T_INLINE_THEN'
&& (isset($tokens[($stackPtr - 2)]) && $tokens[($stackPtr - 2)]['type'] === 'T_INLINE_THEN')
) {
return true;
}
return false;
}
/**
* Callback function to determine whether a T_INLINE_THEN token is really a T_COALESCE token.
*
* @param array $tokens The token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function isTCoalesce($tokens, $stackPtr)
{
if ($tokens[$stackPtr]['code'] !== T_INLINE_THEN || isset($tokens[($stackPtr - 1)]) === false) {
// Function called for wrong token or token has no predecesor.
return false;
}
if ($tokens[($stackPtr - 1)]['code'] === T_INLINE_THEN) {
// Make sure not to confuse it with the T_COALESCE_EQUAL token.
if (isset($tokens[($stackPtr + 1)]) === false || $tokens[($stackPtr + 1)]['code'] !== T_EQUAL) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,74 @@
<?php
/**
* \PHPCompatibility\Sniffs\Operators\NewShortTernarySniff.
*
* PHP version 5.3
*
* @category PHP
* @package PHPCompatibility
* @author Ben Selby <bselby@plus.net>
* @copyright 2012 Ben Selby
*/
namespace PHPCompatibility\Sniffs\Operators;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* \PHPCompatibility\Sniffs\Operators\NewShortTernarySniff.
*
* Performs checks on ternary operators, specifically that the middle expression
* is not omitted for versions that don't support this.
*
* PHP version 5.3
*
* @category PHP
* @package PHPCompatibility
* @author Ben Selby <bselby@plus.net>
* @copyright 2012 Ben Selby
*/
class NewShortTernarySniff extends Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_INLINE_THEN);
}
/**
* 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->supportsBelow('5.2') === false) {
return;
}
$tokens = $phpcsFile->getTokens();
// Get next non-whitespace token, and check it isn't the related inline else
// symbol, which is not allowed prior to PHP 5.3.
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($next !== false && $tokens[$next]['code'] === T_INLINE_ELSE) {
$phpcsFile->addError(
'Middle may not be omitted from ternary operators in PHP < 5.3',
$stackPtr,
'MiddleMissing'
);
}
}
}