2016-10-22 23:35:59 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Block;
|
2016-10-22 23:35:59 +02: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;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Scope\LoopScope;
|
2018-11-10 22:10:59 +01:00
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_merge;
|
|
|
|
use function in_array;
|
|
|
|
use function array_intersect_key;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ForAnalyzer
|
2016-10-22 23:35:59 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @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
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Stmt\For_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-12-17 16:58:03 +01:00
|
|
|
$pre_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
$context->assigned_var_ids = [];
|
|
|
|
|
2020-01-04 19:05:23 +01:00
|
|
|
$init_var_types = [];
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
foreach ($stmt->init as $init) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $init, $context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-04 19:05:23 +01:00
|
|
|
|
|
|
|
if ($init instanceof PhpParser\Node\Expr\Assign
|
|
|
|
&& $init->var instanceof PhpParser\Node\Expr\Variable
|
2020-01-04 19:09:55 +01:00
|
|
|
&& \is_string($init->var->name)
|
2020-01-04 19:05:23 +01:00
|
|
|
&& ($init_var_type = $statements_analyzer->node_data->getType($init->expr))
|
|
|
|
) {
|
|
|
|
$init_var_types[$init->var->name] = $init_var_type;
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
$assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
|
|
|
$context->assigned_var_ids = array_merge(
|
|
|
|
$pre_assigned_var_ids,
|
|
|
|
$assigned_var_ids
|
|
|
|
);
|
|
|
|
|
2017-12-07 05:46:41 +01:00
|
|
|
$while_true = !$stmt->cond && !$stmt->init && !$stmt->loop;
|
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
$pre_context = null;
|
|
|
|
|
|
|
|
if ($while_true) {
|
|
|
|
$pre_context = clone $context;
|
|
|
|
}
|
2017-12-07 05:46:41 +01:00
|
|
|
|
2017-04-11 23:43:46 +02:00
|
|
|
$for_context = clone $context;
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
$for_context->inside_loop = true;
|
2018-11-10 23:29:59 +01:00
|
|
|
$for_context->inside_case = false;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-21 22:24:20 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($codebase->alter_code) {
|
2018-01-21 22:24:20 +01:00
|
|
|
$for_context->branch_point = $for_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:46:41 +01:00
|
|
|
$loop_scope = new LoopScope($for_context, $context);
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
$loop_scope->protected_var_ids = array_merge(
|
|
|
|
$assigned_var_ids,
|
|
|
|
$context->protected_var_ids
|
|
|
|
);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
LoopAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-12-03 00:28:18 +01:00
|
|
|
$stmt->stmts,
|
|
|
|
$stmt->cond,
|
|
|
|
$stmt->loop,
|
2017-12-07 05:46:41 +01:00
|
|
|
$loop_scope,
|
|
|
|
$inner_loop_context
|
2017-12-03 00:28:18 +01:00
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
if (!$inner_loop_context) {
|
|
|
|
throw new \UnexpectedValueException('There should be an inner loop context');
|
|
|
|
}
|
|
|
|
|
|
|
|
$always_enters_loop = false;
|
|
|
|
|
|
|
|
foreach ($stmt->cond as $cond) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($cond_type = $statements_analyzer->node_data->getType($cond)) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($cond_type->getAtomicTypes() as $iterator_type) {
|
2018-11-10 22:10:59 +01:00
|
|
|
$always_enters_loop = $iterator_type instanceof Type\Atomic\TTrue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 19:05:23 +01:00
|
|
|
|
2020-01-04 19:12:22 +01:00
|
|
|
if (\count($stmt->init) === 1
|
|
|
|
&& \count($stmt->cond) === 1
|
2020-01-04 19:09:55 +01:00
|
|
|
&& $cond instanceof PhpParser\Node\Expr\BinaryOp
|
2020-01-04 19:05:23 +01:00
|
|
|
&& $cond->right instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
&& $cond->left instanceof PhpParser\Node\Expr\Variable
|
2020-01-04 19:09:55 +01:00
|
|
|
&& \is_string($cond->left->name)
|
2020-01-04 19:05:23 +01:00
|
|
|
&& isset($init_var_types[$cond->left->name])
|
|
|
|
&& $init_var_types[$cond->left->name]->isSingleIntLiteral()
|
|
|
|
) {
|
|
|
|
$init_value = $init_var_types[$cond->left->name]->getSingleIntLiteral()->value;
|
|
|
|
$cond_value = $cond->right->value;
|
|
|
|
|
|
|
|
if ($cond instanceof PhpParser\Node\Expr\BinaryOp\Smaller && $init_value < $cond_value) {
|
|
|
|
$always_enters_loop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cond instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual && $init_value <= $cond_value) {
|
|
|
|
$always_enters_loop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cond instanceof PhpParser\Node\Expr\BinaryOp\Greater && $init_value > $cond_value) {
|
|
|
|
$always_enters_loop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cond instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual && $init_value >= $cond_value) {
|
|
|
|
$always_enters_loop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($while_true) {
|
|
|
|
$always_enters_loop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$can_leave_loop = !$while_true
|
2018-11-06 03:57:36 +01:00
|
|
|
|| in_array(ScopeAnalyzer::ACTION_BREAK, $loop_scope->final_actions, true);
|
2018-11-10 22:10:59 +01:00
|
|
|
|
|
|
|
if ($always_enters_loop && $can_leave_loop) {
|
|
|
|
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) {
|
|
|
|
// if there are break statements in the loop it's not certain
|
|
|
|
// that the loop has finished executing
|
2018-11-06 03:57:36 +01:00
|
|
|
if (in_array(ScopeAnalyzer::ACTION_BREAK, $loop_scope->final_actions, true)
|
|
|
|
|| in_array(ScopeAnalyzer::ACTION_CONTINUE, $loop_scope->final_actions, true)
|
2018-11-10 22:10:59 +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]
|
|
|
|
);
|
2017-12-07 05:46:41 +01:00
|
|
|
}
|
2018-11-10 22:10:59 +01:00
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
2017-12-07 05:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
$for_context->loop_scope = null;
|
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
if ($can_leave_loop) {
|
2017-12-07 05:46:41 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$for_context->vars_possibly_in_scope
|
|
|
|
);
|
2018-11-10 22:10:59 +01:00
|
|
|
} elseif ($pre_context) {
|
2017-12-07 05:46:41 +01:00
|
|
|
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2019-05-21 00:25:11 +02:00
|
|
|
$context->referenced_var_ids = array_intersect_key(
|
|
|
|
$for_context->referenced_var_ids,
|
|
|
|
$context->referenced_var_ids
|
|
|
|
);
|
2018-01-25 07:04:26 +01:00
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
if ($context->collect_references) {
|
2019-05-21 00:25:11 +02:00
|
|
|
foreach ($for_context->unreferenced_vars as $var_id => $locations) {
|
2020-01-04 17:16:53 +01:00
|
|
|
if (isset($loop_scope->referenced_var_ids[$var_id])) {
|
|
|
|
$statements_analyzer->registerVariableUses($locations);
|
|
|
|
} elseif (isset($context->unreferenced_vars[$var_id])) {
|
2019-05-21 00:25:11 +02:00
|
|
|
$context->unreferenced_vars[$var_id] += $locations;
|
|
|
|
} else {
|
|
|
|
$context->unreferenced_vars[$var_id] = $locations;
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->collect_exceptions) {
|
2019-03-29 00:43:14 +01:00
|
|
|
$context->mergeExceptions($for_context);
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|