Skip to content

Commit

Permalink
Merge pull request #7195 from orklah/switch_weirdness
Browse files Browse the repository at this point in the history
fix wrong handling of flags in context
  • Loading branch information
orklah committed Dec 21, 2021
2 parents 98f8044 + ca25b0f commit ea22f87
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 36 deletions.
Expand Up @@ -223,6 +223,8 @@ public static function analyze(
$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}
$context->inside_general_use = $was_inside_general_use;
Expand Down
Expand Up @@ -134,9 +134,7 @@ function (Clause $c) use ($changed_var_ids): bool {
$first_cond_referenced_var_ids
);

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

if (!$if_context) {
$if_context = clone $outer_context;
Expand All @@ -163,13 +161,15 @@ function (Clause $c) use ($changed_var_ids): bool {
$referenced_var_ids = $first_cond_referenced_var_ids;
$if_conditional_context->referenced_var_ids = [];

$was_inside_conditional = $if_conditional_context->inside_conditional;

$if_conditional_context->inside_conditional = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $cond, $if_conditional_context) === false) {
throw new ScopeAnalysisException();
}

$if_conditional_context->inside_conditional = false;
$if_conditional_context->inside_conditional = $was_inside_conditional;

/** @var array<string, bool> */
$more_cond_referenced_var_ids = $if_conditional_context->referenced_var_ids;
Expand Down
Expand Up @@ -622,6 +622,8 @@ private static function applyPreConditionToLoopContext(
$pre_referenced_var_ids = $loop_context->referenced_var_ids;
$loop_context->referenced_var_ids = [];

$was_inside_conditional = $loop_context->inside_conditional;

$loop_context->inside_conditional = true;

$suppressed_issues = $statements_analyzer->getSuppressedIssues();
Expand All @@ -637,10 +639,12 @@ private static function applyPreConditionToLoopContext(
}

if (ExpressionAnalyzer::analyze($statements_analyzer, $pre_condition, $loop_context) === false) {
$loop_context->inside_conditional = $was_inside_conditional;

return [];
}

$loop_context->inside_conditional = false;
$loop_context->inside_conditional = $was_inside_conditional;

$new_referenced_var_ids = $loop_context->referenced_var_ids;
$loop_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
Expand Down
Expand Up @@ -31,12 +31,17 @@ public static function analyze(
): void {
$codebase = $statements_analyzer->getCodebase();

$was_inside_conditional = $context->inside_conditional;

$context->inside_conditional = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->cond, $context) === false) {
$context->inside_conditional = $was_inside_conditional;

return;
}

$context->inside_conditional = false;
$context->inside_conditional = $was_inside_conditional;

$switch_var_id = ExpressionIdentifier::getArrayVarId(
$stmt->cond,
Expand Down
Expand Up @@ -114,9 +114,7 @@ public static function analyze(
return false;
}

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

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

Expand Down
Expand Up @@ -311,6 +311,8 @@ private static function analyzeArrayItem(
$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $item->key, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}
$context->inside_general_use = $was_inside_general_use;
Expand Down
Expand Up @@ -712,6 +712,8 @@ private static function analyzeNestedArrayAssignment(
$child_stmt->dim,
$context
) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}

Expand Down
Expand Up @@ -82,6 +82,8 @@ public static function analyze(
$context->inside_general_use = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $prop_name, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}

Expand Down
Expand Up @@ -236,6 +236,8 @@ public static function analyze(
}

if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_value, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

if ($var_id) {
if ($array_var_id) {
$context->removeDescendents($array_var_id, null, $assign_value_type);
Expand Down Expand Up @@ -551,9 +553,7 @@ public static function analyze(

$context->vars_in_scope[$var_id] = Type::getNull();

if (!$was_in_assignment) {
$context->inside_assignment = false;
}
$context->inside_assignment = $was_in_assignment;

return $context->vars_in_scope[$var_id];
}
Expand All @@ -571,9 +571,7 @@ public static function analyze(

$context->vars_in_scope[$var_id] = Type::getEmpty();

if (!$was_in_assignment) {
$context->inside_assignment = false;
}
$context->inside_assignment = $was_in_assignment;

return $context->vars_in_scope[$var_id];
}
Expand Down Expand Up @@ -612,9 +610,7 @@ public static function analyze(
}
}

if (!$was_in_assignment) {
$context->inside_assignment = false;
}
$context->inside_assignment = $was_in_assignment;

return $assign_value_type;
}
Expand Down Expand Up @@ -1507,10 +1503,14 @@ private static function analyzePropertyAssignment(
// this can happen when the user actually means to type $this-><autocompleted>, but there's
// a variable on the next line
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->var, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}

if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->name, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}

Expand Down Expand Up @@ -1699,6 +1699,8 @@ private static function analyzeAssignmentToVariable(
$context->inside_general_use = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->name, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}

Expand Down
Expand Up @@ -211,12 +211,12 @@ public static function analyze(
$context->inside_call = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}

if (!$was_inside_call) {
$context->inside_call = false;
}
$context->inside_call = $was_inside_call;

if (($argument_offset === 0 && $method_id === 'array_filter' && count($args) === 2)
|| ($argument_offset > 0 && $method_id === 'array_map' && count($args) >= 2)
Expand Down Expand Up @@ -1118,12 +1118,12 @@ private static function evaluateArbitraryParam(
$context->inside_call = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}

if (!$was_inside_call) {
$context->inside_call = false;
}
$context->inside_call = $was_inside_call;
}

if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch
Expand Down Expand Up @@ -1229,6 +1229,8 @@ private static function handleByRefFunctionArg(
$arg->value,
$context
) === false) {
$context->inside_assignment = $was_inside_assignment;

return false;
}

Expand Down
Expand Up @@ -163,6 +163,8 @@ function ($arg) {
$args[$i]->value,
$context
) === false) {
$context->inside_assignment = $was_inside_assignment;

return false;
}

Expand Down
Expand Up @@ -61,6 +61,8 @@ public static function analyze(
if ($existing_stmt_var_type) {
$statements_analyzer->node_data->setType($stmt->var, $existing_stmt_var_type);
} elseif (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}

Expand All @@ -70,6 +72,8 @@ public static function analyze(
$context->inside_call = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}
}
Expand Down
Expand Up @@ -186,6 +186,8 @@ public static function analyze(
$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}
$context->inside_general_use = $was_inside_general_use;
Expand All @@ -208,6 +210,8 @@ public static function analyze(
$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}
$context->inside_general_use = $was_inside_general_use;
Expand Down
Expand Up @@ -119,6 +119,9 @@ public static function analyze(
$context->inside_unset = false;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->dim, $context) === false) {
$context->inside_unset = $was_inside_unset;
$context->inside_general_use = $was_inside_general_use;

return false;
}

Expand Down
Expand Up @@ -380,6 +380,8 @@ public static function analyze(
$context->inside_general_use = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}

Expand Down
Expand Up @@ -51,6 +51,8 @@ public static function analyze(
}

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}

Expand Down
Expand Up @@ -31,15 +31,12 @@ public static function analyze(
$context->inside_assignment = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
if (!$was_inside_assignment) {
$context->inside_assignment = false;
}
$context->inside_assignment = $was_inside_assignment;

return false;
}

if (!$was_inside_assignment) {
$context->inside_assignment = false;
}
$context->inside_assignment = $was_inside_assignment;

$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);

Expand Down
Expand Up @@ -67,12 +67,12 @@ public static function analyze(
$context->inside_call = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}

if (!$was_inside_call) {
$context->inside_call = false;
}
$context->inside_call = $was_inside_call;

$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);

Expand Down
Expand Up @@ -25,6 +25,8 @@ public static function analyze(
$context->inside_general_use = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
$context->inside_general_use = $was_inside_general_use;

return false;
}

Expand Down
Expand Up @@ -47,6 +47,8 @@ public static function analyze(
$always_non_empty_array
) === false
) {
$context->inside_call = $was_inside_call;

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php
Expand Up @@ -453,6 +453,8 @@ private static function handleExpression(
$was_inside_call = $context->inside_call;
$context->inside_call = true;
if (PrintAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}
$context->inside_call = $was_inside_call;
Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/Internal/Codebase/Methods.php
Expand Up @@ -424,9 +424,7 @@ public function getMethodParams(
);
}

if (!$was_inside_call) {
$context->inside_call = false;
}
$context->inside_call = $was_inside_call;
}

$matching_callable = InternalCallMapHandler::getMatchingCallableFromCallMapOptions(
Expand Down

0 comments on commit ea22f87

Please sign in to comment.