Skip to content

Commit 58578d1

Browse files
committed
Performance/LowExpiryCacheTime: add support for PHP 7.4+ numeric literals and 8.1+ octal literals
While PHPCS fully supports these syntaxes at the tokenizer level, this sniff includes the contents of the found token in a string which will be run through `eval()`, which means that if the number was originally provided in a PHP 7.4+ syntax and the sniffs are run on PHP 7.2, the `eval` will result in a fatal error, breaking the sniff (and possibly the PHPCS run). By using the PHPCSUtils `Numbers` class, we can bypass this and retrieve the decimal value of a integer/float, no matter in what syntax the number is provided. If we then use the decimal value in the string which will be run through `eval()`, the potential fatal error is avoided. Includes tests.
1 parent f2367e9 commit 58578d1

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WordPressVIPMinimum\Sniffs\Performance;
1111

1212
use PHP_CodeSniffer\Util\Tokens;
13+
use PHPCSUtils\Utils\Numbers;
1314
use PHPCSUtils\Utils\TextStrings;
1415
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
1516

@@ -104,8 +105,10 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
104105
if ( $this->tokens[ $i ]['code'] === T_LNUMBER
105106
|| $this->tokens[ $i ]['code'] === T_DNUMBER
106107
) {
107-
// Integer or float.
108-
$tokensAsString .= $this->tokens[ $i ]['content'];
108+
// Make sure that PHP 7.4 numeric literals and PHP 8.1 explicit octals don't cause problems.
109+
$number_info = Numbers::getCompleteNumber( $this->phpcsFile, $i );
110+
$tokensAsString .= $number_info['decimal'];
111+
$i = $number_info['last_token'];
109112
continue;
110113
}
111114

0 commit comments

Comments
 (0)