Skip to content

Commit 7c5899b

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 3043a9d commit 7c5899b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.1.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,16 @@ add_action('my_action', wp_cache_set(...)); // PHP 8.1 first class callable. OK,
137137
// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
138138
#[WP_Cache_Replace('text', 'data', 'group', 50)]
139139
function foo() {}
140+
141+
// Alternative numeric base.
142+
wp_cache_set( $key, $data, '', 0620 ); // Octal number. OK (=400).
143+
wp_cache_set( $key, $data, '', 0x190 ); // Hexidecimal number. OK (=400).
144+
wp_cache_set( $key, $data, '', 0b110010000 ); // PHP 5.4 binary number. OK (=400).
145+
wp_cache_set( $key, $data, '', 1_000 ); // PHP 7.4 numeric literal with underscore. OK.
146+
wp_cache_set( $key, $data, '', 0o620 ); // PHP 8.1 octal literal. OK (=400).
147+
148+
wp_cache_set( $key, $data, '', 0226 ); // Octal number. Bad (=150).
149+
wp_cache_set( $key, $data, '', 0x96 ); // Hexidecimal number. Bad (=150).
150+
wp_cache_set( $key, $data, '', 0b10010110 ); // PHP 5.4 binary number. Bad (=150).
151+
wp_cache_set( $key, $data, '', 1_50 ); // PHP 7.4 numeric literal with underscore. Bad.
152+
wp_cache_set( $key, $data, '', 0o226 ); // PHP 8.1 octal literal. Bad (=150).

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function getWarningList( $testFile = '' ) {
7474
127 => 1,
7575
133 => 1,
7676
134 => 1,
77+
148 => 1,
78+
149 => 1,
79+
150 => 1,
80+
151 => 1,
81+
152 => 1,
7782
];
7883

7984
default:

0 commit comments

Comments
 (0)