Skip to content

Commit

Permalink
Yoast/JsonEncodeAlternative: handle PHP 8.1+ first class callables
Browse files Browse the repository at this point in the history
PHP 8.1 added support for "first class callables".

As the number of parameters which will be passed to the callable is unknown, the sniff should flag these functions when used as first class callable, but should not auto-fix.

Includes tests.
  • Loading branch information
jrfnl committed Nov 4, 2023
1 parent ae9c55c commit 0501528
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Yoast/Sniffs/Yoast/JsonEncodeAlternativeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 4 additions & 0 deletions Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
4 changes: 4 additions & 0 deletions Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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(
2 changes: 2 additions & 0 deletions Yoast/Tests/Yoast/JsonEncodeAlternativeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function getErrorList(): array {
41 => 1,
43 => 1,
44 => 1,
47 => 1,
48 => 1,
52 => 1,
];
}

Expand Down

0 comments on commit 0501528

Please sign in to comment.