Skip to content

Commit

Permalink
Add empty-with-boolean-expression for php (#3560)
Browse files Browse the repository at this point in the history
Detects when:

    empty($foo) && $bar

is accidentally written as

    empty($foo && $bar)
  • Loading branch information
Sjord authored Feb 13, 2025
1 parent d1ab2dd commit 6901ed2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions php/lang/correctness/empty-with-boolean-expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// ok: empty-with-boolean-expression
if (!empty($params['name']) && !empty($params['pass'])) {
}
// ruleid: empty-with-boolean-expression
elseif (!empty($params['name'] && !empty($params['pass']))) {
}

// ok: empty-with-boolean-expression
if (!empty($params['name']) || !empty($params['pass'])) {
}
// ruleid: empty-with-boolean-expression
elseif (!empty($params['name'] || !empty($params['pass']))) {
}
16 changes: 16 additions & 0 deletions php/lang/correctness/empty-with-boolean-expression.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rules:
- id: empty-with-boolean-expression
pattern-either:
- pattern: |
empty($A && $B)
- pattern: |
empty($A || $B)
message: >-
Calling `empty` on a boolean expression may be an indication that a parenthesis is misplaced.
metadata:
category: correctness
technology:
- php
confidence: LOW
languages: [php]
severity: WARNING

0 comments on commit 6901ed2

Please sign in to comment.