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

85 lines
2.6 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;
$before_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
}
$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) {
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-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
);
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
}
}