2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-01-29 23:18:03 +01:00
|
|
|
use Psalm\Checker\AlgebraChecker;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
2018-01-29 23:18:03 +01:00
|
|
|
use Psalm\Clause;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Scope\LoopScope;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class DoChecker
|
|
|
|
{
|
|
|
|
/**
|
2018-02-07 00:44:53 +01:00
|
|
|
* @return void
|
2018-01-14 18:09:40 +01:00
|
|
|
*/
|
|
|
|
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-02-05 23:28:34 +01:00
|
|
|
$suppressed_issues = $statements_checker->getSuppressedIssues();
|
|
|
|
|
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->addSuppressedIssues(['RedundantCondition']);
|
|
|
|
}
|
2018-06-30 18:56:42 +02:00
|
|
|
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
|
|
|
}
|
2018-02-05 23:28:34 +01:00
|
|
|
|
2018-06-17 03:54:44 +02:00
|
|
|
$statements_checker->analyze($stmt->stmts, $do_context);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-02-05 23:28:34 +01:00
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->removeSuppressedIssues(['RedundantCondition']);
|
|
|
|
}
|
2018-06-30 18:56:42 +02:00
|
|
|
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
|
|
|
}
|
2018-02-05 23:28:34 +01:00
|
|
|
|
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-03-08 17:17:27 +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] = clone $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:18:03 +01:00
|
|
|
$mixed_var_ids = [];
|
|
|
|
|
|
|
|
foreach ($do_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
$mixed_var_ids[] = $var_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$while_clauses = \Psalm\Type\Algebra::getFormula(
|
2018-01-29 23:18:03 +01:00
|
|
|
$stmt->cond,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
$while_clauses = array_values(
|
|
|
|
array_filter(
|
|
|
|
$while_clauses,
|
|
|
|
/** @return bool */
|
|
|
|
function (Clause $c) use ($mixed_var_ids) {
|
|
|
|
$keys = array_keys($c->possibilities);
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
foreach ($mixed_var_ids as $mixed_var_id) {
|
|
|
|
if (preg_match('/^' . preg_quote($mixed_var_id, '/') . '(\[|-)/', $key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$while_clauses) {
|
|
|
|
$while_clauses = [new Clause([], true)];
|
|
|
|
}
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$reconcilable_while_types = \Psalm\Type\Algebra::getTruthsFromFormula($while_clauses);
|
2018-01-29 23:18:03 +01:00
|
|
|
|
|
|
|
if ($reconcilable_while_types) {
|
|
|
|
$changed_var_ids = [];
|
|
|
|
$while_vars_in_scope_reconciled =
|
|
|
|
Type\Reconciler::reconcileKeyedTypes(
|
|
|
|
$reconcilable_while_types,
|
|
|
|
$do_context->vars_in_scope,
|
|
|
|
$changed_var_ids,
|
|
|
|
[],
|
|
|
|
$statements_checker,
|
|
|
|
new \Psalm\CodeLocation($statements_checker->getSource(), $stmt->cond),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
$do_context->vars_in_scope = $while_vars_in_scope_reconciled;
|
|
|
|
}
|
|
|
|
|
2018-06-30 18:56:42 +02:00
|
|
|
$do_cond_context = clone $do_context;
|
|
|
|
|
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->addSuppressedIssues(['RedundantCondition']);
|
|
|
|
}
|
|
|
|
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionChecker::analyze($statements_checker, $stmt->cond, $do_cond_context);
|
|
|
|
|
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->removeSuppressedIssues(['RedundantCondition']);
|
|
|
|
}
|
|
|
|
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
|
|
|
|
$statements_checker->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
$do_context->unreferenced_vars = $do_cond_context->unreferenced_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($do_cond_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:19:22 +01:00
|
|
|
LoopChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->stmts,
|
2018-01-29 23:18:03 +01:00
|
|
|
[$stmt->cond],
|
2018-01-25 20:19:22 +01:00
|
|
|
[],
|
|
|
|
$loop_scope,
|
2018-02-06 17:07:27 +01:00
|
|
|
$inner_loop_context,
|
|
|
|
true
|
2018-01-25 20:19:22 +01:00
|
|
|
);
|
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
|
2018-02-07 00:44:53 +01:00
|
|
|
if (!$inner_loop_context) {
|
|
|
|
throw new \UnexpectedValueException('Should never be null');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-06-30 17:08:51 +02:00
|
|
|
ExpressionChecker::analyze($statements_checker, $stmt->cond, $inner_loop_context);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
2018-06-30 18:56:42 +02:00
|
|
|
$context->unreferenced_vars = $do_context->unreferenced_vars;
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
if ($context->collect_exceptions) {
|
2018-06-30 17:08:51 +02:00
|
|
|
$context->possibly_thrown_exceptions += $inner_loop_context->possibly_thrown_exceptions;
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|