diff --git a/Yoast/Sniffs/Yoast/JsonEncodeAlternativeSniff.php b/Yoast/Sniffs/Yoast/JsonEncodeAlternativeSniff.php index 52227499..05aa48fe 100644 --- a/Yoast/Sniffs/Yoast/JsonEncodeAlternativeSniff.php +++ b/Yoast/Sniffs/Yoast/JsonEncodeAlternativeSniff.php @@ -86,6 +86,19 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content * this sniff). */ if ( empty( $params ) ) { + /* + * Make sure this is not a PHP 8.1+ first class callable. If it is, throw the error, but don't autofix. + */ + $ignore = Tokens::$emptyTokens; + $ignore[ \T_OPEN_PARENTHESIS ] = \T_OPEN_PARENTHESIS; + + $first_in_call = $this->phpcsFile->findNext( $ignore, ( $stackPtr + 1 ), null, true ); + if ( $first_in_call !== false && $this->tokens[ $first_in_call ]['code'] === \T_ELLIPSIS ) { + $error_code .= 'InFirstClassCallable'; + $this->phpcsFile->addError( $error, $stackPtr, $error_code, $data ); + return; + } + $fix = $this->phpcsFile->addFixableError( $error, $stackPtr, $error_code, $data ); if ( $fix === true ) { $this->fix_it( $stackPtr ); diff --git a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc index 18da3a67..4b6a254e 100644 --- a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc +++ b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc @@ -43,6 +43,10 @@ $json = wp_json_encode( data: $thing ); // Error. $json = json_encode( flags: 0, depth: 256, value: $thing ); // Error, non-fixable. $json = wp_json_encode( depth: 256, options: 0, data: $thing ); // Error, non-fixable. +// Safeguard handling of the functions when used as PHP 8.1+ first class callables. +call_user_func(json_encode(...), $something); // Error, non-fixable. +\call_user_func(\wp_json_encode(...), $something); // Error, non-fixable. + // Live coding/parse error test. // This must be the last test in the file. $json = wp_json_encode( diff --git a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc.fixed b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc.fixed index 50cfa6f2..ac0724ba 100644 --- a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc.fixed +++ b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc.fixed @@ -43,6 +43,10 @@ $json = \WPSEO_Utils::format_json_encode( data: $thing ); // Error. $json = json_encode( flags: 0, depth: 256, value: $thing ); // Error, non-fixable. $json = wp_json_encode( depth: 256, options: 0, data: $thing ); // Error, non-fixable. +// Safeguard handling of the functions when used as PHP 8.1+ first class callables. +call_user_func(json_encode(...), $something); // Error, non-fixable. +\call_user_func(\wp_json_encode(...), $something); // Error, non-fixable. + // Live coding/parse error test. // This must be the last test in the file. $json = \WPSEO_Utils::format_json_encode( diff --git a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.php b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.php index af18e3de..e42a8735 100644 --- a/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.php +++ b/Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.php @@ -40,7 +40,9 @@ public function getErrorList(): array { 41 => 1, 43 => 1, 44 => 1, + 47 => 1, 48 => 1, + 52 => 1, ]; }