2016-10-22 19:23:18 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-12-04 20:14:00 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssignmentChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\InvalidIterator;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\NullReference;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class ForeachChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Stmt\Foreach_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +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\Foreach_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foreach_context = clone $context;
|
2017-03-13 16:23:26 +01:00
|
|
|
$foreach_context->inside_loop = true;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
$key_type = null;
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->expr,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-10-22 19:23:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$iterator_type = $stmt->expr->inferredType;
|
2017-02-01 05:24:33 +01:00
|
|
|
} elseif ($foreach_context->hasVariable($var_id)) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$iterator_type = $foreach_context->vars_in_scope[$var_id];
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$iterator_type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iterator_type) {
|
2017-02-11 00:12:59 +01:00
|
|
|
foreach ($iterator_type->types as $iterator_type) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// if it's an empty array, we cannot iterate over it
|
2017-02-11 00:12:59 +01:00
|
|
|
if ((string) $iterator_type === 'array<empty, empty>') {
|
2016-10-22 19:23:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TArray) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if (!$value_type) {
|
2017-02-11 00:12:59 +01:00
|
|
|
$value_type = $iterator_type->type_params[1];
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-02-11 00:12:59 +01:00
|
|
|
$value_type = Type::combineUnionTypes($value_type, $iterator_type->type_params[1]);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
$key_type_part = $iterator_type->type_params[0];
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
} else {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\Scalar ||
|
|
|
|
$iterator_type instanceof Type\Atomic\TNull ||
|
|
|
|
$iterator_type instanceof Type\Atomic\TVoid
|
2017-01-15 01:06:58 +01:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidIterator(
|
2017-02-11 00:12:59 +01:00
|
|
|
'Cannot iterate over ' . $iterator_type->getKey(),
|
2017-01-15 01:06:58 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->expr)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$value_type = Type::getMixed();
|
2017-02-11 00:12:59 +01:00
|
|
|
} elseif ($iterator_type instanceof Type\Atomic\TArray ||
|
|
|
|
$iterator_type instanceof Type\Atomic\TObject ||
|
|
|
|
$iterator_type instanceof Type\Atomic\TMixed ||
|
|
|
|
$iterator_type instanceof Type\Atomic\TEmpty
|
2017-01-15 01:06:58 +01:00
|
|
|
) {
|
|
|
|
$value_type = Type::getMixed();
|
2017-02-11 00:12:59 +01:00
|
|
|
} elseif ($iterator_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
if ($iterator_type->value !== 'Traversable' &&
|
|
|
|
$iterator_type->value !== $statements_checker->getClassName()
|
2017-01-15 01:06:58 +01:00
|
|
|
) {
|
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_type->value,
|
2017-01-15 01:06:58 +01:00
|
|
|
$statements_checker->getFileChecker(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->expr),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
2017-01-15 01:06:58 +01:00
|
|
|
) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-02-11 00:12:59 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TGenericObject &&
|
|
|
|
(strtolower($iterator_type->value) === 'iterable' ||
|
|
|
|
strtolower($iterator_type->value) === 'traversable' ||
|
|
|
|
ClassChecker::classImplements(
|
|
|
|
$iterator_type->value,
|
|
|
|
'Traversable'
|
|
|
|
))
|
|
|
|
) {
|
|
|
|
$value_index = count($iterator_type->type_params) - 1;
|
|
|
|
$value_type_part = $iterator_type->type_params[$value_index];
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
|
|
|
} else {
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value_index) {
|
|
|
|
$key_type_part = $iterator_type->type_params[0];
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
} else {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (ClassChecker::classImplements(
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_type->value,
|
2017-01-15 01:06:58 +01:00
|
|
|
'Iterator'
|
|
|
|
)) {
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_method = $iterator_type->value . '::current';
|
2017-01-15 01:06:58 +01:00
|
|
|
$iterator_class_type = MethodChecker::getMethodReturnType($iterator_method);
|
|
|
|
|
|
|
|
if ($iterator_class_type) {
|
|
|
|
$value_type_part = ExpressionChecker::fleshOutTypes(
|
2017-03-01 17:56:36 +01:00
|
|
|
clone $iterator_class_type,
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_type->value,
|
2017-01-15 01:06:58 +01:00
|
|
|
$iterator_method
|
|
|
|
);
|
2017-01-09 06:56:51 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-15 01:06:58 +01:00
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
} else {
|
|
|
|
$value_type = Type::getMixed();
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 01:14:25 +01:00
|
|
|
$before_context = clone $foreach_context;
|
|
|
|
|
2016-12-29 06:32:12 +01:00
|
|
|
if ($stmt->keyVar && $stmt->keyVar instanceof PhpParser\Node\Expr\Variable && is_string($stmt->keyVar->name)) {
|
2017-02-08 00:09:12 +01:00
|
|
|
$key_var_id = '$' . $stmt->keyVar->name;
|
|
|
|
$foreach_context->vars_in_scope[$key_var_id] = $key_type ?: Type::getMixed();
|
|
|
|
$foreach_context->vars_possibly_in_scope[$key_var_id] = true;
|
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($key_var_id)) {
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$key_var_id,
|
2017-02-08 00:18:33 +01:00
|
|
|
new CodeLocation($statements_checker, $stmt->keyVar)
|
2017-02-08 00:09:12 +01:00
|
|
|
);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
AssignmentChecker::analyze(
|
2016-12-04 20:14:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->valueVar,
|
|
|
|
null,
|
|
|
|
$value_type ?: Type::getMixed(),
|
|
|
|
$foreach_context,
|
|
|
|
(string)$stmt->getDocComment()
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
$doc_comment_text = (string)$stmt->getDocComment();
|
|
|
|
|
|
|
|
if ($doc_comment_text) {
|
|
|
|
CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment_text,
|
|
|
|
$foreach_context,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-03-15 01:14:25 +01:00
|
|
|
$changed_vars = Context::getNewOrUpdatedVarIds($before_context, $foreach_context);
|
|
|
|
|
|
|
|
$statements_checker->analyzeLoop($stmt->stmts, $changed_vars, $foreach_context, $context);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-11 01:08:55 +01:00
|
|
|
if (!isset($foreach_context->vars_in_scope[$var])) {
|
2016-10-22 19:23:18 +02:00
|
|
|
unset($context->vars_in_scope[$var]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($foreach_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $foreach_context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) $foreach_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],
|
|
|
|
$foreach_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 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$foreach_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(
|
|
|
|
$foreach_context->referenced_vars,
|
|
|
|
$context->referenced_vars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|