Skip to content

Commit

Permalink
Merge pull request #10502 from kkmuffme/warn-for-non-strict-compariso…
Browse files Browse the repository at this point in the history
…ns-on-truthy-falsy-types

report error for non-strict or empty comparison on truthy+falsy union
  • Loading branch information
danog authored Jan 17, 2024
2 parents b958349 + a3a1324 commit 680c8cd
Show file tree
Hide file tree
Showing 30 changed files with 2,314 additions and 70 deletions.
1 change: 1 addition & 0 deletions config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
<xs:element name="ReferenceReusedFromConfusingScope" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ReservedWord" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RiskyCast" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RiskyTruthyFalsyComparison" type="IssueHandlerType" minOccurs="0" />
<xs:element name="StringIncrement" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TaintedCallable" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TaintedCookie" type="IssueHandlerType" minOccurs="0" />
Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/error_levels.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Level 5 and above allows a more non-verifiable code, and higher levels are even
- [RedundantConditionGivenDocblockType](issues/RedundantConditionGivenDocblockType.md)
- [RedundantFunctionCallGivenDocblockType](issues/RedundantFunctionCallGivenDocblockType.md)
- [ReferenceConstraintViolation](issues/ReferenceConstraintViolation.md)
- [RiskyTruthyFalsyComparison](issues/RiskyTruthyFalsyComparison.md)
- [UndefinedTrace](issues/UndefinedTrace.md)
- [UnresolvableInclude](issues/UnresolvableInclude.md)
- [UnsafeInstantiation](issues/UnsafeInstantiation.md)
Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
- [ReferenceReusedFromConfusingScope](issues/ReferenceReusedFromConfusingScope.md)
- [ReservedWord](issues/ReservedWord.md)
- [RiskyCast](issues/RiskyCast.md)
- [RiskyTruthyFalsyComparison](issues/RiskyTruthyFalsyComparison.md)
- [StringIncrement](issues/StringIncrement.md)
- [TaintedCallable](issues/TaintedCallable.md)
- [TaintedCookie](issues/TaintedCookie.md)
Expand Down
29 changes: 29 additions & 0 deletions docs/running_psalm/issues/RiskyTruthyFalsyComparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# RiskyTruthyFalsyComparison

Emitted when comparing a value with multiple types that can both contain truthy and falsy values.

```php
<?php

/**
* @param array|null $arg
* @return void
*/
function foo($arg) {
if ($arg) {
// this is risky, bc the empty array and null case are handled together
}

if (!$arg) {
// this is risky, bc the empty array and null case are handled together
}
}
```

## Why this is bad

The truthy/falsy type of one variable is often forgotten and not handled explicitly causing hard to track down errors.

## How to fix

Explicitly validate the variable with strict comparison.
Loading

0 comments on commit 680c8cd

Please sign in to comment.