Add Autoloaded_Options_Check for missing $autoload parameter - #1413
faisalahammad wants to merge 3 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. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The sniff documentation and user-facing warning incorrectly state that every omitted autoload argument defaults to autoloading.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds a performance check warning when WordPress option APIs omit the $autoload argument.
Changes:
- Adds and registers the
AutoLoadedOptionsPHPCS sniff. - Integrates the stable
autoloaded_optionscheck. - Adds documentation, fixtures, and automated tests.
| File | Description |
|---|---|
phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/AutoLoadedOptionsSniff.php |
Detects omitted autoload arguments. |
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/AutoLoadedOptionsUnitTest.inc |
Provides sniff fixtures. |
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/AutoLoadedOptionsUnitTest.php |
Defines expected sniff warnings. |
phpcs-sniffs/PluginCheck/ruleset.xml |
Registers the sniff. |
includes/Checker/Checks/Performance/Autoloaded_Options_Check.php |
Exposes the performance check. |
includes/Checker/Default_Check_Repository.php |
Registers the default check. |
docs/checks.md |
Documents the check. |
tests/phpunit/testdata/plugins/test-plugin-autoloaded-options-check-with-errors/load.php |
Adds warning-producing fixtures. |
tests/phpunit/testdata/plugins/test-plugin-autoloaded-options-check-without-errors/load.php |
Adds clean fixtures. |
tests/phpunit/tests/Checker/Checks/Autoloaded_Options_Check_Tests.php |
Tests check integration. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Reword sniff docblock and warning message so they no longer imply that an omitted $autoload always autoloads. The value is left to WordPress and depends on whether the option already exists and on the WordPress version. - Update check description and docs/checks.md row with the same wording, and link to update_option() docs. - Harden the check test: assert empty errors, warning count 4, and read warning codes with dynamic column keys. Addresses PR feedback. Refs WordPress#1413
|
@swissspidy I kept this PR open and fixed the accuracy issue independently. WPCS PR #2520 is still a draft and depends on PHPCS 4.0. This change keeps the current check and corrects its wording for WordPress 6.6+ behavior. |

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?
When the
$autoloadparameter is omitted, WordPress determines the value based on the WordPress version and whether the option already exists. This implicit decision can lead to options being loaded on every page request, which can bloat thealloptionsrow and slow down requests. Letting the author choose explicitly makes the performance trade-off intentional.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.