1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-04 10:38:49 +01:00
psalm/docs/running_psalm/issues/RiskyTruthyFalsyComparison.md

30 lines
707 B
Markdown
Raw Normal View History

2024-01-12 22:52:26 +01:00
# RiskyTruthyFalsyComparison
Emitted when comparing a value with multiple types, where at least one type can be only truthy or falsy and other types can contain both truthy and falsy values.
2024-01-12 22:52:26 +01:00
```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 a variable is often forgotten and not handled explicitly causing hard to track down errors.
2024-01-12 22:52:26 +01:00
## How to fix
Explicitly validate the variable with strict comparison.