Skip to content

Commit 54665c4

Browse files
committed
Performance/LowExpiryCacheTime: add support for handling PHP 8.0+ function calls using named parameters
Includes tests.
1 parent 0ab9037 commit 54665c4

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHPCSUtils\Utils\Numbers;
14+
use PHPCSUtils\Utils\PassedParameters;
1415
use PHPCSUtils\Utils\TextStrings;
1516
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
1617

@@ -68,21 +69,21 @@ class LowExpiryCacheTimeSniff extends AbstractFunctionParameterSniff {
6869
* normal file processing.
6970
*/
7071
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
71-
if ( isset( $parameters[4] ) === false ) {
72+
$expire_param = PassedParameters::getParameterFromStack( $parameters, 4, 'expire' );
73+
if ( $expire_param === false ) {
7274
// If no cache expiry time, bail (i.e. we don't want to flag for something like feeds where it is cached indefinitely until a hook runs).
7375
return;
7476
}
7577

76-
$param = $parameters[4];
7778
$tokensAsString = '';
7879
$reportPtr = null;
7980
$openParens = 0;
8081

8182
$message = 'Cache expiry time could not be determined. Please inspect that the fourth parameter passed to %s() evaluates to 300 seconds or more. Found: "%s"';
8283
$error_code = 'CacheTimeUndetermined';
83-
$data = [ $matched_content, $parameters[4]['clean'] ];
84+
$data = [ $matched_content, $expire_param['clean'] ];
8485

85-
for ( $i = $param['start']; $i <= $param['end']; $i++ ) {
86+
for ( $i = $expire_param['start']; $i <= $expire_param['end']; $i++ ) {
8687
if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) === true ) {
8788
$tokensAsString .= ' ';
8889
continue;

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.1.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,12 @@ wp_cache_set( $key, $data, '', 0x96 ); // Hexidecimal number. Bad (=150).
150150
wp_cache_set( $key, $data, '', 0b10010110 ); // PHP 5.4 binary number. Bad (=150).
151151
wp_cache_set( $key, $data, '', 1_50 ); // PHP 7.4 numeric literal with underscore. Bad.
152152
wp_cache_set( $key, $data, '', 0o226 ); // PHP 8.1 octal literal. Bad (=150).
153+
154+
// Safeguard handling of function calls using PHP 8.0+ named parameters.
155+
wp_cache_add(data: $data, group: $group); // OK, well, not really, missing required $key param, but that's not the concern of this sniff.
156+
wp_cache_replace(data: $data, expire: 400, group: $group); // OK.
157+
wp_cache_add($key, group: $group, data: $data, expires: 100,); // OK, well, not really, typo in param name, but that's not the concern of the sniff.
158+
wp_cache_replace($key, expire: 400, group: $group, data: 100,); // OK.
159+
160+
wp_cache_replace($key, $data, expire: 100 ); // Bad.
161+
wp_cache_replace(expire: 100, data: $data, key: $group); // Bad.

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function getWarningList( $testFile = '' ) {
7979
150 => 1,
8080
151 => 1,
8181
152 => 1,
82+
160 => 1,
83+
161 => 1,
8284
];
8385

8486
default:

0 commit comments

Comments
 (0)