Skip to content

Commit

Permalink
Ignore just-coerced vars
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 19, 2020
1 parent 45326b0 commit eee562c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php
Expand Up @@ -2002,6 +2002,13 @@ public static function addConditionallyAssignedVarsToContext(
Context $outer_context,
array $cond_assigned_var_ids
) : void {
// this filters out coercions to expeccted types in ArgumentAnalyzer
$cond_assigned_var_ids = \array_filter($cond_assigned_var_ids);

if (!$cond_assigned_var_ids) {
return;
}

$exprs = self::getDefinitelyEvaluatedOredExpressions($cond);

// if there was no assignment in the first expression it's safe to proceed
Expand Down
Expand Up @@ -1167,7 +1167,7 @@ private static function coerceValueAfterGatekeeperArgument(
}

if ($context->inside_conditional) {
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = false;
}

if ($was_cloned) {
Expand Down
15 changes: 15 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Expand Up @@ -2856,6 +2856,21 @@ function bar(App $foo, $arr) : void {
}
}'
],
'nonRedundantConditionAfterThing' => [
'<?php
class U {
public function takes(self $u) : bool {
return true;
}
}
function bar(?U $a, ?U $b) : void {
if ($a === null
|| ($b !== null && $a->takes($b))
|| $b === null
) {}
}'
],
];
}

Expand Down

0 comments on commit eee562c

Please sign in to comment.