基础代码

This commit is contained in:
2021-02-26 22:23:13 +08:00
parent 7884df52f0
commit a719feebba
2585 changed files with 328263 additions and 0 deletions
@@ -0,0 +1,199 @@
<?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\Operators;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Detect code affected by the change in operator precedence of concatenation in PHP 8.0.
*
* In PHP < 8.0 the operator precedence of `.`, `+` and `-` are the same.
* As of PHP 8.0, the operator precedence of the concatenation operator will be
* lowered to be right below the `<<` and `>>` operators.
*
* As of PHP 7.4, a deprecation warning will be thrown upon encountering an
* unparenthesized expression containing an `.` before a `+` or `-`.
*
* PHP version 7.4
* PHP version 8.0
*
* @link https://wiki.php.net/rfc/concatenation_precedence
* @link https://www.php.net/manual/en/language.operators.precedence.php
*
* @since 9.2.0
*/
class ChangedConcatOperatorPrecedenceSniff extends Sniff
{
/**
* List of tokens with a lower operator precedence than concatenation in PHP >= 8.0.
*
* @since 9.2.0
*
* @var array
*/
private $tokensWithLowerPrecedence = array(
'T_BITWISE_AND' => true,
'T_BITWISE_XOR' => true,
'T_BITWISE_OR' => true,
'T_COALESCE' => true,
'T_INLINE_THEN' => true,
'T_INLINE_ELSE' => true,
'T_YIELD_FROM' => true,
'T_YIELD' => true,
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 9.2.0
*
* @return array
*/
public function register()
{
return array(
\T_PLUS,
\T_MINUS,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 9.2.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.4') === false) {
return;
}
if ($this->isUnaryPlusMinus($phpcsFile, $stackPtr) === true) {
return;
}
$tokens = $phpcsFile->getTokens();
for ($i = ($stackPtr - 1); $stackPtr >= 0; $i--) {
if ($tokens[$i]['code'] === \T_STRING_CONCAT) {
// Found one.
break;
}
if ($tokens[$i]['code'] === \T_SEMICOLON
|| $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET
|| $tokens[$i]['code'] === \T_OPEN_TAG
|| $tokens[$i]['code'] === \T_OPEN_TAG_WITH_ECHO
|| $tokens[$i]['code'] === \T_COMMA
|| $tokens[$i]['code'] === \T_COLON
|| $tokens[$i]['code'] === \T_CASE
) {
// If we reached any of the above tokens, we've reached the end of
// the statement without encountering a concatenation operator.
return;
}
if ($tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET
&& isset($tokens[$i]['bracket_closer'])
&& $tokens[$i]['bracket_closer'] > $stackPtr
) {
// No need to look any further, this is plus/minus within curly braces
// and we've reached the open curly.
return;
}
if ($tokens[$i]['code'] === \T_OPEN_PARENTHESIS
&& isset($tokens[$i]['parenthesis_closer'])
&& $tokens[$i]['parenthesis_closer'] > $stackPtr
) {
// No need to look any further, this is plus/minus within parenthesis
// and we've reached the open parenthesis.
return;
}
if (($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$i]['code'] === \T_OPEN_SQUARE_BRACKET)
&& isset($tokens[$i]['bracket_closer'])
&& $tokens[$i]['bracket_closer'] > $stackPtr
) {
// No need to look any further, this is plus/minus within a short array
// or array key square brackets and we've reached the opener.
return;
}
if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET) {
if (isset($tokens[$i]['scope_owner'])) {
// Different scope, we've passed the start of the statement.
return;
}
if (isset($tokens[$i]['bracket_opener'])) {
$i = $tokens[$i]['bracket_opener'];
}
continue;
}
if ($tokens[$i]['code'] === \T_CLOSE_PARENTHESIS
&& isset($tokens[$i]['parenthesis_opener'])
) {
// Skip over statements in parenthesis, including long arrays.
$i = $tokens[$i]['parenthesis_opener'];
continue;
}
if (($tokens[$i]['code'] === \T_CLOSE_SQUARE_BRACKET
|| $tokens[$i]['code'] === \T_CLOSE_SHORT_ARRAY)
&& isset($tokens[$i]['bracket_opener'])
) {
// Skip over array keys and short arrays.
$i = $tokens[$i]['bracket_opener'];
continue;
}
// Check for chain being broken by a token with a lower precedence.
if (isset(Tokens::$booleanOperators[$tokens[$i]['code']]) === true
|| isset(Tokens::$assignmentTokens[$tokens[$i]['code']]) === true
) {
return;
}
if (isset($this->tokensWithLowerPrecedence[$tokens[$i]['type']]) === true) {
if ($tokens[$i]['code'] === \T_BITWISE_AND
&& $phpcsFile->isReference($i) === true
) {
continue;
}
return;
}
}
$message = 'Using an unparenthesized expression containing a "." before a "+" or "-" has been deprecated in PHP 7.4';
$isError = false;
if ($this->supportsAbove('8.0') === true) {
$message .= ' and removed in PHP 8.0';
$isError = true;
}
$this->addMessage($phpcsFile, $message, $i, $isError);
}
}
@@ -0,0 +1,109 @@
<?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\Operators;
use PHPCompatibility\Sniff;
use PHPCompatibility\PHPCSHelper;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* Bitwise shifts by negative number will throw an ArithmeticError since PHP 7.0.
*
* PHP version 7.0
*
* @link https://wiki.php.net/rfc/integer_semantics
* @link https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.integers.negative-bitshift
*
* @since 7.0.0
*/
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.}
*
* @since 8.2.0
*
* @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.
*
* @since 7.0.0
* @since 8.2.0 Now registers all bitshift tokens, not just bitshift right (`T_SR`).
*
* @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.
*
* @since 7.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.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,317 @@
<?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\Operators;
use PHPCompatibility\AbstractNewFeatureSniff;
use PHP_CodeSniffer_File as File;
/**
* Detect use of new PHP operators.
*
* PHP version All
*
* @link https://wiki.php.net/rfc/pow-operator
* @link https://wiki.php.net/rfc/combined-comparison-operator
* @link https://wiki.php.net/rfc/isset_ternary
* @link https://wiki.php.net/rfc/null_coalesce_equal_operator
*
* @since 9.0.0 Detection of new operators was originally included in the
* `NewLanguageConstruct` sniff (since 5.6).
*/
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 operator appears.
*
* @since 5.6
*
* @var array(string => array(string => bool|string))
*/
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.
'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.
*
* @since 7.0.3
*
* @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.}
*
* @since 7.0.3
*
* @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.
*
* @since 5.6
*
* @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.
*
* @since 5.6
*
* @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.
*
* @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->newOperators[$itemInfo['name']];
}
/**
* 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('description');
}
/**
* 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['description'] = $itemArray['description'];
return $errorInfo;
}
/**
* Filter the error data before it's passed to PHPCS.
*
* @since 7.1.0
*
* @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.
*
* @since 7.1.2
*
* @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 predecessor.
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.
*
* @since 7.1.2
*
* @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 predecessor.
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,73 @@
<?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\Operators;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* Detect usage of the short ternary (elvis) operator as introduced in PHP 5.3.
*
* Performs checks on ternary operators, specifically that the middle expression
* is not omitted for versions that don't support this.
*
* PHP version 5.3
*
* @link https://www.php.net/manual/en/migration53.new-features.php
* @link https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
*
* @since 7.0.0
* @since 7.0.8 This sniff now throws an error instead of a warning.
* @since 9.0.0 Renamed from `TernaryOperatorsSniff` to `NewShortTernarySniff`.
*/
class NewShortTernarySniff 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_INLINE_THEN);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 7.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->supportsBelow('5.2') === false) {
return;
}
if ($this->isShortTernary($phpcsFile, $stackPtr) === false) {
return;
}
$phpcsFile->addError(
'Middle may not be omitted from ternary operators in PHP < 5.3',
$stackPtr,
'MiddleMissing'
);
}
}
@@ -0,0 +1,157 @@
<?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\Operators;
use PHPCompatibility\Sniff;
use PHPCompatibility\PHPCSHelper;
use PHP_CodeSniffer_File as File;
use PHP_CodeSniffer_Tokens as Tokens;
/**
* The left-associativity of the ternary operator is deprecated in PHP 7.4 and
* removed in PHP 8.0.
*
* PHP version 7.4
* PHP version 8.0
*
* @link https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.nested-ternary
* @link https://wiki.php.net/rfc/ternary_associativity
* @link https://github.com/php/php-src/pull/4017
*
* @since 9.2.0
*/
class RemovedTernaryAssociativitySniff extends Sniff
{
/**
* List of tokens with a lower operator precedence than ternary.
*
* @since 9.2.0
*
* @var array
*/
private $tokensWithLowerPrecedence = array(
'T_YIELD_FROM' => true,
'T_YIELD' => true,
'T_LOGICAL_AND' => true,
'T_LOGICAL_OR' => true,
'T_LOGICAL_XOR' => true,
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 9.2.0
*
* @return array
*/
public function register()
{
return array(\T_INLINE_THEN);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 9.2.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.4') === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$endOfStatement = PHPCSHelper::findEndOfStatement($phpcsFile, $stackPtr);
if ($tokens[$endOfStatement]['code'] !== \T_SEMICOLON
&& $tokens[$endOfStatement]['code'] !== \T_COLON
&& $tokens[$endOfStatement]['code'] !== \T_COMMA
&& $tokens[$endOfStatement]['code'] !== \T_DOUBLE_ARROW
&& $tokens[$endOfStatement]['code'] !== \T_OPEN_TAG
&& $tokens[$endOfStatement]['code'] !== \T_CLOSE_TAG
) {
// End of statement is last non-empty before close brace, so make sure we examine that token too.
++$endOfStatement;
}
$ternaryCount = 0;
$elseCount = 0;
$shortTernaryCount = 0;
// Start at $stackPtr so we don't need duplicate code for short ternary determination.
for ($i = $stackPtr; $i < $endOfStatement; $i++) {
if (($tokens[$i]['code'] === \T_OPEN_SHORT_ARRAY
|| $tokens[$i]['code'] === \T_OPEN_SQUARE_BRACKET
|| $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET)
&& isset($tokens[$i]['bracket_closer'])
) {
// Skip over short arrays, array access keys and curlies.
$i = $tokens[$i]['bracket_closer'];
continue;
}
if ($tokens[$i]['code'] === \T_OPEN_PARENTHESIS
&& isset($tokens[$i]['parenthesis_closer'])
) {
// Skip over anything between parentheses.
$i = $tokens[$i]['parenthesis_closer'];
continue;
}
// Check for operators with lower operator precedence.
if (isset(Tokens::$assignmentTokens[$tokens[$i]['code']])
|| isset($this->tokensWithLowerPrecedence[$tokens[$i]['code']])
) {
break;
}
if ($tokens[$i]['code'] === \T_INLINE_THEN) {
++$ternaryCount;
if ($this->isShortTernary($phpcsFile, $i) === true) {
++$shortTernaryCount;
}
continue;
}
if ($tokens[$i]['code'] === \T_INLINE_ELSE) {
if (($ternaryCount - $elseCount) >= 2) {
// This is the `else` for a ternary in the middle part of a previous ternary.
--$ternaryCount;
} else {
++$elseCount;
}
continue;
}
}
if ($ternaryCount > 1 && $ternaryCount === $elseCount && $ternaryCount > $shortTernaryCount) {
$message = 'The left-associativity of the ternary operator has been deprecated in PHP 7.4';
$isError = false;
if ($this->supportsAbove('8.0') === true) {
$message .= ' and removed in PHP 8.0';
$isError = true;
}
$message .= '. Multiple consecutive ternaries detected. Use parenthesis to clarify the order in which the operations should be executed';
$this->addMessage($phpcsFile, $message, $stackPtr, $isError);
}
}
}