1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 11:26:55 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php

222 lines
7.5 KiB
PHP
Raw Normal View History

2018-01-14 18:09:40 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer\Statements\Block;
2018-01-14 18:09:40 +01:00
use PhpParser;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ScopeAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Clause;
2018-01-14 18:09:40 +01:00
use Psalm\Context;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Scope\LoopScope;
2018-01-14 18:09:40 +01:00
use Psalm\Type;
2018-11-10 20:06:31 +01:00
use Psalm\Type\Algebra;
2018-01-14 18:09:40 +01:00
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class DoAnalyzer
2018-01-14 18:09:40 +01:00
{
/**
* @return void
2018-01-14 18:09:40 +01:00
*/
public static function analyze(
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer,
2018-01-14 18:09:40 +01:00
PhpParser\Node\Stmt\Do_ $stmt,
Context $context
) {
$do_context = clone $context;
$do_context->inside_case = false;
2018-11-11 18:01:14 +01:00
$codebase = $statements_analyzer->getCodebase();
2018-11-06 03:57:36 +01:00
if ($codebase->alter_code) {
$do_context->branch_point = $do_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
}
2018-01-14 18:09:40 +01:00
$loop_scope = new LoopScope($do_context, $context);
$loop_scope->protected_var_ids = $context->protected_var_ids;
2018-11-11 18:01:14 +01:00
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
2018-07-06 03:03:44 +02:00
$do_context->loop_scope = $loop_scope;
2018-11-11 18:01:14 +01:00
$statements_analyzer->analyze($stmt->stmts, $do_context);
2018-01-14 18:09:40 +01:00
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
2018-11-11 18:01:14 +01:00
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
2018-11-10 20:06:31 +01:00
$loop_scope->iteration_count++;
2018-01-14 18:09:40 +01:00
foreach ($context->vars_in_scope as $var => $type) {
if ($type->hasMixed()) {
2018-01-14 18:09:40 +01:00
continue;
}
if ($do_context->hasVariable($var)) {
if ($do_context->vars_in_scope[$var]->getId() !== $type->getId()) {
$do_context->vars_in_scope[$var] = Type::combineUnionTypes($do_context->vars_in_scope[$var], $type);
2018-01-14 18:09:40 +01:00
}
}
}
2018-01-29 23:18:03 +01:00
$mixed_var_ids = [];
foreach ($do_context->vars_in_scope as $var_id => $type) {
if ($type->hasMixed()) {
2018-01-29 23:18:03 +01:00
$mixed_var_ids[] = $var_id;
}
}
2018-11-10 20:06:31 +01:00
$while_clauses = Algebra::getFormula(
2018-01-29 23:18:03 +01:00
$stmt->cond,
$context->self,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
2018-11-06 03:57:36 +01:00
$codebase
2018-01-29 23:18:03 +01:00
);
$while_clauses = array_values(
array_filter(
$while_clauses,
/** @return bool */
function (Clause $c) use ($mixed_var_ids) {
$keys = array_keys($c->possibilities);
foreach ($keys as $key) {
foreach ($mixed_var_ids as $mixed_var_id) {
if (preg_match('/^' . preg_quote($mixed_var_id, '/') . '(\[|-)/', $key)) {
return false;
}
}
}
return true;
}
)
);
if (!$while_clauses) {
$while_clauses = [new Clause([], true)];
}
2018-05-07 07:26:06 +02:00
$reconcilable_while_types = \Psalm\Type\Algebra::getTruthsFromFormula($while_clauses);
2018-01-29 23:18:03 +01:00
if ($reconcilable_while_types) {
$changed_var_ids = [];
$while_vars_in_scope_reconciled =
Type\Reconciler::reconcileKeyedTypes(
$reconcilable_while_types,
$do_context->vars_in_scope,
$changed_var_ids,
[],
2018-11-11 18:01:14 +01:00
$statements_analyzer,
[],
true,
new \Psalm\CodeLocation($statements_analyzer->getSource(), $stmt->cond)
2018-01-29 23:18:03 +01:00
);
$do_context->vars_in_scope = $while_vars_in_scope_reconciled;
}
2019-01-07 07:15:30 +01:00
foreach ($do_context->vars_in_scope as $var_id => $type) {
if (isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
}
}
2018-11-06 03:57:36 +01:00
LoopAnalyzer::analyze(
2018-11-11 18:01:14 +01:00
$statements_analyzer,
2018-01-25 20:19:22 +01:00
$stmt->stmts,
2018-01-29 23:18:03 +01:00
[$stmt->cond],
2018-01-25 20:19:22 +01:00
[],
$loop_scope,
$inner_loop_context,
true
2018-01-25 20:19:22 +01:00
);
2018-01-14 18:09:40 +01:00
// because it's a do {} while, inner loop vars belong to the main context
if (!$inner_loop_context) {
throw new \UnexpectedValueException('Should never be null');
}
2018-11-10 20:06:31 +01:00
$negated_while_clauses = Algebra::negateFormula($while_clauses);
$negated_while_types = Algebra::getTruthsFromFormula(
Algebra::simplifyCNF(
array_merge($context->clauses, $negated_while_clauses)
)
);
2018-11-11 18:01:14 +01:00
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->cond, $inner_loop_context);
2018-11-10 20:06:31 +01:00
if ($negated_while_types) {
$changed_var_ids = [];
$inner_loop_context->vars_in_scope =
Type\Reconciler::reconcileKeyedTypes(
$negated_while_types,
$inner_loop_context->vars_in_scope,
$changed_var_ids,
[],
2018-11-11 18:01:14 +01:00
$statements_analyzer,
[],
true,
new \Psalm\CodeLocation($statements_analyzer->getSource(), $stmt->cond)
2018-11-10 20:06:31 +01:00
);
}
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) {
2018-11-10 20:06:31 +01:00
// if there are break statements in the loop it's not certain
// that the loop has finished executing, so the assertions at the end
// the loop in the while conditional may not hold
2018-11-06 03:57:36 +01:00
if (in_array(ScopeAnalyzer::ACTION_BREAK, $loop_scope->final_actions, true)) {
2018-11-10 20:06:31 +01:00
if (isset($loop_scope->possibly_defined_loop_parent_vars[$var_id])) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
$type,
$loop_scope->possibly_defined_loop_parent_vars[$var_id]
);
}
} else {
$context->vars_in_scope[$var_id] = $type;
2018-01-14 18:09:40 +01:00
}
}
$do_context->loop_scope = null;
2018-01-14 18:09:40 +01:00
$context->vars_possibly_in_scope = array_merge(
$context->vars_possibly_in_scope,
$do_context->vars_possibly_in_scope
);
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$do_context->referenced_var_ids
);
if ($context->collect_references) {
2019-01-07 07:15:30 +01:00
$context->unreferenced_vars = $inner_loop_context->unreferenced_vars;
}
if ($context->collect_exceptions) {
$context->possibly_thrown_exceptions += $inner_loop_context->possibly_thrown_exceptions;
}
2018-01-14 18:09:40 +01:00
}
}