Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrong handling of flags in context #7195

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
Original file line number Diff line number Diff line change
@@ -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();
@@ -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);
Original file line number Diff line number Diff line change
@@ -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,
Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -712,6 +712,8 @@ private static function analyzeNestedArrayAssignment(
$child_stmt->dim,
$context
) === false) {
$context->inside_general_use = $was_inside_general_use;

return;
}

Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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);
@@ -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];
}
@@ -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];
}
@@ -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;
}
@@ -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;
}

@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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)
@@ -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
@@ -1229,6 +1229,8 @@ private static function handleByRefFunctionArg(
$arg->value,
$context
) === false) {
$context->inside_assignment = $was_inside_assignment;

return false;
}

Original file line number Diff line number Diff line change
@@ -163,6 +163,8 @@ function ($arg) {
$args[$i]->value,
$context
) === false) {
$context->inside_assignment = $was_inside_assignment;

return false;
}

Original file line number Diff line number Diff line change
@@ -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;
}

@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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);

Original file line number Diff line number Diff line change
@@ -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);

Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ public static function analyze(
$always_non_empty_array
) === false
) {
$context->inside_call = $was_inside_call;

return false;
}

2 changes: 2 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions src/Psalm/Internal/Codebase/Methods.php
Original file line number Diff line number Diff line change
@@ -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(