From e331fe6f08a7fdd45a22bb41cb033e49b51bd8f6 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 5 Aug 2025 16:27:08 -0300 Subject: [PATCH 1/4] WP/GetMetaSingle: simplify code using two class constants Now that support for PHP < 7.2 has been dropped in PR 2614, we can simplify the sniff code by defining the two different function signature formats as class constants. This was not possible before as PHP < 5.6 did not support defining class constants as arrays. --- WordPress/Sniffs/WP/GetMetaSingleSniff.php | 134 ++++++++------------- 1 file changed, 50 insertions(+), 84 deletions(-) diff --git a/WordPress/Sniffs/WP/GetMetaSingleSniff.php b/WordPress/Sniffs/WP/GetMetaSingleSniff.php index 243f272507..512d992ca2 100644 --- a/WordPress/Sniffs/WP/GetMetaSingleSniff.php +++ b/WordPress/Sniffs/WP/GetMetaSingleSniff.php @@ -33,6 +33,48 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { */ const METRIC_NAME = 'get_*meta() function called with $single parameter'; + /** + * Relevant signature structure for specific meta functions. + * + * These functions use 'key' as the parameter name at position 2 and + * 'single' as the parameter name at position 3. + * + * @since 3.2.0 + * + * @var array> + */ + const SPECIFIC_META_FUNCTIONS_FORMAT = array( + 'condition' => array( + 'param_name' => 'key', + 'position' => 2, + ), + 'recommended' => array( + 'param_name' => 'single', + 'position' => 3, + ), + ); + + /** + * Relevant signature structure for generic meta functions. + * + * These functions use 'meta_key' as the parameter name at position 3 and + * 'single' as the parameter name at position 4. + * + * @since 3.2.0 + * + * @var array> + */ + const GENERIC_META_FUNCTIONS_FORMAT = array( + 'condition' => array( + 'param_name' => 'meta_key', + 'position' => 3, + ), + 'recommended' => array( + 'param_name' => 'single', + 'position' => 4, + ), + ); + /** * The group name for this group of functions. * @@ -45,10 +87,6 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { /** * List of functions this sniff should examine. * - * {@internal Once support for PHP < 5.6 is dropped, it is possible to create two class constants - * 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/ @@ -66,86 +104,14 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { * the relevant parameters. */ protected $target_functions = array( - 'get_comment_meta' => array( - 'condition' => array( - 'param_name' => 'key', - 'position' => 2, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 3, - ), - ), - 'get_metadata' => array( - 'condition' => array( - 'param_name' => 'meta_key', - 'position' => 3, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 4, - ), - ), - 'get_metadata_default' => array( - 'condition' => array( - 'param_name' => 'meta_key', - 'position' => 3, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 4, - ), - ), - 'get_metadata_raw' => array( - 'condition' => array( - 'param_name' => 'meta_key', - 'position' => 3, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 4, - ), - ), - 'get_post_meta' => array( - 'condition' => array( - 'param_name' => 'key', - 'position' => 2, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 3, - ), - ), - 'get_site_meta' => array( - 'condition' => array( - 'param_name' => 'key', - 'position' => 2, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 3, - ), - ), - 'get_term_meta' => array( - 'condition' => array( - 'param_name' => 'key', - 'position' => 2, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 3, - ), - ), - 'get_user_meta' => array( - 'condition' => array( - 'param_name' => 'key', - 'position' => 2, - ), - 'recommended' => array( - 'param_name' => 'single', - 'position' => 3, - ), - ), + 'get_comment_meta' => self::SPECIFIC_META_FUNCTIONS_FORMAT, + 'get_metadata' => self::GENERIC_META_FUNCTIONS_FORMAT, + 'get_metadata_default' => self::GENERIC_META_FUNCTIONS_FORMAT, + 'get_metadata_raw' => self::GENERIC_META_FUNCTIONS_FORMAT, + 'get_post_meta' => self::SPECIFIC_META_FUNCTIONS_FORMAT, + 'get_site_meta' => self::SPECIFIC_META_FUNCTIONS_FORMAT, + 'get_term_meta' => self::SPECIFIC_META_FUNCTIONS_FORMAT, + 'get_user_meta' => self::SPECIFIC_META_FUNCTIONS_FORMAT, ); /** From a09975d17c87902b436ffd3db9a15ae156d5ee35 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 17 Oct 2025 08:28:27 -0300 Subject: [PATCH 2/4] Fix version number in since tags --- WordPress/Sniffs/WP/GetMetaSingleSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/Sniffs/WP/GetMetaSingleSniff.php b/WordPress/Sniffs/WP/GetMetaSingleSniff.php index 512d992ca2..8a8ff32122 100644 --- a/WordPress/Sniffs/WP/GetMetaSingleSniff.php +++ b/WordPress/Sniffs/WP/GetMetaSingleSniff.php @@ -39,7 +39,7 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { * These functions use 'key' as the parameter name at position 2 and * 'single' as the parameter name at position 3. * - * @since 3.2.0 + * @since 3.3.0 * * @var array> */ @@ -60,7 +60,7 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { * These functions use 'meta_key' as the parameter name at position 3 and * 'single' as the parameter name at position 4. * - * @since 3.2.0 + * @since 3.3.0 * * @var array> */ From e53d4f914969987cfa993de482dcddf36ef08afb Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 20 Oct 2025 09:16:06 -0300 Subject: [PATCH 3/4] Change the order of the two class constants --- WordPress/Sniffs/WP/GetMetaSingleSniff.php | 28 +++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/WordPress/Sniffs/WP/GetMetaSingleSniff.php b/WordPress/Sniffs/WP/GetMetaSingleSniff.php index 8a8ff32122..af9f6c9267 100644 --- a/WordPress/Sniffs/WP/GetMetaSingleSniff.php +++ b/WordPress/Sniffs/WP/GetMetaSingleSniff.php @@ -34,44 +34,44 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { const METRIC_NAME = 'get_*meta() function called with $single parameter'; /** - * Relevant signature structure for specific meta functions. + * Relevant signature structure for generic meta functions. * - * These functions use 'key' as the parameter name at position 2 and - * 'single' as the parameter name at position 3. + * These functions use 'meta_key' as the parameter name at position 3 and + * 'single' as the parameter name at position 4. * * @since 3.3.0 * * @var array> */ - const SPECIFIC_META_FUNCTIONS_FORMAT = array( + const GENERIC_META_FUNCTIONS_FORMAT = array( 'condition' => array( - 'param_name' => 'key', - 'position' => 2, + 'param_name' => 'meta_key', + 'position' => 3, ), 'recommended' => array( 'param_name' => 'single', - 'position' => 3, + 'position' => 4, ), ); /** - * Relevant signature structure for generic meta functions. + * Relevant signature structure for specific meta functions. * - * These functions use 'meta_key' as the parameter name at position 3 and - * 'single' as the parameter name at position 4. + * These functions use 'key' as the parameter name at position 2 and + * 'single' as the parameter name at position 3. * * @since 3.3.0 * * @var array> */ - const GENERIC_META_FUNCTIONS_FORMAT = array( + const SPECIFIC_META_FUNCTIONS_FORMAT = array( 'condition' => array( - 'param_name' => 'meta_key', - 'position' => 3, + 'param_name' => 'key', + 'position' => 2, ), 'recommended' => array( 'param_name' => 'single', - 'position' => 4, + 'position' => 3, ), ); From e3672da484eb3c90a2abaed736f7cc014c37e263 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 20 Oct 2025 10:57:22 -0300 Subject: [PATCH 4/4] Add `private` visibility to the two new constants --- WordPress/Sniffs/WP/GetMetaSingleSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/Sniffs/WP/GetMetaSingleSniff.php b/WordPress/Sniffs/WP/GetMetaSingleSniff.php index af9f6c9267..76b576d109 100644 --- a/WordPress/Sniffs/WP/GetMetaSingleSniff.php +++ b/WordPress/Sniffs/WP/GetMetaSingleSniff.php @@ -43,7 +43,7 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { * * @var array> */ - const GENERIC_META_FUNCTIONS_FORMAT = array( + private const GENERIC_META_FUNCTIONS_FORMAT = array( 'condition' => array( 'param_name' => 'meta_key', 'position' => 3, @@ -64,7 +64,7 @@ final class GetMetaSingleSniff extends AbstractFunctionParameterSniff { * * @var array> */ - const SPECIFIC_META_FUNCTIONS_FORMAT = array( + private const SPECIFIC_META_FUNCTIONS_FORMAT = array( 'condition' => array( 'param_name' => 'key', 'position' => 2,