2016-10-22 19:23:18 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
2016-12-04 20:14:00 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssignmentChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\InvalidIterator;
|
2017-05-22 17:59:58 +02:00
|
|
|
use Psalm\Issue\NullIterator;
|
|
|
|
use Psalm\Issue\PossiblyNullIterator;
|
2017-11-15 04:55:48 +01:00
|
|
|
use Psalm\Issue\RawObjectIteration;
|
2017-05-25 04:07:49 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2017-12-03 00:28:18 +01:00
|
|
|
use Psalm\Scope\LoopScope;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Type;
|
2017-12-04 14:50:59 +01:00
|
|
|
use Psalm\VarDocblockComment;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
class ForeachChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Stmt\Foreach_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @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;
|
|
|
|
|
2018-01-21 22:24:20 +01:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2018-01-21 22:24:20 +01:00
|
|
|
|
|
|
|
if ($project_checker->alter_code) {
|
|
|
|
$foreach_context->branch_point =
|
|
|
|
$foreach_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
|
|
|
|
}
|
|
|
|
|
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;
|
2018-01-28 23:28:34 +01:00
|
|
|
} elseif ($var_id && $foreach_context->hasVariable($var_id, $statements_checker)) {
|
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-05-22 17:59:58 +02:00
|
|
|
if ($iterator_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullIterator(
|
|
|
|
'Cannot iterate over null',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->expr)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($iterator_type->isNullable() && !$iterator_type->ignore_nullable_issues) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullIterator(
|
|
|
|
'Cannot iterate over nullable var ' . $iterator_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->expr)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($iterator_type->getTypes() as $iterator_type) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// if it's an empty array, we cannot iterate over it
|
2017-12-10 22:17:27 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TArray
|
|
|
|
&& $iterator_type->type_params[1]->isEmpty()
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:59:58 +02:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TNull) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($iterator_type instanceof Type\Atomic\TArray
|
|
|
|
|| $iterator_type instanceof Type\Atomic\ObjectLike
|
|
|
|
) {
|
|
|
|
if ($iterator_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$iterator_type = $iterator_type->getGenericArrayType();
|
|
|
|
}
|
|
|
|
|
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\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-08-08 00:38:38 +02:00
|
|
|
} elseif ($iterator_type instanceof Type\Atomic\TObject ||
|
2017-02-11 00:12:59 +01:00
|
|
|
$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(
|
2018-01-02 02:04:03 +01:00
|
|
|
$statements_checker,
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_type->value,
|
2017-01-15 01:06:58 +01:00
|
|
|
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' ||
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase->classImplements(
|
2017-02-11 00:12:59 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($codebase->classImplements(
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_type->value,
|
2017-01-15 01:06:58 +01:00
|
|
|
'Iterator'
|
2017-11-24 18:57:00 +01:00
|
|
|
) ||
|
2017-12-01 01:00:09 +01:00
|
|
|
(
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase->interfaceExists($iterator_type->value)
|
|
|
|
&& $codebase->interfaceExtends(
|
2017-11-24 18:57:00 +01:00
|
|
|
$iterator_type->value,
|
|
|
|
'Iterator'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
2017-02-11 00:12:59 +01:00
|
|
|
$iterator_method = $iterator_type->value . '::current';
|
2018-01-26 16:59:30 +01:00
|
|
|
$self_class = $iterator_type->value;
|
|
|
|
$iterator_class_type = MethodChecker::getMethodReturnType(
|
|
|
|
$project_checker,
|
|
|
|
$iterator_method,
|
|
|
|
$self_class
|
|
|
|
);
|
2017-01-15 01:06:58 +01:00
|
|
|
|
|
|
|
if ($iterator_class_type) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$value_type_part = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$iterator_class_type,
|
2018-01-26 16:59:30 +01:00
|
|
|
$self_class,
|
2018-01-26 19:51:00 +01:00
|
|
|
$self_class
|
2017-01-15 01:06:58 +01:00
|
|
|
);
|
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
|
|
|
}
|
2018-02-01 06:50:01 +01:00
|
|
|
} elseif ($codebase->classImplements(
|
2017-11-15 17:58:46 +01:00
|
|
|
$iterator_type->value,
|
|
|
|
'Traversable'
|
2017-11-24 18:57:00 +01:00
|
|
|
) ||
|
2017-12-01 01:00:09 +01:00
|
|
|
(
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase->interfaceExists($iterator_type->value)
|
|
|
|
&& $codebase->interfaceExtends(
|
2017-11-24 18:57:00 +01:00
|
|
|
$iterator_type->value,
|
|
|
|
'Traversable'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
2017-11-15 17:58:46 +01:00
|
|
|
// @todo try and get value type
|
2017-11-15 18:09:13 +01:00
|
|
|
} elseif (!in_array(
|
|
|
|
strtolower($iterator_type->value),
|
2017-11-24 18:57:00 +01:00
|
|
|
['iterator', 'iterable', 'traversable'],
|
|
|
|
true
|
2017-11-15 18:09:13 +01:00
|
|
|
)) {
|
2017-11-15 04:55:48 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new RawObjectIteration(
|
|
|
|
'Possibly undesired iteration over regular object ' . $iterator_type->value,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->expr)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
$location = new CodeLocation($statements_checker, $stmt->keyVar);
|
|
|
|
|
|
|
|
if ($context->collect_references && !isset($foreach_context->byref_constraints[$key_var_id])) {
|
|
|
|
$foreach_context->unreferenced_vars[$key_var_id] = $location;
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
if (!$statements_checker->hasVariable($key_var_id)) {
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$key_var_id,
|
2018-01-28 23:28:34 +01:00
|
|
|
$location,
|
2018-01-21 22:24:20 +01:00
|
|
|
$foreach_context->branch_point
|
2017-02-08 00:09:12 +01:00
|
|
|
);
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
|
|
if ($stmt->byRef) {
|
|
|
|
$statements_checker->registerVariableUse($location);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references
|
|
|
|
&& $stmt->byRef
|
|
|
|
&& $stmt->valueVar instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& is_string($stmt->valueVar->name)
|
|
|
|
) {
|
|
|
|
$foreach_context->byref_constraints['$' . $stmt->valueVar->name]
|
|
|
|
= new \Psalm\ReferenceConstraint($value_type);
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-12-04 14:50:59 +01:00
|
|
|
/** @var VarDocblockComment|null $var_comment */
|
|
|
|
$var_comment = null;
|
|
|
|
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
|
|
|
$var_comment = CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment_text,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$statements_checker->getSource()->getAliases()
|
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker, $stmt)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
|
|
|
if ($var_comment && $var_comment->var_id) {
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
2018-01-02 02:04:03 +01:00
|
|
|
$var_comment->type,
|
2018-01-26 19:51:00 +01:00
|
|
|
$context->self,
|
2017-10-07 16:22:52 +02:00
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$foreach_context->vars_in_scope[$var_comment->var_id] = $comment_type;
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
$loop_scope = new LoopScope($foreach_context, $context);
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
$protected_var_ids = $context->protected_var_ids;
|
|
|
|
if ($var_id) {
|
|
|
|
$protected_var_ids[$var_id] = true;
|
|
|
|
}
|
|
|
|
$loop_scope->protected_var_ids = $protected_var_ids;
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
LoopChecker::analyze($statements_checker, $stmt->stmts, [], [], $loop_scope);
|
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-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$foreach_context->referenced_var_ids,
|
|
|
|
$context->referenced_var_ids
|
|
|
|
);
|
2017-02-01 05:24:33 +01:00
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
|
|
|
$context->unreferenced_vars = $foreach_context->unreferenced_vars;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|