feat(1314): add unclosed ob_start check#1318
Conversation
|
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. |
2f01cac to
32eccbe
Compare
|
Thanks for working on this. The new check is a good direction, but I found a few issues that should be addressed before merge:
|
|
Could this be implemented as a PHPCS Sniff instead? Since it's walking over the tokens in the source file, this would seem more appropriate. |
- Move logic into PluginCheck.CodeAnalysis.UnclosedObStart sniff - Check wrapper now extends Abstract_PHP_CodeSniffer_Check - Handle fully-qualified \ob_start() calls (T_NAME_FULLY_QUALIFIED + T_NS_SEPARATOR) - Skip warning when ob_start() is followed by add_action/add_filter (hook paired buffer) - Point docs URL to WordPress 6.9 field guide - Add sniff unit test and FQN/hook-paired test fixtures Addresses review feedback from davidperezgar and westonruter on WordPress#1314.
|
Hi @davidperezgar @westonruter, thanks for the feedback. I reworked the check based on it. Main change: following @westonruter's suggestion, I moved the logic out of the custom The three issues from @davidperezgar:
Pairing logic: a close only pairs with a start when they share the same nesting context, so a buffer closed only inside an One thing I want your opinion on: the hook-paired heuristic is the softest part. I went with "next statement is If you'd rather keep the original |
|
@faisalahammad The check is missing Other Performance checks that also apply to the plugin directory follow the same pattern of returning both categories. For example, public function get_categories() {
return array(
Check_Categories::CATEGORY_PLUGIN_REPO,
Check_Categories::CATEGORY_PERFORMANCE,
);
}Please update |
….1.0 - Add CATEGORY_PLUGIN_REPO so check runs in Plugin Directory UI by default - Override add_result_message_for_file to use get_documentation_url() when no docs URL provided - Bump @SInCE tags from 1.4.0 to 2.1.0 in check and sniff - Add test_get_categories() asserting both categories Refs WordPress#1318
|
Hi @davidperezgar, pushed changes addressing your feedback:
Quality gates (PHPCS + PHPStan) pass clean. PR description updated to reflect the PHPCS sniff implementation. Ready for re-review when you have time. |
Is this accurate? |
Sorry, I unchecked the checkbox. Is there anything else I should fix before merging? Thank you. |
|
@faisalahammad then it seems you need to check this checkbox instead:
Right? If so then explain how the tools were used, per the description:
|
|
Done. Checked the AI-assisted box and described Claude Code's role: PHPCS sniff architecture research, scope-tracking logic, PHP 7.4/8.0 FQN handling, PHPUnit fixture scenarios, PR body draft. Could you please check again and share your feedback, @westonruter? |
What?
Closes #1314
Adds a new performance check to detect
ob_start()calls that are not paired with a corresponding buffer-closing function within the same scope. Prevents unpredictable behaviour caused by misaligned output buffers in shared WordPress environments.Why?
Every
ob_start()must be paired with a closing call (ob_get_clean(),ob_end_clean(),ob_get_flush(), orob_end_flush()) in the same scope. Unclosed buffers break headers, lose output, and cause redirect issues in shared hosting. WordPress core, themes, and other plugins all share the buffer stack.How?
Implemented as a PHPCS sniff (
PluginCheck.CodeAnalysis.UnclosedObStart) following @westonruter's suggestion:Abstract_PHP_CodeSniffer_Checklike other sniff-based checksconditionsdata for scope tracking instead of hand-rolled brace-depth\ob_start()calls (PHP 7.4 two-token form + PHP 8+T_NAME_FULLY_QUALIFIED)ob_end_clean()only inside anif) as warningsob_start()is immediately followed byadd_action()/add_filter()Default_Check_Repositoryunder bothCATEGORY_PERFORMANCEandCATEGORY_PLUGIN_REPOTesting Instructions
wp plugin check <plugin-slug>or use the Plugin Check admin UIob_start()— confirm a warning is reported on the exact lineob_start()/ob_end_clean()in the same function — confirm no warning\ob_start()in namespaced code — confirm detection worksob_start()+add_action()) — confirm no warningAI Usage Disclosure
If AI tools were used, please describe how they were used:
AbstractFunctionParameterSniff/AbstractFunctionRestrictionsSniffpatterns inphpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/)$tokens[ $stackPtr ]['conditions'])T_STRING+T_NS_SEPARATORvsT_NAME_FULLY_QUALIFIED)Screenshots or screencast