2016-10-22 23:35:59 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-10-22 23:35:59 +02:00
|
|
|
use Psalm\Context;
|
2017-03-18 18:37:00 +01:00
|
|
|
use Psalm\Checker\AlgebraChecker;
|
2016-10-22 23:35:59 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2017-01-07 20:35:07 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssertionFinder;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2017-02-21 22:52:27 +01:00
|
|
|
use Psalm\Checker\ScopeChecker;
|
2016-10-22 23:35:59 +02:00
|
|
|
use Psalm\Checker\TypeChecker;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class WhileChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Stmt\While_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @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
|
|
|
|
) {
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_context = clone $context;
|
|
|
|
|
2017-02-13 00:47:02 +01:00
|
|
|
$while_context->inside_conditional = true;
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->cond, $while_context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-13 00:47:02 +01:00
|
|
|
$while_context->inside_conditional = false;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$while_clauses = AlgebraChecker::getFormula(
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->cond,
|
2017-01-31 07:30:11 +01:00
|
|
|
$context->self,
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
2017-04-02 21:26:10 +02:00
|
|
|
$while_context->parent_context = $context;
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$while_context->clauses = AlgebraChecker::simplifyCNF(array_merge($context->clauses, $while_clauses));
|
2017-01-31 07:30:11 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$reconcilable_while_types = AlgebraChecker::getTruthsFromFormula($while_context->clauses);
|
2017-01-31 07:30:11 +01:00
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
// if the while has an or as the main component, we cannot safely reason about it
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp &&
|
2017-02-17 03:00:45 +01:00
|
|
|
($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr ||
|
|
|
|
$stmt->cond instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-10-22 23:35:59 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-12-28 20:20:16 +01:00
|
|
|
$changed_vars = [];
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
|
2017-01-31 07:30:11 +01:00
|
|
|
$reconcilable_while_types,
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_context->vars_in_scope,
|
2016-12-28 20:20:16 +01:00
|
|
|
$changed_vars,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFileChecker(),
|
2016-12-11 19:59:36 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->cond),
|
2016-10-22 23:35:59 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($while_vars_in_scope_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$while_context->vars_in_scope = $while_vars_in_scope_reconciled;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:34:29 +01:00
|
|
|
$asserted_while_vars = array_keys(AlgebraChecker::getTruthsFromFormula($while_clauses));
|
2017-03-15 01:14:25 +01:00
|
|
|
|
2017-04-06 20:53:45 +02:00
|
|
|
$statements_checker->analyzeLoop($stmt->stmts, $asserted_while_vars, $while_context, $context);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($while_context->hasVariable($var)) {
|
2016-10-22 23:35:59 +02:00
|
|
|
if ($while_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $while_context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) $while_context->vars_in_scope[$var] !== (string) $type) {
|
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($while_context->vars_in_scope[$var], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 22:52:27 +01:00
|
|
|
if (!ScopeChecker::doesEverBreak($stmt->stmts)) {
|
|
|
|
// if the while contains an assertion and there are no break statements, we can negate that assertion
|
|
|
|
// and apply it to the current context
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_while_types = AlgebraChecker::getTruthsFromFormula(AlgebraChecker::negateFormula($while_clauses));
|
2017-02-21 22:52:27 +01:00
|
|
|
|
|
|
|
if ($negated_while_types) {
|
2017-02-22 06:42:50 +01:00
|
|
|
$changed_vars = [];
|
2017-02-21 22:52:27 +01:00
|
|
|
$vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_while_types,
|
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_vars,
|
|
|
|
$statements_checker->getFileChecker(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->cond),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($vars_in_scope_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope = $vars_in_scope_reconciled;
|
2017-02-21 23:19:15 +01:00
|
|
|
|
|
|
|
foreach ($changed_vars as $changed_var) {
|
2017-04-02 21:26:10 +02:00
|
|
|
$context->removeVarFromConflictingClauses($changed_var);
|
2017-02-21 23:19:15 +01:00
|
|
|
}
|
2017-02-21 22:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-02-21 23:41:54 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$while_context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
|
2017-02-27 05:09:18 +01:00
|
|
|
if ($context->collect_references) {
|
2017-02-01 05:24:33 +01:00
|
|
|
$context->referenced_vars = array_merge(
|
|
|
|
$context->referenced_vars,
|
|
|
|
$while_context->referenced_vars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|