Skip to content

Commit

Permalink
Fix #3631 - apply assertions to RHS of equality in conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jun 22, 2020
1 parent 29eb830 commit 9c17795
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/FunctionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ public static function getReturnTypeFromCallMapWithArgs(
return clone $array_type->type_param;
}
} elseif ($first_arg_type->hasScalarType()
&& isset($call_args[1])
&& ($second_arg = $call_args[1]->value)
&& ($second_arg = ($call_args[1]->value ?? null))
&& ($second_arg_type = $statements_analyzer->node_data->getType($second_arg))
&& $second_arg_type->hasScalarType()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ public static function scrapeAssertions(
return $if_types;
}

if ($conditional instanceof PhpParser\Node\Expr\Assign) {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->var,
$this_class_name,
$source
);

$if_types = self::scrapeAssertions(
$conditional->expr,
$this_class_name,
$source,
$codebase,
$inside_negation,
$cache
);

if ($var_name) {
$if_types[$var_name] = [['!falsy']];
}

return $if_types;
}

$var_name = ExpressionIdentifier::getArrayVarId(
$conditional,
$this_class_name,
Expand All @@ -139,20 +162,6 @@ public static function scrapeAssertions(
}
}

if ($conditional instanceof PhpParser\Node\Expr\Assign) {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->var,
$this_class_name,
$source
);

if ($var_name) {
$if_types[$var_name] = [['!falsy']];
}

return $if_types;
}

if ($conditional instanceof PhpParser\Node\Expr\BooleanNot) {
$expr_assertions = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public static function fetch(

if (InternalCallMapHandler::inCallMap((string) $call_map_id)) {
if (($template_result->upper_bounds || $class_storage->stubbed)
&& isset($class_storage->methods[$method_id->method_name])
&& ($method_storage = $class_storage->methods[$method_id->method_name])
&& ($method_storage = ($class_storage->methods[$method_id->method_name] ?? null))
&& $method_storage->return_type
) {
$return_type_candidate = clone $method_storage->return_type;
Expand Down
14 changes: 14 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,20 @@ function sayHi(string $greeting): void {
$a->format("d-m-Y");
}',
],
'applyTruthyAssertionsToRightHandSideOfAssignment' => [
'<?php
function takesAString(string $name): void {}
function randomReturn(): ?string {
return rand(1,2) === 1 ? "foo" : null;
}
$name = randomReturn();
if ($foo = ($name !== null)) {
takesAString($name);
}'
],
];
}

Expand Down

0 comments on commit 9c17795

Please sign in to comment.