Adjust sniffs to avoid false positive / breaking changes in Views code#2855
Conversation
📝 WalkthroughWalkthroughAdds defensive checks to multiple sniffs: ensure SimplifyEmptyTernary only rewrites when the variable is definitely set; avoid fixes when isset/empty involve array-access; and mark inline-ternary tokens as complex for SimplifyIfReturn to prevent unsafe simplifications. Changes
Sequence Diagram(s)(Skipped — changes are intra-sniff control-flow and helpers, not multi-component sequential flows.) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 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: 3
🤖 Fix all issues with AI agents
In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyEmptyTernarySniff.php`:
- Around line 190-197: The phpdoc `@return` union ordering violates
phpdoc_types_order; update the return type order in the docblock for the method
that "Find the containing function for a token." by swapping the union from
"int|false" to "false|int" (i.e., change the `@return` annotation so false comes
before int) to satisfy php-cs-fixer.
- Around line 166-168: The local variable $tokens in the
isVariableDefinitelySet( File $phpcsFile, $stackPtr, $variableName ) method is
declared but never used; remove the unused assignment so the function no longer
creates $tokens from $phpcsFile->getTokens(), leaving the rest of the method
intact and relying on $phpcsFile directly where needed.
- Around line 198-213: The findContainingFunction method currently returns the
first function/closure in $tokens[$stackPtr]['conditions'], which picks the
outermost scope; change it to return the innermost function/closure instead by
selecting the last matching condition key in the $conditions array (e.g.,
iterate conditions in reverse or pick the last key where $conditionType ===
T_FUNCTION || $conditionType === T_CLOSURE) so nested closures are handled
correctly and outer parameters are not treated as defined inside inner closures.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyEmptyTernarySniff.php`:
- Around line 304-315: The current loop over $conditions (using
$conditionPtr/$conditionType) only rejects a hardcoded list and misses nested
closures/functions; instead determine the outer function's scope boundaries from
the $functionToken (use $tokens[$functionToken]['scope_opener'] and
['scope_closer'] or the file API to get the function scope) and inside the
foreach return false for any $conditionPtr !== $functionToken whose scope_opener
(or token position) lies strictly inside the function's scope (i.e. nested scope
start > function scope opener), which will reject T_CLOSURE/T_FUNCTION and any
other inner scopes rather than relying on a fixed token list.
♻️ Duplicate comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyEmptyTernarySniff.php (2)
166-168: Remove unused$tokens.Line 167 assigns
$tokensbut it’s never used, which triggers PHPMD.🧹 Proposed fix
- $tokens = $phpcsFile->getTokens();
198-213: Return the innermost function/closure.Line 207-210 returns the first matching condition, which is the outermost scope. In nested closures this can incorrectly treat outer parameters as “definitely set.” Prefer the innermost match.
✅ Safer selection (innermost)
- foreach ( $conditions as $conditionPtr => $conditionType ) { - if ( $conditionType === T_FUNCTION || $conditionType === T_CLOSURE ) { - return $conditionPtr; - } - } - - return false; + $functionToken = false; + foreach ( $conditions as $conditionPtr => $conditionType ) { + if ( $conditionType === T_FUNCTION || $conditionType === T_CLOSURE ) { + $functionToken = $conditionPtr; + } + } + + return $functionToken;
…positive_changes Adjust sniffs to avoid false positive / breaking changes in Views code
These address some issues I'm seeing applying sniffs in Views.
Summary by CodeRabbit
Bug Fixes
Behavior Change
✏️ Tip: You can customize this high-level summary in your review settings.