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;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
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
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function check(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Stmt\While_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_context = clone $context;
|
|
|
|
|
|
|
|
if (ExpressionChecker::check($statements_checker, $stmt->cond, $while_context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$while_types = TypeChecker::getTypeAssertions(
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->cond,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getNamespace(),
|
|
|
|
$statements_checker->getAliasedClasses()
|
|
|
|
);
|
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 &&
|
|
|
|
$stmt->cond instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|
|
|
|
) {
|
2016-10-22 23:35:59 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 23:35:59 +02:00
|
|
|
$while_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$while_types,
|
|
|
|
$while_context->vars_in_scope,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statements_checker->check($stmt->stmts, $while_context, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($while_context->vars_in_scope[$var])) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$while_context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|