Skip to content

Commit

Permalink
Empty checks variables are really falsy checks
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 24, 2020
1 parent 5ff3f13 commit 94e26b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Expand Up @@ -484,7 +484,16 @@ public static function scrapeAssertions(
);

if ($var_name) {
$if_types[$var_name] = [['empty']];
if ($conditional->expr instanceof PhpParser\Node\Expr\Variable
&& $source instanceof StatementsAnalyzer
&& ($var_type = $source->node_data->getType($conditional->expr))
&& !$var_type->isMixed()
&& !$var_type->possibly_undefined
) {
$if_types[$var_name] = [['falsy']];
} else {
$if_types[$var_name] = [['empty']];
}
}

return $if_types;
Expand Down
13 changes: 13 additions & 0 deletions tests/TypeReconciliation/TypeAlgebraTest.php
Expand Up @@ -1271,6 +1271,19 @@ function foo(string $input) : string {
}',
'error_message' => 'ParadoxicalCondition',
],
'mismatchingChecks' => [
'<?php
function doesntFindBug(?string $old, ?string $new): void {
if (empty($old) && empty($new)) {
return;
}
if (($old && empty($new)) || ($new && empty($old))) {
return;
}
}',
'error_message' => 'RedundantCondition',
],
];
}
}

0 comments on commit 94e26b2

Please sign in to comment.