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
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function check(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Stmt\Foreach_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if (ExpressionChecker::check($statements_checker, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foreach_context = clone $context;
|
|
|
|
$foreach_context->in_loop = true;
|
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
$key_type = null;
|
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
|
|
|
$value_type = null;
|
|
|
|
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->expr,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getNamespace(),
|
|
|
|
$statements_checker->getAliasedClasses()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$iterator_type = $stmt->expr->inferredType;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif (isset($foreach_context->vars_in_scope[$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) {
|
|
|
|
foreach ($iterator_type->types as $return_type) {
|
|
|
|
// if it's an empty array, we cannot iterate over it
|
2016-11-13 07:43:51 +01:00
|
|
|
if ((string) $return_type === 'array<empty, empty>') {
|
2016-10-22 19:23:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return_type instanceof Type\Generic) {
|
|
|
|
$value_index = count($return_type->type_params) - 1;
|
|
|
|
$value_type_part = $return_type->type_params[$value_index];
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value_index) {
|
|
|
|
$key_type_part = $return_type->type_params[0];
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = $key_type_part;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($return_type->value) {
|
|
|
|
case 'mixed':
|
|
|
|
case 'empty':
|
|
|
|
case 'Generator':
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'null':
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new NullReference(
|
|
|
|
'Cannot iterate over ' . $return_type->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
case 'void':
|
|
|
|
case 'int':
|
|
|
|
case 'bool':
|
|
|
|
case 'false':
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new InvalidIterator(
|
|
|
|
'Cannot iterate over ' . $return_type->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (ClassChecker::classImplements($return_type->value, 'Iterator')) {
|
|
|
|
$iterator_method = $return_type->value . '::current';
|
|
|
|
$iterator_class_type = MethodChecker::getMethodReturnTypes($iterator_method);
|
|
|
|
|
|
|
|
if ($iterator_class_type) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$value_type_part = ExpressionChecker::fleshOutTypes(
|
|
|
|
$iterator_class_type,
|
|
|
|
[],
|
|
|
|
$return_type->value,
|
|
|
|
$iterator_method
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$value_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($return_type->value !== 'Traversable' &&
|
|
|
|
$return_type->value !== $statements_checker->getClassName()
|
|
|
|
) {
|
2016-11-08 01:16:51 +01:00
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2016-11-02 07:29:00 +01:00
|
|
|
$return_type->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 07:57:11 +02:00
|
|
|
if ($stmt->keyVar && $stmt->keyVar instanceof PhpParser\Node\Expr\Variable) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$foreach_context->vars_in_scope['$' . $stmt->keyVar->name] = $key_type ?: Type::getMixed();
|
|
|
|
$foreach_context->vars_possibly_in_scope['$' . $stmt->keyVar->name] = true;
|
|
|
|
$statements_checker->registerVariable('$' . $stmt->keyVar->name, $stmt->getLine());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value_type && $value_type instanceof Type\Atomic) {
|
|
|
|
$value_type = new Type\Union([$value_type]);
|
|
|
|
}
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
AssignmentChecker::check(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->valueVar,
|
|
|
|
null,
|
|
|
|
$value_type ?: Type::getMixed(),
|
|
|
|
$foreach_context,
|
|
|
|
(string)$stmt->getDocComment()
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
CommentChecker::getTypeFromComment(
|
|
|
|
(string) $stmt->getDocComment(),
|
|
|
|
$foreach_context,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
null
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
$statements_checker->check($stmt->stmts, $foreach_context, $context);
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($foreach_context->vars_in_scope[$var])) {
|
|
|
|
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]
|
|
|
|
);
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|