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
|
|
|
foreach ($stmt->init as $init) {
|
2017-04-11 23:43:46 +02:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $init, $context) === false) {
|
2016-10-22 23:35:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:43:46 +02:00
|
|
|
$for_context = clone $context;
|
|
|
|
$before_context = clone $context;
|
|
|
|
$for_context->inside_loop = true;
|
|
|
|
|
2016-10-22 23:35:59 +02:00
|
|
|
foreach ($stmt->cond as $condition) {
|
2017-02-13 00:47:02 +01:00
|
|
|
$for_context->inside_conditional = true;
|
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-02-13 00:47:02 +01:00
|
|
|
$for_context->inside_conditional = false;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:14:25 +01:00
|
|
|
$changed_vars = Context::getNewOrUpdatedVarIds($before_context, $for_context);
|
|
|
|
|
|
|
|
$statements_checker->analyzeLoop($stmt->stmts, $changed_vars, $for_context, $context);
|
2017-01-18 23:13:20 +01:00
|
|
|
|
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
|
|
|
|
2017-04-02 21:26:10 +02:00
|
|
|
$context->removeVarFromConflictingClauses($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
|
|
|
|
);
|
|
|
|
|
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(
|
|
|
|
$for_context->referenced_vars,
|
|
|
|
$context->referenced_vars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
}
|
|
|
|
}
|