Skip to content

New sniff to catch another redundant empty case#2879

Merged
Crabcyborg merged 2 commits into
masterfrom
new_sniff_to_catch_another_redundant_empty_case
Jan 26, 2026
Merged

New sniff to catch another redundant empty case#2879
Crabcyborg merged 2 commits into
masterfrom
new_sniff_to_catch_another_redundant_empty_case

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 26, 2026

This issue in errors.php was throwing an error in PHPStan. It's easily detectable and auto-fixable, so I've added a new sniff.

Summary by CodeRabbit

  • Refactor

    • Simplified conditional logic in error handling
  • Chores

    • Added new static analysis rules to enforce code quality standards
    • Enhanced strict error detection in code analysis tooling

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 26, 2026

📝 Walkthrough

Walkthrough

This PR introduces a new PHPCS sniff that detects and automatically fixes redundant empty() checks following truthy checks (patterns like $var && !empty($var) simplified to $var). The sniff is enabled in the ruleset, applied to an existing file, and PHPStan configuration is adjusted to reflect the simplified code.

Changes

Cohort / File(s) Summary
PHPCS Sniff Implementation
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveRedundantEmptyAfterTruthySniff.php
New sniff class (~330 lines) that detects $var && !empty($var) patterns by analyzing tokens around boolean AND operators. Includes helper methods to extract operands and pattern components, reports fixable errors, and automatically rewrites matched patterns to simplified form using PHPCS token replacements.
Code Simplification
classes/views/frm-entries/errors.php
Simplified redundant condition from $img && !empty($img) to truthy check if ($img) before rendering error icon.
Configuration Updates
phpcs-sniffs/Formidable/ruleset.xml
Enabled two new rules in CodeAnalysis section: RedundantParentheses and RemoveRedundantEmptyAfterTruthy.
PHPStan Configuration
phpstan.neon
Removed frm-entries/errors.php from the ignore rule for "always exists and is not falsy" error, allowing PHPStan to report that error for the file.

Sequence Diagram

sequenceDiagram
    participant PHPCS as PHPCS Framework
    participant Sniff as RemoveRedundantEmptyAfterTruthySniff
    participant Tokens as Token Stream

    PHPCS->>Tokens: Parse PHP file into tokens
    PHPCS->>Sniff: process(File, stackPtr) at T_BOOLEAN_AND
    Sniff->>Tokens: Extract left operand tokens
    Sniff->>Tokens: Extract right operand tokens
    alt Pattern Match: $var && !empty($var)
        Sniff->>Sniff: Extract variable name
        Sniff->>PHPCS: Report fixable error
        Sniff->>Tokens: Replace tokens: remove && !empty(...)
        Sniff->>Tokens: Adjust whitespace
        Note over Sniff: Simplified to: $var
    else No Match
        Sniff->>PHPCS: Continue to next token
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

Poem

🐰 A sniff so keen hops through your code,
Finds $var && !empty($var) on the road!
Poof! Simplified to just $var,
Cleaner logic, shining far! ⭐
PHPStan watches close, configuration glows bright. ✨

🚥 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 accurately describes the main change: introducing a new PHPCS sniff to detect redundant empty() checks in logical AND conditions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

@Crabcyborg Crabcyborg merged commit b61e581 into master Jan 26, 2026
35 of 38 checks passed
@Crabcyborg Crabcyborg deleted the new_sniff_to_catch_another_redundant_empty_case branch January 26, 2026 16:05
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