From f8fdd6f7325e4d113c87ccd062e994e8c03696f7 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 26 Aug 2024 09:56:59 -0300 Subject: [PATCH 01/26] =?UTF-8?q?=E2=9C=A8=20New=20WordPress.WP.OptionAuto?= =?UTF-8?q?load=20sniff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This sniff warns users about missing `$autoload` parameter when calling relevant WordPress option functions. The sniff runs when the following functions are found in the code: - add_option( $option, $value = '', $deprecated = '', $autoload = null) - update_option( $option, $value, $autoload = null ) - wp_set_options_autoload( $options, $autoload ) - wp_set_option_autoload( $option, $autoload ) - wp_set_option_autoload_values( $options ) For `add_option()` and `update_option()`, if the `$autoload` parameter is not passed, it simply recommends passing it. For the other three functions, the `$autoload` parameter (or `$options` parameter in the case of `wp_set_option_autoload_values()`) is mandatory, so it is not needed to check if the parameter is omitted. If the `$autoload` parameter is passed and is `true|false|null` for `add_option()` and `update_option()` or `true|false` for `wp_set_options_autoload()`, `wp_set_option_autoload()` and `wp_set_option_autoload_values()`, the sniff stays silent as those are valid values. If the `$autoload` parameter is passed and is `'yes'|'no'`, the sniff flags as discouraged parameter values and auto-fixes to `true|false`. For the internal-use only values of the `$autoload` parameter, if the value is `'auto'|'auto-on'|'auto-off'` the sniff flags it as unsupported without auto-fixing. If the value is `'on'|'off'`, the sniff flags it as unsupported and auto-fixes to `true|false`. Any other value passed is flagged as an unsupported value and a list of supported values is provided. The sniff ignores the function call when it is not able to determine the value of the `$autoload` parameter. For example, when a variable or a constant is passed. --- WordPress-Extra/ruleset.xml | 7 + WordPress/Docs/WP/OptionAutoloadStandard.xml | 135 ++++++ WordPress/Sniffs/WP/OptionAutoloadSniff.php | 416 ++++++++++++++++++ WordPress/Tests/WP/OptionAutoloadUnitTest.inc | 214 +++++++++ .../Tests/WP/OptionAutoloadUnitTest.inc.fixed | 214 +++++++++ WordPress/Tests/WP/OptionAutoloadUnitTest.php | 116 +++++ 6 files changed, 1102 insertions(+) create mode 100644 WordPress/Docs/WP/OptionAutoloadStandard.xml create mode 100644 WordPress/Sniffs/WP/OptionAutoloadSniff.php create mode 100644 WordPress/Tests/WP/OptionAutoloadUnitTest.inc create mode 100644 WordPress/Tests/WP/OptionAutoloadUnitTest.inc.fixed create mode 100644 WordPress/Tests/WP/OptionAutoloadUnitTest.php diff --git a/WordPress-Extra/ruleset.xml b/WordPress-Extra/ruleset.xml index a939d1eb95..8b9da1a178 100644 --- a/WordPress-Extra/ruleset.xml +++ b/WordPress-Extra/ruleset.xml @@ -193,6 +193,13 @@ https://github.com/WordPress/WordPress-Coding-Standards/issues/2459 --> + + +