Skip to content
Permalink
Browse files

Only warn about clauses in the currently-scanned conditional

  • Loading branch information
muglug committed Dec 8, 2019
1 parent 6236a30 commit 02fbf123378af92120726a599affd02ccc328fbf
@@ -32,7 +32,8 @@
"Psalm\\": "src/Psalm"
},
"files": [
"src/functions.php"
"src/functions.php",
"src/spl_object_id.php"
]
},
"autoload-dev": {
@@ -95,6 +95,7 @@ public static function analyze(
}

$while_clauses = Algebra::getFormula(
\spl_object_id($stmt->cond),
$stmt->cond,
$context->self,
$statements_analyzer,
@@ -134,6 +135,7 @@ function (Clause $c) use ($mixed_var_ids) {
$while_vars_in_scope_reconciled =
Type\Reconciler::reconcileKeyedTypes(
$reconcilable_while_types,
[],
$do_context->vars_in_scope,
$changed_var_ids,
[],
@@ -187,6 +189,7 @@ function (Clause $c) use ($mixed_var_ids) {
$inner_loop_context->vars_in_scope =
Type\Reconciler::reconcileKeyedTypes(
$negated_while_types,
[],
$inner_loop_context->vars_in_scope,
$changed_var_ids,
[],
@@ -105,6 +105,7 @@ public static function analyze(
}

$if_clauses = Algebra::getFormula(
\spl_object_id($stmt->cond),
$stmt->cond,
$context->self,
$statements_analyzer,
@@ -194,9 +195,13 @@ function ($c) use ($reconciled_expression_clauses) {
)
);

$active_if_types = [];

$reconcilable_if_types = Algebra::getTruthsFromFormula(
$if_context->clauses,
$cond_referenced_var_ids
\spl_object_id($stmt->cond),
$cond_referenced_var_ids,
$active_if_types
);

if (array_filter(
@@ -233,6 +238,7 @@ function ($carry, Clause $clause) {
$if_vars_in_scope_reconciled =
Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
$active_if_types,
$if_context->vars_in_scope,
$changed_var_ids,
$cond_referenced_var_ids,
@@ -280,9 +286,10 @@ function ($carry, Clause $clause) {
if ($if_scope->negated_types) {
$else_vars_reconciled = Reconciler::reconcileKeyedTypes(
$if_scope->negated_types,
[],
$temp_else_context->vars_in_scope,
$changed_var_ids,
$stmt->else || $stmt->elseifs ? $cond_referenced_var_ids : [],
[],
$statements_analyzer,
$statements_analyzer->getTemplateTypeMap() ?: [],
$context->inside_loop,
@@ -511,6 +518,7 @@ public static function analyzeIfConditional(
if ($if_scope->negated_types) {
$vars_reconciled = Reconciler::reconcileKeyedTypes(
$if_scope->negated_types,
[],
$outer_context->vars_in_scope,
$changed_var_ids,
[],
@@ -847,6 +855,7 @@ protected static function analyzeIfBlock(

$outer_context_vars_reconciled = Reconciler::reconcileKeyedTypes(
$if_scope->negated_types,
[],
$outer_context->vars_in_scope,
$changed_var_ids,
[],
@@ -1028,6 +1037,7 @@ protected static function analyzeElseIfBlock(
}

$elseif_clauses = Algebra::getFormula(
\spl_object_id($elseif->cond),
$elseif->cond,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -1109,6 +1119,8 @@ function ($c) use ($reconciled_expression_clauses) {

$elseif_context->clauses = Algebra::simplifyCNF($elseif_context_clauses);

$active_elseif_types = [];

try {
if (array_filter(
$entry_clauses,
@@ -1136,8 +1148,15 @@ function ($carry, Clause $clause) {
$omit_keys
);
}
$reconcilable_elseif_types = Algebra::getTruthsFromFormula($elseif_context->clauses);
$negated_elseif_types = Algebra::getTruthsFromFormula(Algebra::negateFormula($elseif_clauses));
$reconcilable_elseif_types = Algebra::getTruthsFromFormula(
$elseif_context->clauses,
\spl_object_id($elseif->cond),
$cond_referenced_var_ids,
$active_elseif_types
);
$negated_elseif_types = Algebra::getTruthsFromFormula(
Algebra::negateFormula($elseif_clauses)
);
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
$reconcilable_elseif_types = [];
$negated_elseif_types = [];
@@ -1169,6 +1188,7 @@ function ($carry, Clause $clause) {
if ($reconcilable_elseif_types) {
$elseif_vars_reconciled = Reconciler::reconcileKeyedTypes(
$reconcilable_elseif_types,
$active_elseif_types,
$elseif_context->vars_in_scope,
$changed_var_ids,
$cond_referenced_var_ids,
@@ -1357,6 +1377,7 @@ function ($carry, Clause $clause) {

$leaving_vars_reconciled = Reconciler::reconcileKeyedTypes(
$negated_elseif_types,
[],
$pre_conditional_context->vars_in_scope,
$changed_var_ids,
[],
@@ -1515,6 +1536,7 @@ protected static function analyzeElseBlock(

$else_vars_reconciled = Reconciler::reconcileKeyedTypes(
$else_types,
[],
$else_context->vars_in_scope,
$changed_var_ids,
[],
@@ -70,6 +70,7 @@ public static function analyze(
$pre_condition_clauses = array_merge(
$pre_condition_clauses,
Algebra::getFormula(
\spl_object_id($pre_condition),
$pre_condition,
$loop_scope->loop_context->self,
$statements_analyzer,
@@ -440,6 +441,7 @@ public static function analyze(

$vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$negated_pre_condition_types,
[],
$inner_context->vars_in_scope,
$changed_var_ids,
[],
@@ -594,8 +596,11 @@ private static function applyPreConditionToLoopContext(
array_merge($outer_context->clauses, $pre_condition_clauses)
);

$active_while_types = [];

$reconcilable_while_types = Algebra::getTruthsFromFormula(
$loop_context->clauses,
\spl_object_id($pre_condition),
$new_referenced_var_ids
);

@@ -604,6 +609,7 @@ private static function applyPreConditionToLoopContext(
if ($reconcilable_while_types) {
$pre_condition_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$reconcilable_while_types,
$active_while_types,
$loop_context->vars_in_scope,
$changed_var_ids,
$new_referenced_var_ids,
@@ -148,6 +148,7 @@ public static function analyze(
$case_vars_in_scope_reconciled =
Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
[],
$original_context->vars_in_scope,
$changed_var_ids,
[],
@@ -433,6 +434,7 @@ private static function handleCase(

if ($case_equality_expr) {
$case_clauses = Algebra::getFormula(
\spl_object_id($case_equality_expr),
$case_equality_expr,
$context->self,
$statements_analyzer,
@@ -487,6 +489,7 @@ private static function handleCase(
$case_vars_in_scope_reconciled =
Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
[],
$case_context->vars_in_scope,
$changed_var_ids,
$case->cond && $switch_var_id ? [$switch_var_id => true] : [],
@@ -78,6 +78,7 @@ public static function analyze(
$stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
) {
$left_clauses = Algebra::getFormula(
\spl_object_id($stmt->left),
$stmt->left,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -146,7 +147,14 @@ function ($c) use ($reconciled_expression_clauses) {

$simplified_clauses = Algebra::simplifyCNF($context_clauses);

$left_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses);
$active_left_assertions = [];

$left_type_assertions = Algebra::getTruthsFromFormula(
$simplified_clauses,
\spl_object_id($stmt->left),
$new_referenced_var_ids,
$active_left_assertions
);

$changed_var_ids = [];

@@ -157,6 +165,7 @@ function ($c) use ($reconciled_expression_clauses) {
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
$left_type_assertions,
$active_left_assertions,
$context->vars_in_scope,
$changed_var_ids,
$new_referenced_var_ids,
@@ -286,6 +295,7 @@ function ($c) {
$new_referenced_var_ids = array_diff_key($new_referenced_var_ids, $new_assigned_var_ids);

$left_clauses = Algebra::getFormula(
\spl_object_id($stmt->left),
$stmt->left,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -318,7 +328,14 @@ function ($c) use ($reconciled_expression_clauses) {
)
);

$negated_type_assertions = Algebra::getTruthsFromFormula($clauses_for_right_analysis);
$active_negated_type_assertions = [];

$negated_type_assertions = Algebra::getTruthsFromFormula(
$clauses_for_right_analysis,
\spl_object_id($stmt->left),
$new_referenced_var_ids,
$active_negated_type_assertions
);

$changed_var_ids = [];

@@ -329,6 +346,7 @@ function ($c) use ($reconciled_expression_clauses) {
// statements of the form if ($x === null || $x->foo())
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
$negated_type_assertions,
$active_negated_type_assertions,
$op_context->vars_in_scope,
$changed_var_ids,
$new_referenced_var_ids,
@@ -509,6 +527,7 @@ function ($c) {
$t_if_context = clone $context;

$if_clauses = Algebra::getFormula(
\spl_object_id($stmt),
$stmt,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -566,6 +585,7 @@ function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
if ($reconcilable_if_types) {
$t_if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
[],
$t_if_context->vars_in_scope,
$changed_var_ids,
[],
@@ -659,6 +679,7 @@ function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
if ($negated_if_types) {
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$negated_if_types,
[],
$t_else_context->vars_in_scope,
$changed_var_ids,
[],
@@ -582,6 +582,7 @@ public static function analyze(
isset($stmt->args[0])
) {
$assert_clauses = \Psalm\Type\Algebra::getFormula(
\spl_object_id($stmt->args[0]->value),
$stmt->args[0]->value,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -598,6 +599,7 @@ public static function analyze(
// while in an and, we allow scope to boil over to support
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
$assert_type_assertions,
$assert_type_assertions,
$context->vars_in_scope,
$changed_var_ids,
@@ -933,6 +935,7 @@ function (array $_) : bool {

if ($assertions) {
Reconciler::reconcileKeyedTypes(
$assertions,
$assertions,
$context->vars_in_scope,
$changed_vars,
@@ -3448,17 +3448,21 @@ protected static function applyAssertionsToContext(
}
} elseif ($arg_value && ($assertion->rule === [['!falsy']] || $assertion->rule === [['true']])) {
if ($assertion->rule === [['true']]) {
$conditional = new PhpParser\Node\Expr\BinaryOp\Identical(
$arg_value,
new PhpParser\Node\Expr\ConstFetch(new PhpParser\Node\Name('true'))
);

$assert_clauses = \Psalm\Type\Algebra::getFormula(
new PhpParser\Node\Expr\BinaryOp\Identical(
$arg_value,
new PhpParser\Node\Expr\ConstFetch(new PhpParser\Node\Name('true'))
),
\spl_object_id($conditional),
$conditional,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
$statements_analyzer->getCodebase()
);
} else {
$assert_clauses = \Psalm\Type\Algebra::getFormula(
\spl_object_id($arg_value),
$arg_value,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -3488,6 +3492,7 @@ protected static function applyAssertionsToContext(
// while in an and, we allow scope to boil over to support
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = \Psalm\Type\Reconciler::reconcileKeyedTypes(
$type_assertions,
$type_assertions,
$context->vars_in_scope,
$changed_var_ids,
@@ -63,6 +63,7 @@ public static function analyze(
$codebase = $statements_analyzer->getCodebase();

$if_clauses = \Psalm\Type\Algebra::getFormula(
\spl_object_id($stmt->cond),
$stmt->cond,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
@@ -132,13 +133,21 @@ function ($c) use ($reconciled_expression_clauses) {
)
);

$reconcilable_if_types = Algebra::getTruthsFromFormula($ternary_clauses, $cond_referenced_var_ids);
$active_if_types = [];

$reconcilable_if_types = Algebra::getTruthsFromFormula(
$ternary_clauses,
\spl_object_id($stmt->cond),
$cond_referenced_var_ids,
$active_if_types
);

$changed_var_ids = [];

if ($reconcilable_if_types) {
$if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
$active_if_types,
$if_context->vars_in_scope,
$changed_var_ids,
$cond_referenced_var_ids,
@@ -177,6 +186,7 @@ function ($c) use ($reconciled_expression_clauses) {

if ($negated_if_types) {
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$negated_if_types,
$negated_if_types,
$t_else_context->vars_in_scope,
$changed_var_ids,

0 comments on commit 02fbf12

Please sign in to comment.
You can’t perform that action at this time.