Skip to content

New sniff to prefer ob_get_clean#2764

Merged
Crabcyborg merged 1 commit into
masterfrom
new_sniff_to_prefer_ob_get_clean
Jan 9, 2026
Merged

New sniff to prefer ob_get_clean#2764
Crabcyborg merged 1 commit into
masterfrom
new_sniff_to_prefer_ob_get_clean

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 9, 2026

Walkthrough

This PR refactors output buffer management across the codebase by systematically replacing two-step buffer operations (ob_get_contents(); ob_end_clean();) with single ob_get_clean() calls. Additionally, a new PHP_CodeSniffer sniff is introduced to detect and enforce this pattern going forward.

Changes

Cohort / File(s) Summary
Output buffer refactoring in controllers
classes/controllers/FrmEntriesAJAXSubmitController.php, classes/controllers/FrmFieldsController.php, classes/controllers/FrmFormsController.php
Replaced two-step ob_get_contents()/ob_end_clean() with single ob_get_clean() call for footer AJAX scripts and form/field HTML capture.
Output buffer refactoring in helpers and models
classes/helpers/FrmAppHelper.php, classes/models/FrmStyle.php, classes/models/fields/FrmFieldCreditCard.php, classes/models/fields/FrmFieldDefault.php, classes/models/fields/FrmFieldType.php
Consolidated output buffer retrieval and cleanup into ob_get_clean() calls, eliminating intermediate variable assignments in clip(), get_file_contents(), get_css_content(), and field rendering methods.
New PHP_CodeSniffer sniff
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferObGetCleanSniff.php
Introduces sniff to detect ob_get_contents()/ob_end_clean() pattern and provide automated fix to replace with ob_get_clean(). Includes token validation and semicolon boundary checking.
Output buffer refactoring in stripe module
stripe/helpers/FrmStrpLiteConnectHelper.php, stripe/models/FrmStrpLiteAuth.php
Replaced two-step buffer operations with ob_get_clean() in render_settings() and maybe_show_message().
Output buffer refactoring in tests
tests/phpunit/base/FrmUnitTest.php, tests/phpunit/forms/test_FrmFormsController.php, tests/phpunit/misc/test_FrmAppController.php, tests/phpunit/misc/test_FrmAppHelper.php, tests/phpunit/styles/test_FrmStylesController.php
Simplified output buffer handling in test utilities and test methods by replacing ob_get_contents()/ob_end_clean() with ob_get_clean(). Also adds explicit fclose() after fwrite() in one test utility.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Prepare for v6.23 #2461: Modifies FrmFieldsController::load_single_field docblock (\@SInCE tag) which is a file touched by this PR's output-buffering refactor.

Suggested reviewers

  • lauramekaj1
🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.78% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess whether the description relates to the changeset. Add a description explaining the sniff's purpose, the pattern it detects, and why this refactoring improves code quality.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'New sniff to prefer ob_get_clean' accurately describes the primary change: introducing a new PHP_CodeSniffer sniff that detects and fixes the ob_get_contents()/ob_end_clean() pattern.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03b80c3 and 4515a4a.

⛔ Files ignored due to path filters (1)
  • phpcs.xml is excluded by !**/*.xml
📒 Files selected for processing (16)
  • classes/controllers/FrmEntriesAJAXSubmitController.php
  • classes/controllers/FrmFieldsController.php
  • classes/controllers/FrmFormsController.php
  • classes/helpers/FrmAppHelper.php
  • classes/models/FrmStyle.php
  • classes/models/fields/FrmFieldCreditCard.php
  • classes/models/fields/FrmFieldDefault.php
  • classes/models/fields/FrmFieldType.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferObGetCleanSniff.php
  • stripe/helpers/FrmStrpLiteConnectHelper.php
  • stripe/models/FrmStrpLiteAuth.php
  • tests/phpunit/base/FrmUnitTest.php
  • tests/phpunit/forms/test_FrmFormsController.php
  • tests/phpunit/misc/test_FrmAppController.php
  • tests/phpunit/misc/test_FrmAppHelper.php
  • tests/phpunit/styles/test_FrmStylesController.php
🧰 Additional context used
🧬 Code graph analysis (2)
classes/models/fields/FrmFieldType.php (2)
classes/controllers/FrmFieldsController.php (1)
  • input_html (527-552)
classes/models/fields/FrmFieldRadio.php (1)
  • input_html (42-44)
classes/models/fields/FrmFieldDefault.php (4)
classes/controllers/FrmFieldsController.php (1)
  • input_html (527-552)
classes/models/fields/FrmFieldType.php (1)
  • input_html (227-229)
classes/models/fields/FrmFieldCheckbox.php (1)
  • input_html (37-39)
classes/models/fields/FrmFieldRadio.php (1)
  • input_html (42-44)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Cypress
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: Cypress
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: Cypress
🔇 Additional comments (24)
classes/controllers/FrmEntriesAJAXSubmitController.php (1)

81-83: LGTM!

Clean refactoring to consolidate output buffering into a single ob_get_clean() call. The change maintains the same functionality while being more concise.

classes/models/FrmStyle.php (1)

437-439: LGTM!

The inline use of ob_get_clean() within the nested function calls is clean and maintains the same functionality as the previous two-step pattern.

stripe/helpers/FrmStrpLiteConnectHelper.php (1)

643-645: LGTM!

Consistent application of the ob_get_clean() pattern for capturing template output. The refactoring is correct and aligns with the PR's objective.

stripe/models/FrmStrpLiteAuth.php (1)

71-73: LGTM!

The consolidation to ob_get_clean() is correct and maintains the same behavior while being more concise.

classes/helpers/FrmAppHelper.php (2)

1535-1544: LGTM!

The refactored control flow using a single return statement with ob_get_clean() is cleaner and maintains the same functionality. The logic is correct: when $echo is false, it captures and returns the buffer; otherwise returns null.


2661-2669: LGTM!

Clean consolidation of the buffer capture pattern. The direct return of ob_get_clean() is correct and more concise than the previous two-step approach.

classes/models/fields/FrmFieldType.php (2)

201-203: LGTM!

The refactor to ob_get_clean() correctly consolidates the two-step buffer operation into a single call while preserving the same semantics.


1522-1524: LGTM!

The refactor to ob_get_clean() correctly consolidates the buffer operation while preserving the same return value and behavior.

phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferObGetCleanSniff.php (2)

22-31: LGTM!

The sniff structure follows PHP_CodeSniffer conventions correctly. Registering for T_STRING tokens is appropriate for detecting function calls like ob_get_contents().


86-98: Auto-fix logic is sound.

The fixer correctly:

  1. Replaces ob_get_contents with ob_get_clean
  2. Removes the redundant ob_end_clean() call and everything between the two statements

The implementation achieves the PR's objective of consolidating the two-step buffer operation.

classes/controllers/FrmFieldsController.php (1)

51-53: LGTM!

The refactor to ob_get_clean() correctly consolidates the buffer operation while maintaining the same behavior for capturing field HTML.

tests/phpunit/styles/test_FrmStylesController.php (1)

22-24: LGTM!

Both test methods correctly refactor to ob_get_clean(), consolidating the buffer operations while preserving the same test behavior and assertions.

Also applies to: 72-74

classes/models/fields/FrmFieldCreditCard.php (1)

55-66: LGTM!

Directly returning ob_get_clean() is a clean simplification that eliminates the need for an intermediate variable while maintaining the same functionality.

tests/phpunit/misc/test_FrmAppHelper.php (3)

463-466: LGTM! Clean buffer management.

The refactoring from ob_get_contents() + ob_end_clean() to ob_get_clean() is correct and more concise.


479-482: LGTM! Consistent refactoring.

Correctly applies the same buffer consolidation pattern.


496-499: LGTM! Consistent pattern applied.

All three buffer management refactorings in this file follow the same correct pattern.

tests/phpunit/forms/test_FrmFormsController.php (1)

370-373: LGTM! Simplified buffer handling.

The post_new_entry method correctly uses ob_get_clean() to capture and return the output from FrmEntriesController::process_entry() while cleaning the buffer in a single operation.

classes/controllers/FrmFormsController.php (1)

2506-2518: LGTM! Production code refactoring is safe.

The get_form method correctly consolidates buffer operations. The change from ob_get_contents() + ob_end_clean() to ob_get_clean() maintains identical behavior while being more concise.

tests/phpunit/base/FrmUnitTest.php (2)

462-466: LGTM! Clean buffer management in test utility.

The get_footer_output method correctly uses ob_get_clean() to capture and return footer output.


586-596: LGTM! Buffer refactoring plus resource management improvement.

The change to ob_get_clean() on line 588 is correct. Additionally, the explicit fclose($fw) on line 595 is a good improvement for proper resource cleanup, even though it's beyond the scope of this PR's primary objective.

tests/phpunit/misc/test_FrmAppController.php (2)

25-28: LGTM! Consistent buffer handling.

The test_menu method correctly uses ob_get_clean() to capture menu output.


84-88: LGTM! Consistent pattern applied.

The test_load_wp_admin_style method correctly uses ob_get_clean() to capture style output.

classes/models/fields/FrmFieldDefault.php (2)

38-41: LGTM! Clean refactoring to idiomatic PHP.

Using ob_get_clean() is the preferred PHP pattern for capturing buffered output, combining ob_get_contents() and ob_end_clean() into a single call. This improves code clarity without changing behavior.


61-64: LGTM! Consistent refactoring with proper safety checks.

The ob_get_clean() refactoring matches the pattern in show_on_form_builder. The defensive is_string() check on line 66 appropriately handles the (unlikely) case where ob_get_clean() returns false.

@Crabcyborg Crabcyborg merged commit c67b709 into master Jan 9, 2026
36 of 38 checks passed
@Crabcyborg Crabcyborg deleted the new_sniff_to_prefer_ob_get_clean branch January 9, 2026 22:49
stephywells pushed a commit that referenced this pull request Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant