1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/src/Psalm/Checker/Statements/Block/SwitchChecker.php

400 lines
16 KiB
PHP
Raw Normal View History

2016-10-22 19:37:06 +02:00
<?php
namespace Psalm\Checker\Statements\Block;
use PhpParser;
use Psalm\Checker\AlgebraChecker;
2016-10-22 19:37:06 +02:00
use Psalm\Checker\ScopeChecker;
use Psalm\Checker\Statements\ExpressionChecker;
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
use Psalm\Issue\ContinueOutsideLoop;
2016-11-02 07:29:00 +01:00
use Psalm\IssueBuffer;
2017-12-03 00:28:18 +01:00
use Psalm\Scope\LoopScope;
2016-10-22 19:37:06 +02:00
use Psalm\Type;
2018-05-07 07:26:06 +02:00
use Psalm\Type\Algebra;
use Psalm\Type\Reconciler;
2016-10-22 19:37:06 +02:00
class SwitchChecker
{
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Stmt\Switch_ $stmt
* @param Context $context
2017-05-27 02:16:18 +02:00
*
2016-11-02 07:29:00 +01:00
* @return false|null
2016-10-22 19:37:06 +02:00
*/
public static function analyze(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Stmt\Switch_ $stmt,
Context $context,
2017-12-03 00:28:18 +01:00
LoopScope $loop_scope = null
2016-11-02 07:29:00 +01:00
) {
if (ExpressionChecker::analyze($statements_checker, $stmt->cond, $context) === false) {
2016-10-22 19:37:06 +02:00
return false;
}
$original_context = clone $context;
$new_vars_in_scope = null;
$new_vars_possibly_in_scope = [];
$redefined_vars = null;
$possibly_redefined_vars = null;
2016-10-22 19:37:06 +02:00
// the last statement always breaks, by default
$last_case_exit_type = 'break';
$case_exit_types = new \SplFixedArray(count($stmt->cases));
$has_default = false;
$case_action_map = [];
2016-10-22 19:37:06 +02:00
// create a map of case statement -> ultimate exit type
2017-05-27 02:05:57 +02:00
for ($i = count($stmt->cases) - 1; $i >= 0; --$i) {
2016-10-22 19:37:06 +02:00
$case = $stmt->cases[$i];
$case_actions = $case_action_map[$i] = ScopeChecker::getFinalControlActions($case->stmts, true);
if (!in_array(ScopeChecker::ACTION_NONE, $case_actions, true)) {
if ($case_actions === [ScopeChecker::ACTION_END]) {
$last_case_exit_type = 'return_throw';
} elseif ($case_actions === [ScopeChecker::ACTION_CONTINUE]) {
$last_case_exit_type = 'continue';
} elseif (in_array(ScopeChecker::ACTION_BREAK, $case_actions, true)) {
$last_case_exit_type = 'break';
}
2016-10-22 19:37:06 +02:00
}
$case_exit_types[$i] = $last_case_exit_type;
}
$leftover_statements = [];
$leftover_case_equality_expr = null;
2016-10-22 19:37:06 +02:00
$project_checker = $statements_checker->getFileChecker()->project_checker;
for ($i = 0, $l = count($stmt->cases); $i < $l; $i++) {
2016-10-22 19:37:06 +02:00
$case = $stmt->cases[$i];
2016-12-17 06:48:31 +01:00
/** @var string */
2016-10-22 19:37:06 +02:00
$case_exit_type = $case_exit_types[$i];
2017-09-11 17:52:34 +02:00
$case_actions = $case_action_map[$i];
// has a return/throw at end
$has_ending_statements = $case_actions === [ScopeChecker::ACTION_END];
$has_leaving_statements = $has_ending_statements
|| (count($case_actions) && !in_array(ScopeChecker::ACTION_NONE, $case_actions, true));
2017-09-11 17:52:34 +02:00
$case_context = clone $original_context;
if ($project_checker->alter_code) {
$case_context->branch_point = $case_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
}
2017-09-11 17:52:34 +02:00
$case_context->parent_context = $context;
$case_equality_expr = null;
2016-10-22 19:37:06 +02:00
if ($case->cond) {
if (ExpressionChecker::analyze($statements_checker, $case->cond, $case_context) === false) {
2016-10-22 19:37:06 +02:00
return false;
}
$switch_condition = $stmt->cond;
if ($switch_condition instanceof PhpParser\Node\Expr\Variable
&& is_string($switch_condition->name)
&& isset($context->vars_in_scope['$' . $switch_condition->name])
) {
$switch_var_type = $context->vars_in_scope['$' . $switch_condition->name];
$type_statements = [];
foreach ($switch_var_type->getTypes() as $type) {
if ($type instanceof Type\Atomic\GetClassT) {
$type_statements[] = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name(['get_class']),
[
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable(substr($type->typeof, 1))
2017-12-13 21:57:10 +01:00
),
]
);
} elseif ($type instanceof Type\Atomic\GetTypeT) {
$type_statements[] = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name(['gettype']),
[
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable(substr($type->typeof, 1))
2017-12-13 21:57:10 +01:00
),
]
);
} else {
$type_statements = null;
break;
}
}
if ($type_statements && count($type_statements) === 1) {
$switch_condition = $type_statements[0];
}
}
$case_equality_expr = new PhpParser\Node\Expr\BinaryOp\Equal(
$switch_condition,
$case->cond,
$case->cond->getAttributes()
);
}
$case_stmts = $case->stmts;
$case_stmts = array_merge($leftover_statements, $case_stmts);
if (!$case->cond) {
$has_default = true;
}
if (!$has_leaving_statements && $i !== $l - 1) {
if (!$case_equality_expr) {
$case_equality_expr = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name\FullyQualified(['rand']),
[
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\LNumber(0)),
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\LNumber(1)),
],
$case->getAttributes()
);
}
$leftover_case_equality_expr = $leftover_case_equality_expr
? new PhpParser\Node\Expr\BinaryOp\BooleanOr(
$leftover_case_equality_expr,
$case_equality_expr,
$case->cond ? $case->cond->getAttributes() : $case->getAttributes()
)
: $case_equality_expr;
$case_if_stmt = new PhpParser\Node\Stmt\If_(
$leftover_case_equality_expr,
['stmts' => $case_stmts]
);
$leftover_statements = [$case_if_stmt];
continue;
}
2017-09-11 17:52:34 +02:00
if ($leftover_case_equality_expr) {
$case_or_default_equality_expr = $case_equality_expr;
if (!$case_or_default_equality_expr) {
$case_or_default_equality_expr = new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name\FullyQualified(['rand']),
[
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\LNumber(0)),
new PhpParser\Node\Arg(new PhpParser\Node\Scalar\LNumber(1)),
],
$case->getAttributes()
);
}
$case_equality_expr = new PhpParser\Node\Expr\BinaryOp\BooleanOr(
$leftover_case_equality_expr,
$case_or_default_equality_expr,
$case_or_default_equality_expr->getAttributes()
);
}
$case_context->inside_case = true;
$leftover_statements = [];
$leftover_case_equality_expr = null;
if ($case_equality_expr) {
2018-05-07 07:26:06 +02:00
$case_clauses = Algebra::getFormula(
$case_equality_expr,
$context->self,
$statements_checker
);
2017-09-11 17:52:34 +02:00
// this will see whether any of the clauses in set A conflict with the clauses in set B
AlgebraChecker::checkForParadox(
$context->clauses,
$case_clauses,
$statements_checker,
$stmt->cond,
[]
);
2018-05-07 07:26:06 +02:00
$case_context->clauses = Algebra::simplifyCNF(array_merge($context->clauses, $case_clauses));
2018-05-07 07:26:06 +02:00
$reconcilable_if_types = Algebra::getTruthsFromFormula($case_context->clauses);
// if the if has an || in the conditional, we cannot easily reason about it
if ($reconcilable_if_types) {
$changed_var_ids = [];
$case_vars_in_scope_reconciled =
Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
$case_context->vars_in_scope,
$changed_var_ids,
[],
$statements_checker,
new CodeLocation($statements_checker->getSource(), $stmt->cond, $context->include_location),
$statements_checker->getSuppressedIssues()
);
$case_context->vars_in_scope = $case_vars_in_scope_reconciled;
foreach ($reconcilable_if_types as $var_id => $_) {
2017-12-02 19:32:20 +01:00
$case_context->vars_possibly_in_scope[$var_id] = true;
}
if ($changed_var_ids) {
$case_context->removeReconciledClauses($changed_var_ids);
}
2016-10-22 19:37:06 +02:00
}
}
2017-12-03 00:28:18 +01:00
$statements_checker->analyze($case_stmts, $case_context, $loop_scope);
2016-10-22 19:37:06 +02:00
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$case_context->referenced_var_ids
);
2016-10-22 19:37:06 +02:00
if ($case_exit_type !== 'return_throw') {
2016-11-02 07:29:00 +01:00
$vars = array_diff_key(
$case_context->vars_possibly_in_scope,
$original_context->vars_possibly_in_scope
);
2016-10-22 19:37:06 +02:00
// if we're leaving this block, add vars to outer for loop scope
if ($case_exit_type === 'continue') {
2017-12-03 00:28:18 +01:00
if ($loop_scope) {
$loop_scope->vars_possibly_in_scope = array_merge(
2016-11-02 07:29:00 +01:00
$vars,
2017-12-03 00:28:18 +01:00
$loop_scope->vars_possibly_in_scope
2016-11-02 07:29:00 +01:00
);
} else {
2016-10-22 19:37:06 +02:00
if (IssueBuffer::accepts(
new ContinueOutsideLoop(
2016-10-22 19:37:06 +02:00
'Continue called when not in loop',
new CodeLocation($statements_checker->getSource(), $case)
2016-10-22 19:37:06 +02:00
)
)) {
return false;
}
}
2016-11-02 07:29:00 +01:00
} else {
2017-12-03 00:28:18 +01:00
$case_redefined_vars = $case_context->getRedefinedVars($original_context->vars_in_scope);
2016-10-22 19:37:06 +02:00
if ($possibly_redefined_vars === null) {
$possibly_redefined_vars = $case_redefined_vars;
} else {
foreach ($case_redefined_vars as $var_id => $type) {
if (!isset($possibly_redefined_vars[$var_id])) {
$possibly_redefined_vars[$var_id] = $type;
} else {
$possibly_redefined_vars[$var_id] = Type::combineUnionTypes(
$type,
$possibly_redefined_vars[$var_id]
);
}
}
}
2016-10-22 19:37:06 +02:00
if ($redefined_vars === null) {
$redefined_vars = $case_redefined_vars;
2016-11-02 07:29:00 +01:00
} else {
foreach ($redefined_vars as $var_id => $type) {
if (!isset($case_redefined_vars[$var_id])) {
unset($redefined_vars[$var_id]);
} else {
$redefined_vars[$var_id] = Type::combineUnionTypes(
$type,
$case_redefined_vars[$var_id]
);
2016-10-22 19:37:06 +02:00
}
}
}
$context_new_vars = array_diff_key($case_context->vars_in_scope, $context->vars_in_scope);
2016-10-22 19:37:06 +02:00
if ($new_vars_in_scope === null) {
$new_vars_in_scope = $context_new_vars;
2016-11-02 07:29:00 +01:00
$new_vars_possibly_in_scope = array_diff_key(
$case_context->vars_possibly_in_scope,
$context->vars_possibly_in_scope
);
} else {
2016-12-28 20:38:16 +01:00
foreach ($new_vars_in_scope as $new_var => $type) {
if (!$case_context->hasVariable($new_var, $statements_checker)) {
2016-10-22 19:37:06 +02:00
unset($new_vars_in_scope[$new_var]);
} else {
2016-12-28 20:38:16 +01:00
$new_vars_in_scope[$new_var] =
Type::combineUnionTypes($case_context->vars_in_scope[$new_var], $type);
2016-10-22 19:37:06 +02:00
}
}
$new_vars_possibly_in_scope = array_merge(
array_diff_key(
$case_context->vars_possibly_in_scope,
$context->vars_possibly_in_scope
),
$new_vars_possibly_in_scope
);
}
}
}
if ($context->collect_references) {
foreach ($case_context->unreferenced_vars as $var_id => $location) {
if (isset($context->unreferenced_vars[$var_id])
&& $context->unreferenced_vars[$var_id] !== $location
) {
$context->hasVariable($var_id, $statements_checker);
}
}
$context->unreferenced_vars = array_merge(
$context->unreferenced_vars,
$case_context->unreferenced_vars
);
}
2016-10-22 19:37:06 +02:00
}
// only update vars if there is a default
// if that default has a throw/return/continue, that should be handled above
if ($has_default) {
if ($new_vars_in_scope) {
$context->vars_in_scope = array_merge($context->vars_in_scope, $new_vars_in_scope);
}
if ($redefined_vars) {
$context->vars_in_scope = array_merge($context->vars_in_scope, $redefined_vars);
}
if ($possibly_redefined_vars) {
foreach ($possibly_redefined_vars as $var_id => $type) {
if (!isset($redefined_vars[$var_id]) && !isset($new_vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id]
= Type::combineUnionTypes($type, $context->vars_in_scope[$var_id]);
}
}
}
} elseif ($possibly_redefined_vars) {
foreach ($possibly_redefined_vars as $var_id => $type) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($type, $context->vars_in_scope[$var_id]);
}
2016-10-22 19:37:06 +02:00
}
$context->vars_possibly_in_scope = array_merge($context->vars_possibly_in_scope, $new_vars_possibly_in_scope);
2017-05-25 04:07:49 +02:00
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:37:06 +02:00
}
}