Add Autoloaded_Options_Check for missing $autoload parameter - #1413
Add Autoloaded_Options_Check for missing $autoload parameter#1413faisalahammad wants to merge 2 commits into
Conversation
Warns when add_option() or update_option() is called without explicitly setting the $autoload parameter. The option then defaults to autoloading on every page request, which bloats the alloptions row and slows down every request. The check runs a new PluginCheck.CodeAnalysis.AutoLoadedOptions sniff that flags calls where $autoload is not passed. Calls with an explicit boolean are left alone.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
- Capitalize first letter of two long-description lines that started with a function name. Generic.Commenting.DocComment.LongNotCapital requires the long description in a doc comment to start uppercase. Refs WordPress#1413
|
Would be nice to make some progress on WordPress/WordPress-Coding-Standards#2520 first, which would simplify things here. |
|
Yes, that seems reasonable. |
|
Thank you, @swissspidy and @davidperezgar. It looks like multiple contributors are working on this: WordPress/WordPress-Coding-Standards#2520. Should I close this PR? |
|
@faisalahammad I just left a comment on the WPCS PR. Let's see how they respond. |
What?
Closes #28
Adds a new check,
autoloaded_options, that warns plugin authors whenadd_option()orupdate_option()is called without explicitly setting the$autoloadparameter.Why?
The
$autoloadparameter onadd_option()andupdate_option()defaults totrue, which loads the option on every page request. Plugins that accumulate autoloaded options bloat thealloptionsrow, and every WordPress request slows down. Letting the author decide explicitly improves performance across the install.How?
Custom PHPCS sniff
PluginCheck.CodeAnalysis.AutoLoadedOptionsthat wrapsAbstractFunctionParameterSniff. The sniff targets the two call sites (the$autoloadparameter sits at position 4 onadd_optionafter a deprecated arg, and at position 3 onupdate_option). Calls without an explicit$autoloadproduce a single warning per call site; an explicit booleantrueorfalseis left untouched, including the WP 6.6+ recommendation to pass boolean autoload instead of'yes'/'no'strings.A new check class
Autoloaded_Options_Checkregisters the sniff under theperformancecategory. It is wired intoDefault_Check_Repositoryunder theautoloaded_optionsslug.Files touched:
phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/AutoLoadedOptionsSniff.php(new sniff)phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/AutoLoadedOptionsUnitTest.{inc,php}(sniff unit test)phpcs-sniffs/PluginCheck/ruleset.xml(registers the new rule)includes/Checker/Checks/Performance/Autoloaded_Options_Check.php(check class)includes/Checker/Default_Check_Repository.php(registration)docs/checks.md(new row)tests/phpunit/testdata/plugins/test-plugin-autoloaded-options-check-{with,without}-errors/load.php(PHPUnit fixtures)tests/phpunit/tests/Checker/Checks/Autoloaded_Options_Check_Tests.php(PHPUnit integration test)Testing Instructions
wp plugin-check list-checksand confirmautoloaded_optionsis listed in theperformancecategory.wp plugin-check check <plugin>on it.PluginCheck.CodeAnalysis.AutoLoadedOptions.add_option_autoloadMissingandPluginCheck.CodeAnalysis.AutoLoadedOptions.update_option_autoloadMissing) and zero autoloaded_options results on the second.Automated tests:
composer testruns the PHPUnit integration tests against both fixtures.cd phpcs-sniffs && php vendor/bin/phpunit vendor/squizlabs/php_codesniffer/tests/AllTests.php --filter AutoLoadedOptions --no-coverage.AI Usage Disclosure
If AI tools were used, please describe how they were used:
Used Claude Code for code generation, refactoring and documentation drafting under direction of maintainer.