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

80 lines
2.3 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;
$for_context->in_loop = true;
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) {
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) {
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->count_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
}
}