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\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 in_array;
|
|
|
|
use function array_merge;
|
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 WhileAnalyzer
|
2016-10-22 23:35:59 +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\While_ $stmt,
|
|
|
|
Context $context
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?bool {
|
2018-02-06 19:40:28 +01:00
|
|
|
$while_true = ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch && $stmt->cond->name->parts === ['true'])
|
|
|
|
|| ($stmt->cond instanceof PhpParser\Node\Scalar\LNumber && $stmt->cond->value > 0);
|
2017-12-07 05:46:41 +01:00
|
|
|
|
|
|
|
$pre_context = null;
|
|
|
|
|
|
|
|
if ($while_true) {
|
|
|
|
$pre_context = clone $context;
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_context = clone $context;
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
$while_context->inside_loop = true;
|
2020-01-27 18:17:12 +01:00
|
|
|
$while_context->break_types[] = 'loop';
|
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
|
|
|
$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
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (LoopAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-11-28 06:46:41 +01:00
|
|
|
$stmt->stmts,
|
2020-03-28 23:22:00 +01:00
|
|
|
self::getAndExpressions($stmt->cond),
|
2017-11-28 06:46:41 +01:00
|
|
|
[],
|
2017-12-07 05:46:41 +01:00
|
|
|
$loop_scope,
|
|
|
|
$inner_loop_context
|
2018-03-07 17:16:56 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
if (!$inner_loop_context) {
|
|
|
|
throw new \UnexpectedValueException('Should always enter loop');
|
|
|
|
}
|
|
|
|
|
|
|
|
$always_enters_loop = false;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_cond_type = $statements_analyzer->node_data->getType($stmt->cond)) {
|
2018-11-10 22:10:59 +01:00
|
|
|
$always_enters_loop = true;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_cond_type->getAtomicTypes() as $iterator_type) {
|
2018-11-10 22:10:59 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TArray
|
2020-08-30 17:44:14 +02:00
|
|
|
|| $iterator_type instanceof Type\Atomic\TKeyedArray
|
2018-11-10 22:10:59 +01:00
|
|
|
) {
|
2020-08-30 17:44:14 +02:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TKeyedArray) {
|
2018-11-10 22:10:59 +01:00
|
|
|
if (!$iterator_type->sealed) {
|
|
|
|
$always_enters_loop = false;
|
|
|
|
}
|
|
|
|
} elseif (!$iterator_type instanceof Type\Atomic\TNonEmptyArray) {
|
|
|
|
$always_enters_loop = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iterator_type instanceof Type\Atomic\TTrue) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iterator_type instanceof Type\Atomic\TLiteralString
|
|
|
|
&& $iterator_type->value
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iterator_type instanceof Type\Atomic\TLiteralInt
|
|
|
|
&& $iterator_type->value
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$always_enters_loop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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, 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)
|
|
|
|
|| 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
|
|
|
$while_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,
|
|
|
|
$while_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;
|
|
|
|
}
|
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
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
2020-03-28 23:22:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return list<PhpParser\Node\Expr>
|
|
|
|
*/
|
|
|
|
private static function getAndExpressions(
|
|
|
|
PhpParser\Node\Expr $expr
|
|
|
|
) : array {
|
|
|
|
if ($expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
|
|
|
|
return array_merge(
|
|
|
|
self::getAndExpressions($expr->left),
|
|
|
|
self::getAndExpressions($expr->right)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$expr];
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|