Skip to content

Commit

Permalink
Fix issue where inside_conditional is forgotten
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 11, 2019
1 parent 2f9b1ff commit e476eed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php
Expand Up @@ -564,6 +564,8 @@ function (Clause $c) use ($changed_var_ids) {

$internally_applied_if_cond_expr = self::getDefinitelyEvaluatedExpressionInsideIf($cond);

$was_inside_conditional = $outer_context->inside_conditional;

$outer_context->inside_conditional = true;

$pre_condition_vars_in_scope = $outer_context->vars_in_scope;
Expand Down Expand Up @@ -602,7 +604,9 @@ function (Clause $c) use ($changed_var_ids) {
$first_cond_referenced_var_ids
);

$outer_context->inside_conditional = false;
if (!$was_inside_conditional) {
$outer_context->inside_conditional = false;
}

if (!$if_context) {
$if_context = clone $outer_context;
Expand Down
Expand Up @@ -267,6 +267,7 @@ private static function handleCase(
$old_node_data = $statements_analyzer->node_data;

if ($case->cond) {
$was_inside_conditional = $case_context->inside_conditional;
$case_context->inside_conditional = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $case->cond, $case_context) === false) {
Expand All @@ -278,7 +279,9 @@ private static function handleCase(
return false;
}

$case_context->inside_conditional = false;
if (!$was_inside_conditional) {
$case_context->inside_conditional = false;
}

$statements_analyzer->node_data = clone $statements_analyzer->node_data;

Expand Down
15 changes: 15 additions & 0 deletions tests/UnusedCodeTest.php
Expand Up @@ -642,6 +642,21 @@ function takesMixed($i) : int {
return $i;
}'
],
'usedFunctionCallInsideSwitchWithTernary' => [
'<?php
function getArg(string $method) : void {
switch (strtolower($method ?: "")) {
case "post":
break;
case "get":
break;
default:
break;
}
}'
]
];
}

Expand Down

0 comments on commit e476eed

Please sign in to comment.