2016-10-22 23:35:59 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
2017-12-07 05:46:41 +01:00
|
|
|
use Psalm\Checker\ScopeChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Context;
|
2017-12-03 00:28:18 +01:00
|
|
|
use Psalm\Scope\LoopScope;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
|
|
class WhileChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Stmt\While_ $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 23:35:59 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyze(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Stmt\While_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-12-07 05:46:41 +01:00
|
|
|
$while_true = $stmt->cond
|
|
|
|
&& ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch && $stmt->cond->name->parts === ['true'])
|
|
|
|
|| ($stmt->cond instanceof PhpParser\Node\Scalar\LNumber && $stmt->cond->value > 0);
|
|
|
|
|
|
|
|
$pre_context = null;
|
|
|
|
|
|
|
|
if ($while_true) {
|
|
|
|
$pre_context = clone $context;
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_context = clone $context;
|
|
|
|
|
2018-01-21 22:24:20 +01:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
if ($project_checker->alter_code) {
|
|
|
|
$while_context->branch_point = $while_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:46:41 +01:00
|
|
|
$loop_scope = new LoopScope($while_context, $context);
|
2017-12-17 16:58:03 +01:00
|
|
|
$loop_scope->protected_var_ids = $context->protected_var_ids;
|
2017-12-07 05:46:41 +01:00
|
|
|
|
2017-11-30 07:07:20 +01:00
|
|
|
LoopChecker::analyze(
|
|
|
|
$statements_checker,
|
2017-11-28 06:46:41 +01:00
|
|
|
$stmt->stmts,
|
|
|
|
$stmt->cond ? [$stmt->cond] : [],
|
|
|
|
[],
|
2017-12-07 05:46:41 +01:00
|
|
|
$loop_scope,
|
|
|
|
$inner_loop_context
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2017-12-07 05:46:41 +01:00
|
|
|
if ($inner_loop_context && $while_true) {
|
|
|
|
// if we actually leave the loop
|
|
|
|
if (in_array(ScopeChecker::ACTION_BREAK, $loop_scope->final_actions, true)
|
|
|
|
|| in_array(ScopeChecker::ACTION_END, $loop_scope->final_actions, true)
|
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$while_true
|
|
|
|
|| in_array(ScopeChecker::ACTION_BREAK, $loop_scope->final_actions, true)
|
|
|
|
|| in_array(ScopeChecker::ACTION_END, $loop_scope->final_actions, true)
|
|
|
|
|| !$pre_context
|
|
|
|
) {
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$while_context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope = $pre_context->vars_in_scope;
|
|
|
|
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
|
|
|
|
}
|
2017-02-21 23:41:54 +01:00
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$while_context->referenced_var_ids
|
|
|
|
);
|
2017-02-01 05:24:33 +01:00
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
2018-01-28 23:28:34 +01:00
|
|
|
$context->unreferenced_vars = $while_context->unreferenced_vars;
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|