2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Scope\LoopScope;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class DoChecker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyze(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Stmt\Do_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$do_context = clone $context;
|
|
|
|
|
2018-01-21 22:24:20 +01:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
if ($project_checker->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-01-25 19:46:17 +01:00
|
|
|
$statements_checker->analyze($stmt->stmts, $do_context, $loop_scope);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($do_context->hasVariable($var)) {
|
2018-01-25 19:46:17 +01:00
|
|
|
if ($context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$do_context->vars_in_scope[$var] = $do_context->vars_in_scope[$var];
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($do_context->vars_in_scope[$var]->getId() !== $type->getId()) {
|
2018-01-25 19:46:17 +01:00
|
|
|
$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-25 20:19:22 +01:00
|
|
|
LoopChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->stmts,
|
|
|
|
$stmt->cond ? [$stmt->cond] : [],
|
|
|
|
[],
|
|
|
|
$loop_scope,
|
|
|
|
$inner_loop_context
|
|
|
|
);
|
2018-01-25 19:46:17 +01:00
|
|
|
|
2018-01-14 18:09:40 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// because it's a do {} while, inner loop vars belong to the main context
|
|
|
|
if ($inner_loop_context) {
|
|
|
|
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
2018-01-28 23:28:34 +01:00
|
|
|
$context->unreferenced_vars = $do_context->unreferenced_vars;
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return ExpressionChecker::analyze($statements_checker, $stmt->cond, $context);
|
|
|
|
}
|
|
|
|
}
|