1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 19:36:59 +01:00
psalm/src/Psalm/Checker/Statements/Block/ForChecker.php

82 lines
2.4 KiB
PHP
Raw Normal View History

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
*/
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;
2017-03-13 16:23:26 +01:00
$for_context->inside_loop = true;
2016-10-22 23:35:59 +02:00
foreach ($stmt->init as $init) {
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-02-13 00:47:02 +01:00
$for_context->inside_conditional = true;
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-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) {
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]
);
$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
);
if ($context->collect_references) {
$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
}
}