New sniff to return early before expensive code#2848
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughAdds an early-return in Changes
Sequence Diagram(s)(Skipped — changes do not introduce a multi-component runtime flow suitable for sequence diagrams.) Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Fix all issues with AI agents
In `@classes/models/FrmEntry.php`:
- Around line 90-93: There is an extra/missing blank line around the new
early-return after applying filters which triggers the
blank_line_before_statement rule; adjust spacing so there is exactly one blank
line before the "if ( ! isset( $values['item_meta'] ) ) { return; }" statement
(or run the project's PHP CS fixer) immediately after the apply_filters call in
the same block where $check_val is set and the early-return is added; ensure the
apply_filters($check_val) line and the if ( ! isset( $values['item_meta'] ) )
return; follow the repository's blank-line conventions.
- Around line 90-99: The is_duplicate method currently has two early exits that
use bare returns (return;) which yield null; change both early exits to
explicitly return false so the function matches its `@return` bool signature and
works with strict comparisons—specifically update the branches that check for
missing $values['item_meta'] and for !$entry_exists in FrmEntry::is_duplicate
(references: $values['item_meta'] check and $entry_exists from FrmDb::get_col)
to return false instead of a bare return.
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/MoveSimpleCheckBeforeExpensiveCallSniff.php`:
- Around line 366-455: The phpdoc union types in this file are ordered
incorrectly and trip the phpdoc_types_order rule; update the `@return` annotations
so falses come first (e.g., change "string|false" to "false|string" on
extractVariableName(), change "array|false" to "false|array" on
findPrecedingAssignment(), and likewise any "int|false" to "false|int" in the
following docblock for findStatementStart()), or run the project's phpdoc fixer
to apply the same ordering across the file.
- Around line 98-127: isEarlyReturnPattern currently treats any "return <expr>;"
as an early-return but applyFix always inserts "return;" which can change
behavior; update the logic so you either (A) restrict isEarlyReturnPattern to
only match bare returns by checking that the token immediately after T_RETURN is
a semicolon (i.e. confirm no expression tokens between T_RETURN and the
semicolon) before returning true, or (B) preserve the return expression by, in
applyFix, extracting the tokens between the T_RETURN and the semicolon inside
the matched scope and re-inserting that exact expression when rebuilding the
moved early-return; refer to isEarlyReturnPattern and applyFix to locate the
checks and the insertion logic and implement one of these two safer approaches.
🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/MoveSimpleCheckBeforeExpensiveCallSniff.php (2)
180-182: Remove unused$tokensincheckForOptimization.PHPMD flags
$tokensas unused. It can be dropped safely.♻️ Suggested cleanup
- $tokens = $phpcsFile->getTokens(); -
229-256: Drop unused parameters/variables inapplyFix.
$conditionOpener,$conditionCloser, and$scopeOpeneraren’t used. Removing them quiets PHPMD and simplifies the signature.♻️ Suggested cleanup
- $this->applyFix( $phpcsFile, $stackPtr, $assignmentData, $simpleCheckPart, $variableCheckPart, $conditionOpener, $conditionCloser ); + $this->applyFix( $phpcsFile, $stackPtr, $assignmentData, $simpleCheckPart, $variableCheckPart ); @@ - private function applyFix( File $phpcsFile, $stackPtr, $assignmentData, $simpleCheckPart, $variableCheckPart, $conditionOpener, $conditionCloser ) { + private function applyFix( File $phpcsFile, $stackPtr, $assignmentData, $simpleCheckPart, $variableCheckPart ) { @@ - $scopeOpener = $tokens[ $stackPtr ]['scope_opener']; $scopeCloser = $tokens[ $stackPtr ]['scope_closer'];
…fore_expensive_code New sniff to return early before expensive code
Summary by CodeRabbit
New Features
Bug Fixes
Chores
✏️ Tip: You can customize this high-level summary in your review settings.