Skip to content

New sniff to prefer specialized phpunit assert functions to improve r…#2811

Merged
Crabcyborg merged 6 commits into
masterfrom
new_sniff_to_prefer_specialized_phpunit_assert_functions
Jan 15, 2026
Merged

New sniff to prefer specialized phpunit assert functions to improve r…#2811
Crabcyborg merged 6 commits into
masterfrom
new_sniff_to_prefer_specialized_phpunit_assert_functions

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 15, 2026

…eadability

Summary by CodeRabbit

  • New Features

    • Added a new code quality check that suggests improved PHPUnit assertions and automatically refactors generic type checks to more specific assertion methods.
  • Tests

    • Updated test suites to use more specific PHPUnit assertion methods for type validation.
    • Removed obsolete Stripe Lite test suite.
  • Chores

    • Updated code quality ruleset configuration.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 15, 2026

Warning

Rate limit exceeded

@Crabcyborg has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 3 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 229fc5d and dde852a.

📒 Files selected for processing (8)
  • phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertArrayHasKeySniff.php
  • phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php
  • phpcs-sniffs/Formidable/ruleset.xml
  • tests/phpunit/applications/test_FrmApplicationApi.php
  • tests/phpunit/applications/test_FrmApplicationsController.php
  • tests/phpunit/bootstrap.php
  • tests/phpunit/styles/test_FrmStyle.php
  • tests/phpunit/xml/test_FrmXMLHelper.php
📝 Walkthrough

Walkthrough

A new PHP CodeSniffer sniff detects and auto-refactors PHPUnit assertions from assertTrue/assertFalse with type-checking functions (is_array, is_object, is_string, is_numeric) to their corresponding assertIs* counterparts. The sniff is registered in the ruleset, test files are updated to use the recommended assertions, and a Stripe Lite test class is removed.

Changes

Cohort / File(s) Summary
CodeSniffer Sniff Implementation
phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php
New sniff class that registers for string tokens, detects assertTrue/assertFalse calls wrapping is_* functions, and provides automated fixes to refactor them to assertIs*/assertIsNot* methods with proper token rewriting and whitespace preservation.
CodeSniffer Configuration
phpcs-sniffs/Formidable/ruleset.xml
Registers the new Formidable.PHPUnit.PreferAssertIsArray sniff rule in the PHPUnit rules section.
Test Assertion Refactoring
tests/phpunit/applications/test_FrmApplicationApi.php, tests/phpunit/entries/test_FrmEntry.php, tests/phpunit/fields/test_FrmField.php, tests/phpunit/fields/test_FrmFieldsAjax.php, tests/phpunit/forms/test_FrmForm.php, tests/phpunit/misc/test_FrmAntiSpam.php, tests/phpunit/misc/test_FrmAppHelper.php, tests/phpunit/misc/test_FrmCSVExportHelper.php, tests/phpunit/xml/test_FrmXMLHelper.php
Replaces assertTrue/assertFalse with type-checking functions (is_array, is_object, is_string, is_numeric) with dedicated PHPUnit assertion methods (assertIsArray, assertIsObject, assertIsString, assertIsNumeric, assertIsNotNumeric) across multiple test files.
Stripe Test Removal
tests/phpunit/stripe/FrmStrpLiteUnitTest.php
Entire test class and all associated methods removed, including setup/teardown logic, Stripe resource creation helpers, and assertion utilities for FrmStrpLite integration testing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A sniff so clever, it sees the way,
assertTrue(is_array) has had its day,
Now assertIsArray takes the stage with grace,
While tests refactor across each place,
One Stripe test bids farewell to time,
As code grows cleaner, line by line!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding a new sniff to prefer specialized PHPUnit assert functions, which aligns with the core objective and implementation across all modified files.
Docstring Coverage ✅ Passed Docstring coverage is 89.47% which is sufficient. The required threshold is 80.00%.

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


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

🤖 Fix all issues with AI agents
In `@tests/phpunit/misc/test_FrmCSVExportHelper.php`:
- Line 167: The assertion uses assertIsString( $csv_path && $csv_path &&
file_exists( $csv_path ) ) which evaluates to a boolean and thus is wrong;
replace it by two separate assertions: verify that $csv_path is a string using
assertIsString($csv_path) (and optionally assertNotEmpty($csv_path)) and verify
the file exists using assertTrue(file_exists($csv_path)) (or
assertFileExists($csv_path)), updating the test around the $csv_path variable
and removing the combined boolean expression.
🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php (2)

29-41: Class name doesn't reflect full scope.

The class is named PreferAssertIsArraySniff but it handles is_array, is_object, is_string, and is_numeric. Consider renaming to something like PreferAssertIsTypeSniff or PreferSpecializedAssertSniff to accurately reflect its broader scope.

Additionally, the class docblock (lines 16-28) only documents is_array, is_object, and is_string but omits is_numeric which is present in FUNCTION_MAP.


150-150: Remove unused parameter $isFuncPtr.

The parameter $isFuncPtr is passed to applyFix() but never used within the method body. This was also flagged by static analysis.

♻️ Proposed fix

Update the method signature at line 150:

-	private function applyFix( File $phpcsFile, $methodNamePtr, $openParen, $isFuncPtr, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName ) {
+	private function applyFix( File $phpcsFile, $methodNamePtr, $openParen, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName ) {

Update the docblock (remove lines 142-143):

 	 * `@param` int    $methodNamePtr    The assertTrue/assertFalse token position.
 	 * `@param` int    $openParen        The opening parenthesis of assertTrue.
-	 * `@param` int    $isFuncPtr        The is_* function token position.
 	 * `@param` int    $isFuncOpenParen  The opening parenthesis of is_*.

Update the call site at line 132:

-			$this->applyFix( $phpcsFile, $stackPtr, $openParen, $firstArg, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName );
+			$this->applyFix( $phpcsFile, $stackPtr, $openParen, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName );
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 418d504 and 229fc5d.

📒 Files selected for processing (12)
  • phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php
  • phpcs-sniffs/Formidable/ruleset.xml
  • tests/phpunit/applications/test_FrmApplicationApi.php
  • tests/phpunit/entries/test_FrmEntry.php
  • tests/phpunit/fields/test_FrmField.php
  • tests/phpunit/fields/test_FrmFieldsAjax.php
  • tests/phpunit/forms/test_FrmForm.php
  • tests/phpunit/misc/test_FrmAntiSpam.php
  • tests/phpunit/misc/test_FrmAppHelper.php
  • tests/phpunit/misc/test_FrmCSVExportHelper.php
  • tests/phpunit/stripe/FrmStrpLiteUnitTest.php
  • tests/phpunit/xml/test_FrmXMLHelper.php
💤 Files with no reviewable changes (1)
  • tests/phpunit/stripe/FrmStrpLiteUnitTest.php
🧰 Additional context used
📓 Path-based instructions (1)
**/*.php

⚙️ CodeRabbit configuration file

**/*.php: - Do not make suggestions when we use x.x for a version placeholder. This is used like "@SInCE x.x" in a PHP doc comment, but also like 'x.x' when functions like _deprecated_function() are used.

  • Treat these placeholders as acceptable in this codebase.

Files:

  • tests/phpunit/misc/test_FrmAntiSpam.php
  • tests/phpunit/applications/test_FrmApplicationApi.php
  • tests/phpunit/entries/test_FrmEntry.php
  • tests/phpunit/misc/test_FrmAppHelper.php
  • tests/phpunit/fields/test_FrmField.php
  • tests/phpunit/xml/test_FrmXMLHelper.php
  • tests/phpunit/misc/test_FrmCSVExportHelper.php
  • phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php
  • tests/phpunit/fields/test_FrmFieldsAjax.php
  • tests/phpunit/forms/test_FrmForm.php
🪛 PHPMD (2.15.0)
phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php

150-150: Avoid unused parameters such as '$isFuncPtr'. (undefined)

(UnusedFormalParameter)

🔇 Additional comments (18)
phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php (1)

60-134: LGTM!

The detection and transformation logic is well-implemented. The sniff correctly:

  • Validates method call context ($this->, self::, static::)
  • Uses built-in parenthesis matching for reliability
  • Preserves additional arguments (like custom messages) by stopping at non-whitespace tokens (lines 170-176)
phpcs-sniffs/Formidable/ruleset.xml (1)

39-40: LGTM!

The new PHPUnit sniff rule is correctly registered and follows the existing ruleset structure.

tests/phpunit/xml/test_FrmXMLHelper.php (1)

103-103: LGTM!

The assertion change from assertTrue(is_array(...)) to assertIsArray(...) improves readability and provides better failure messages when the assertion fails.

tests/phpunit/entries/test_FrmEntry.php (1)

25-25: LGTM!

The change to assertIsNumeric() is consistent with the other type assertions in this file and aligns with the new sniff's guidelines.

tests/phpunit/fields/test_FrmField.php (1)

25-25: LGTM!

The change to assertIsNumeric() improves assertion clarity and will provide more descriptive failure messages.

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

99-99: LGTM!

Correct use of assertIsObject() to validate that $settings is an object instance.


539-539: LGTM!

Correct use of assertIsNotNumeric() with the failure message preserved.

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

21-21: LGTM!

Correct use of assertIsString() for token validation.


30-30: LGTM!

Correct use of assertIsString() for secret key validation.


39-39: LGTM!

Correct use of assertIsArray() for tokens validation.

tests/phpunit/applications/test_FrmApplicationApi.php (2)

15-15: LGTM!

Correct use of assertIsArray() for API response validation.


25-25: LGTM!

Correct use of assertIsArray() for application entry validation.

tests/phpunit/fields/test_FrmFieldsAjax.php (4)

24-24: LGTM!

Correct use of assertIsNumeric() for form ID validation.


47-52: LGTM!

Correct use of assertIsNumeric() for field ID and assertIsObject() for field object validation.


95-95: LGTM!

Correct use of assertIsObject() with the failure message preserved.


135-135: LGTM!

Correct use of assertIsNumeric() for field ID validation.

tests/phpunit/forms/test_FrmForm.php (2)

16-21: LGTM!

The change from assertTrue(is_numeric(...)) to assertIsNumeric() improves test readability and provides more informative failure messages. Good refactor.


26-36: LGTM!

Consistent application of the assertIsNumeric() assertion in test_duplicate(), matching the pattern used in test_create().

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment thread tests/phpunit/misc/test_FrmCSVExportHelper.php Outdated
@Crabcyborg Crabcyborg merged commit 212d1ab into master Jan 15, 2026
16 checks passed
@Crabcyborg Crabcyborg deleted the new_sniff_to_prefer_specialized_phpunit_assert_functions branch January 15, 2026 19:53
@coderabbitai coderabbitai Bot mentioned this pull request Jan 21, 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