Skip to content

more tolerant rule along the lines of booleansInConditions ? #270

@shish

Description

@shish

booleansInConditions is very strict, which is great and all (kind of the point of this project), but makes it a bit of a pain to enable in a large legacy codebase -- I believe (and may well be wrong) that should be possible to accept a significantly wider variety of inputs and still be equally correct - specifically: allowing non-booleans so long as only one of the types in the union can be false-y, eg

  • MyClass|null - if($var) is checking for object vs null, that's ok
  • bool|null - if($var) is ambiguous if we're checking for false or null, reject
  • array - if($var) can only be checking for empty-array vs non-empty-array, allow
  • array|null - is ambiguous, reject
  • non-empty-array|null - if($var) can only mean one thing, allow
  • 0|1 - allowed
  • 0|1|false - 0 and false are meaningfully different, we should reject and force the dev to handle it
  • etc

(I currently have booleansInConditions disabled because it makes a lot of noise in my large legacy project, and nearly all the instances that it flags are cases that I would personally consider to be false-positives -- but I just got bitten by a bug where I was handling a bool|null and forgot to handle the false and null cases separately... FWIW my gut says it's weird that I'm willing to accept multiple-potential-truths but want my linter to reject multiple-potential-falseys - but scanning my codebases, "multiple potential truths" is always fine in practice and "multiple false-y types all being treated the same" is nearly-always a bug, so maybe a rule like this would be broadly valuable?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions