From d60856ea09ef309031cd1b0571215625bcea2cac Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Sun, 28 Jul 2024 16:31:08 +0200 Subject: [PATCH] Implement changes to the sniff code asked during review --- .../GetMetaFunctionSingleParameterSniff.php | 17 +++++++--- ...GetMetaFunctionSingleParameterUnitTest.inc | 32 ++++++++++++++----- ...GetMetaFunctionSingleParameterUnitTest.php | 23 ++++++------- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/WordPress/Sniffs/WP/GetMetaFunctionSingleParameterSniff.php b/WordPress/Sniffs/WP/GetMetaFunctionSingleParameterSniff.php index e50990e52..670dc89b6 100644 --- a/WordPress/Sniffs/WP/GetMetaFunctionSingleParameterSniff.php +++ b/WordPress/Sniffs/WP/GetMetaFunctionSingleParameterSniff.php @@ -36,6 +36,10 @@ final class GetMetaFunctionSingleParameterSniff extends AbstractFunctionParamete /** * List of functions this sniff should examine. * + * Once support for PHP 5.4 is dropped, it is possible to create two private properties + * representing the two different signatures of get meta functions to remove the duplication + * of the name and position of the parameters. + * * @link https://developer.wordpress.org/reference/functions/get_comment_meta/ * @link https://developer.wordpress.org/reference/functions/get_metadata/ * @link https://developer.wordpress.org/reference/functions/get_metadata_default/ @@ -47,8 +51,10 @@ final class GetMetaFunctionSingleParameterSniff extends AbstractFunctionParamete * * @since 3.2.0 * - * @var array Key is function name, value is an array containing information - * about the name and position of the relevant parameters. + * @var array>> Key is function name, value is an + * array containing information about + * the name and position of the + * relevant parameters. */ protected $target_functions = array( 'get_comment_meta' => array( @@ -160,13 +166,14 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p return; } - $tokens = $this->phpcsFile->getTokens(); + $tokens = $this->phpcsFile->getTokens(); + $message_data = array( $condition['parameter'], $tokens[ $stackPtr ]['content'], $recommended['parameter'] ); $this->phpcsFile->addWarning( - 'When passing the $%s parameter to %s(), the $%s parameter must also be passed to make it explicit whether an array or a string is expected.', + 'When passing the $%s parameter to %s(), it is recommended to pass the $%s parameter as well to make it explicit whether an array or a string is expected.', $stackPtr, 'ReturnTypeNotExplicit', - array( $condition['parameter'], $tokens[ $stackPtr ]['content'], $recommended['parameter'] ) + $message_data ); } } diff --git a/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.inc b/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.inc index 80d47d4b0..54ea2fecc 100644 --- a/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.inc +++ b/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.inc @@ -1,11 +1,17 @@ get_post_meta($a, $b); +MyClass::get_post_meta($a, $b); +echo GET_POST_META; +add_action('my_action', get_post_meta(...)); // PHP 8.1 first class callable. +/* + * These should all be okay. + */ $ok = get_post_meta( $post_id ); $ok = get_post_meta( $post_id, $meta_key, false ); $ok = get_post_meta( $post_id, single: true ); @@ -15,10 +21,20 @@ $ok = get_metadata( 'post', $post_id, $meta_key, true ); $ok = get_metadata( 'post', $post_id, single: true ); $ok = get_metadata( 'post', $post_id, single: true, meta_key: $meta_key ); -$warning = get_post_meta( $post_id, $meta_key ); -$warning = get_post_meta( $post_id, key: $meta_key ); +// Incorrect function calls, should be ignored by the sniff. +$incorrect_but_ok = get_post_meta(); +$incorrect_but_ok = get_post_meta( single: true, $post_id ); +$incorrect_but_ok = get_post_meta( single: true ); +$incorrect_but_ok = get_metadata( 'post' ); + +/* + * These should all be flagged with a warning. + */ +$warning = \get_post_meta( $post_id, $meta_key ); +implode(', ', get_post_meta( $post_id, $meta_key )); +if (get_post_meta( $post_id, key: $meta_key )) {} $warning = get_post_meta( $post_id, key: $meta_key, sinngle: true ); // Typo in parameter name. -$warning = get_comment_meta( $comment_id, $meta_key ); +echo get_comment_meta( $comment_id, $meta_key ); $warning = get_site_meta( $site_id, $meta_key ); $warning = get_term_meta( $term_id, $meta_key ); $warning = get_user_meta( $user_id, $meta_key ); diff --git a/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.php b/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.php index 98e415a81..b1582c6a5 100644 --- a/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.php +++ b/WordPress/Tests/WP/GetMetaFunctionSingleParameterUnitTest.php @@ -36,17 +36,18 @@ public function getErrorList() { */ public function getWarningList() { return array( - 18 => 1, - 19 => 1, - 20 => 1, - 21 => 1, - 22 => 1, - 23 => 1, - 24 => 1, - 25 => 1, - 26 => 1, - 31 => 1, - 32 => 1, + 33 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 40 => 1, + 41 => 1, + 42 => 1, + 47 => 1, + 48 => 1, ); } }