2016-10-22 23:35:59 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
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\Type;
|
|
|
|
|
|
|
|
class ForChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Stmt\For_ $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\For_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-10-22 23:35:59 +02:00
|
|
|
$for_context = clone $context;
|
|
|
|
$for_context->in_loop = true;
|
|
|
|
|
|
|
|
foreach ($stmt->init as $init) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $init, $for_context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->cond as $condition) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $condition, $for_context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 23:13:20 +01:00
|
|
|
$statements_checker->analyze($stmt->stmts, $for_context, $context);
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
foreach ($stmt->loop as $expr) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $expr, $for_context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($for_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $for_context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) $for_context->vars_in_scope[$var] !== (string) $type) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var],
|
|
|
|
$for_context->vars_in_scope[$var]
|
|
|
|
);
|
2017-01-31 04:25:51 +01:00
|
|
|
|
|
|
|
$context->removeVarFromClauses($var);
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$for_context->vars_possibly_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|