1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 03:17:02 +01:00
psalm/src/Psalm/Checker/Statements/Block/ForChecker.php

125 lines
3.9 KiB
PHP
Raw Normal View History

2016-10-22 23:35:59 +02:00
<?php
namespace Psalm\Checker\Statements\Block;
use PhpParser;
use Psalm\Checker\ScopeChecker;
2016-10-22 23:35:59 +02:00
use Psalm\Checker\Statements\ExpressionChecker;
2016-11-02 07:29:00 +01:00
use Psalm\Checker\StatementsChecker;
use Psalm\Context;
2017-12-03 00:28:18 +01:00
use Psalm\Scope\LoopScope;
2016-10-22 23:35:59 +02:00
class ForChecker
{
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Stmt\For_ $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
*/
public static function analyze(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Stmt\For_ $stmt,
Context $context
) {
$pre_assigned_var_ids = $context->assigned_var_ids;
$context->assigned_var_ids = [];
2016-10-22 23:35:59 +02:00
foreach ($stmt->init as $init) {
2017-04-11 23:43:46 +02:00
if (ExpressionChecker::analyze($statements_checker, $init, $context) === false) {
2016-10-22 23:35:59 +02:00
return false;
}
}
$assigned_var_ids = $context->assigned_var_ids;
$context->assigned_var_ids = array_merge(
$pre_assigned_var_ids,
$assigned_var_ids
);
$while_true = !$stmt->cond && !$stmt->init && !$stmt->loop;
$pre_context = $while_true ? clone $context : null;
2017-04-11 23:43:46 +02:00
$for_context = clone $context;
$for_context->inside_loop = true;
$project_checker = $statements_checker->getFileChecker()->project_checker;
if ($project_checker->alter_code) {
$for_context->branch_point = $for_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
}
$loop_scope = new LoopScope($for_context, $context);
$loop_scope->protected_var_ids = array_merge(
$assigned_var_ids,
$context->protected_var_ids
);
2017-12-03 00:28:18 +01:00
LoopChecker::analyze(
$statements_checker,
$stmt->stmts,
$stmt->cond,
$stmt->loop,
$loop_scope,
$inner_loop_context
2017-12-03 00:28:18 +01:00
);
2016-10-22 23:35:59 +02: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,
$for_context->vars_possibly_in_scope
);
$context->possibly_assigned_var_ids =
$for_context->possibly_assigned_var_ids + $context->possibly_assigned_var_ids;
} else {
$context->vars_in_scope = $pre_context->vars_in_scope;
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
}
2016-11-02 07:29:00 +01:00
$context->referenced_var_ids =
$for_context->referenced_var_ids + $context->referenced_var_ids;
if ($context->collect_references) {
$context->unreferenced_vars = array_intersect_key(
$for_context->unreferenced_vars,
$context->unreferenced_vars
);
}
if ($context->collect_references) {
$context->unreferenced_vars = array_intersect_key(
$for_context->unreferenced_vars,
$context->unreferenced_vars
);
}
if ($context->collect_exceptions) {
$context->possibly_thrown_exceptions += $for_context->possibly_thrown_exceptions;
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 23:35:59 +02:00
}
}