2016-10-22 19:23:18 +02:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Block;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-21 23:56:04 +01:00
|
|
|
|
use Psalm\Codebase;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\CommentAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\ScopeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssignmentAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
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;
|
2018-03-21 03:59:22 +01:00
|
|
|
|
use Psalm\Issue\PossiblyFalseIterator;
|
|
|
|
|
use Psalm\Issue\PossiblyInvalidIterator;
|
2017-05-22 17:59:58 +02:00
|
|
|
|
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;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Scope\LoopScope;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
|
class ForeachAnalyzer
|
2016-10-22 19:23:18 +02:00
|
|
|
|
{
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @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(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
PhpParser\Node\Stmt\Foreach_ $stmt,
|
|
|
|
|
Context $context
|
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-21 22:24:20 +01:00
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
|
$key_type = null;
|
|
|
|
|
$value_type = null;
|
2018-11-10 22:10:59 +01:00
|
|
|
|
$always_non_empty_array = true;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2016-10-22 19:23:18 +02:00
|
|
|
|
$stmt->expr,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
|
$statements_analyzer
|
2016-10-22 19:23:18 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
|
|
|
|
$iterator_type = $stmt->expr->inferredType;
|
2018-11-11 18:01:14 +01:00
|
|
|
|
} elseif ($var_id && $context->hasVariable($var_id, $statements_analyzer)) {
|
2018-11-10 22:10:59 +01:00
|
|
|
|
$iterator_type = $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) {
|
2018-11-21 23:56:04 +01:00
|
|
|
|
if (self::checkIteratorType(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$stmt,
|
|
|
|
|
$iterator_type,
|
|
|
|
|
$codebase,
|
|
|
|
|
$context,
|
|
|
|
|
$key_type,
|
|
|
|
|
$value_type,
|
|
|
|
|
$always_non_empty_array
|
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
2018-03-21 03:59:22 +01:00
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
|
$foreach_context = clone $context;
|
|
|
|
|
|
|
|
|
|
$foreach_context->inside_loop = true;
|
2018-11-10 23:29:59 +01:00
|
|
|
|
$foreach_context->inside_case = false;
|
2018-11-10 22:10:59 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase->alter_code) {
|
2018-11-10 22:10:59 +01:00
|
|
|
|
$foreach_context->branch_point =
|
|
|
|
|
$foreach_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
|
|
|
|
|
}
|
|
|
|
|
|
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-11-11 18:01:14 +01:00
|
|
|
|
$location = new CodeLocation($statements_analyzer, $stmt->keyVar);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
|
|
|
|
if ($context->collect_references && !isset($foreach_context->byref_constraints[$key_var_id])) {
|
2018-06-17 02:01:33 +02:00
|
|
|
|
$foreach_context->unreferenced_vars[$key_var_id] = [$location->getHash() => $location];
|
2018-01-28 23:28:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (!$statements_analyzer->hasVariable($key_var_id)) {
|
|
|
|
|
$statements_analyzer->registerVariable(
|
2017-02-08 00:09:12 +01:00
|
|
|
|
$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-06-17 06:52:32 +02:00
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariableAssignment(
|
2018-06-17 06:52:32 +02:00
|
|
|
|
$key_var_id,
|
|
|
|
|
$location
|
|
|
|
|
);
|
2017-02-08 00:09:12 +01:00
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
|
if ($stmt->byRef && $context->collect_references) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
}
|
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]
|
2018-11-06 03:57:36 +01:00
|
|
|
|
= new \Psalm\Internal\ReferenceConstraint($value_type);
|
2018-01-25 07:04:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
AssignmentAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2016-12-04 20:14:00 +01:00
|
|
|
|
$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) {
|
2018-02-08 05:33:31 +01:00
|
|
|
|
$var_comments = [];
|
2017-12-04 14:50:59 +01:00
|
|
|
|
|
2017-11-15 03:43:31 +01:00
|
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_comments = CommentAnalyzer::getTypeFromComment(
|
2017-11-15 03:43:31 +01:00
|
|
|
|
$doc_comment_text,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
|
$statements_analyzer->getSource()->getAliases()
|
2017-11-15 03:43:31 +01:00
|
|
|
|
);
|
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidDocblock(
|
|
|
|
|
(string)$e->getMessage(),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
2017-11-15 03:43:31 +01:00
|
|
|
|
)
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
|
foreach ($var_comments as $var_comment) {
|
|
|
|
|
if (!$var_comment->var_id) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
|
$codebase,
|
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;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
LoopAnalyzer::analyze($statements_analyzer, $stmt->stmts, [], [], $loop_scope, $inner_loop_context);
|
2018-11-10 22:10:59 +01:00
|
|
|
|
|
|
|
|
|
if (!$inner_loop_context) {
|
|
|
|
|
throw new \UnexpectedValueException('There should be an inner loop context');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($always_non_empty_array) {
|
|
|
|
|
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) {
|
|
|
|
|
// if there are break statements in the loop it's not certain
|
|
|
|
|
// that the loop has finished executing, so the assertions at the end
|
|
|
|
|
// the loop in the while conditional may not hold
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (in_array(ScopeAnalyzer::ACTION_BREAK, $loop_scope->final_actions, true)
|
|
|
|
|
|| in_array(ScopeAnalyzer::ACTION_CONTINUE, $loop_scope->final_actions, true)
|
2018-11-10 22:10:59 +01:00
|
|
|
|
) {
|
|
|
|
|
if (isset($loop_scope->possibly_defined_loop_parent_vars[$var_id])) {
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
|
$type,
|
|
|
|
|
$loop_scope->possibly_defined_loop_parent_vars[$var_id]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
|
$foreach_context->loop_scope = null;
|
|
|
|
|
|
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-06-22 07:13:49 +02:00
|
|
|
|
if ($context->collect_exceptions) {
|
|
|
|
|
$context->possibly_thrown_exceptions += $foreach_context->possibly_thrown_exceptions;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
|
if ($context->collect_references) {
|
2018-06-17 03:14:19 +02:00
|
|
|
|
foreach ($foreach_context->unreferenced_vars as $var_id => $locations) {
|
|
|
|
|
if (isset($context->unreferenced_vars[$var_id])) {
|
|
|
|
|
$context->unreferenced_vars[$var_id] += $locations;
|
|
|
|
|
} else {
|
|
|
|
|
$context->unreferenced_vars[$var_id] = $locations;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
}
|
2018-11-21 23:56:04 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ?Type\Union $key_type
|
|
|
|
|
* @param ?Type\Union $value_type
|
|
|
|
|
* @return false|null
|
|
|
|
|
*/
|
|
|
|
|
public static function checkIteratorType(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
PhpParser\Node\Stmt\Foreach_ $stmt,
|
|
|
|
|
Type\Union $iterator_type,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
Context $context,
|
|
|
|
|
&$key_type,
|
|
|
|
|
&$value_type,
|
|
|
|
|
bool &$always_non_empty_array
|
|
|
|
|
) {
|
|
|
|
|
if ($iterator_type->isNull()) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new NullIterator(
|
|
|
|
|
'Cannot iterate over null',
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->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_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($iterator_type->isFalsable() && !$iterator_type->ignore_falsable_issues) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyFalseIterator(
|
|
|
|
|
'Cannot iterate over falsable var ' . $iterator_type,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$has_valid_iterator = false;
|
|
|
|
|
$invalid_iterator_types = [];
|
|
|
|
|
|
|
|
|
|
foreach ($iterator_type->getTypes() as $iterator_atomic_type) {
|
|
|
|
|
// if it's an empty array, we cannot iterate over it
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
&& $iterator_atomic_type->type_params[1]->isEmpty()
|
|
|
|
|
) {
|
|
|
|
|
$always_non_empty_array = false;
|
|
|
|
|
$has_valid_iterator = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\TNull
|
|
|
|
|
|| $iterator_atomic_type instanceof Type\Atomic\TFalse
|
|
|
|
|
) {
|
|
|
|
|
$always_non_empty_array = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
|| $iterator_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
|
) {
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
if (!$iterator_atomic_type->sealed) {
|
|
|
|
|
$always_non_empty_array = false;
|
|
|
|
|
}
|
|
|
|
|
$iterator_atomic_type = $iterator_atomic_type->getGenericArrayType();
|
|
|
|
|
} elseif (!$iterator_atomic_type instanceof Type\Atomic\TNonEmptyArray) {
|
|
|
|
|
$always_non_empty_array = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
|
$value_type = $iterator_atomic_type->type_params[1];
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $iterator_atomic_type->type_params[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$key_type_part = $iterator_atomic_type->type_params[0];
|
|
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$has_valid_iterator = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$always_non_empty_array = false;
|
|
|
|
|
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\Scalar ||
|
|
|
|
|
$iterator_atomic_type instanceof Type\Atomic\TVoid
|
|
|
|
|
) {
|
|
|
|
|
$invalid_iterator_types[] = $iterator_atomic_type->getKey();
|
|
|
|
|
|
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
|
} elseif ($iterator_atomic_type instanceof Type\Atomic\TObject ||
|
|
|
|
|
$iterator_atomic_type instanceof Type\Atomic\TMixed ||
|
|
|
|
|
$iterator_atomic_type instanceof Type\Atomic\TEmpty
|
|
|
|
|
) {
|
|
|
|
|
$has_valid_iterator = true;
|
|
|
|
|
$value_type = Type::getMixed();
|
2019-01-04 20:54:40 +01:00
|
|
|
|
} elseif ($iterator_atomic_type instanceof Type\Atomic\TIterable) {
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\TGenericIterable) {
|
|
|
|
|
$value_type_part = $iterator_atomic_type->type_params[1];
|
|
|
|
|
$key_type_part = $iterator_atomic_type->type_params[0];
|
|
|
|
|
} else {
|
|
|
|
|
$value_type_part = Type::getMixed();
|
|
|
|
|
$key_type_part = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
|
$value_type = $value_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$has_valid_iterator = true;
|
2018-11-21 23:56:04 +01:00
|
|
|
|
} elseif ($iterator_atomic_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
|
if ($iterator_atomic_type->value !== 'Traversable' &&
|
|
|
|
|
$iterator_atomic_type->value !== $statements_analyzer->getClassName()
|
|
|
|
|
) {
|
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TypeAnalyzer::isAtomicContainedBy(
|
|
|
|
|
$codebase,
|
|
|
|
|
$iterator_atomic_type,
|
2019-01-04 20:54:40 +01:00
|
|
|
|
new Type\Atomic\TIterable
|
2018-11-21 23:56:04 +01:00
|
|
|
|
)) {
|
|
|
|
|
self::handleIterable(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$iterator_atomic_type,
|
|
|
|
|
$stmt->expr,
|
|
|
|
|
$codebase,
|
|
|
|
|
$context,
|
|
|
|
|
$key_type,
|
|
|
|
|
$value_type,
|
|
|
|
|
$has_valid_iterator
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RawObjectIteration(
|
|
|
|
|
'Possibly undesired iteration over regular object ' . $iterator_atomic_type->value,
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
2018-11-27 15:34:54 +01:00
|
|
|
|
// fall through
|
2018-11-21 23:56:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($invalid_iterator_types) {
|
|
|
|
|
if ($has_valid_iterator) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new PossiblyInvalidIterator(
|
|
|
|
|
'Cannot iterate over ' . $invalid_iterator_types[0],
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidIterator(
|
|
|
|
|
'Cannot iterate over ' . $invalid_iterator_types[0],
|
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr)
|
|
|
|
|
),
|
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ?Type\Union $key_type
|
|
|
|
|
* @param ?Type\Union $value_type
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function handleIterable(
|
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
|
Type\Atomic\TNamedObject $iterator_atomic_type,
|
|
|
|
|
PhpParser\Node\Expr $foreach_expr,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
Context $context,
|
|
|
|
|
&$key_type,
|
|
|
|
|
&$value_type,
|
|
|
|
|
bool &$has_valid_iterator
|
|
|
|
|
) {
|
|
|
|
|
if ($iterator_atomic_type->extra_types) {
|
|
|
|
|
$iterator_atomic_type_copy = clone $iterator_atomic_type;
|
|
|
|
|
$iterator_atomic_type_copy->extra_types = [];
|
|
|
|
|
$iterator_atomic_types = [$iterator_atomic_type_copy];
|
|
|
|
|
$iterator_atomic_types = array_merge($iterator_atomic_types, $iterator_atomic_type->extra_types);
|
|
|
|
|
} else {
|
|
|
|
|
$iterator_atomic_types = [$iterator_atomic_type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($iterator_atomic_types as $iterator_atomic_type) {
|
|
|
|
|
if ($iterator_atomic_type instanceof Type\Atomic\TGenericParam) {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get a generic param here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$has_valid_iterator = true;
|
|
|
|
|
|
2019-01-04 20:54:40 +01:00
|
|
|
|
if (($iterator_atomic_type instanceof Type\Atomic\TGenericIterable)
|
|
|
|
|
|| ($iterator_atomic_type instanceof Type\Atomic\TGenericObject
|
|
|
|
|
&& strtolower($iterator_atomic_type->value) === 'traversable')
|
|
|
|
|
|| ($iterator_atomic_type instanceof Type\Atomic\TGenericObject
|
|
|
|
|
&& $codebase->classImplements(
|
2018-11-21 23:56:04 +01:00
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
'Traversable'
|
|
|
|
|
))
|
|
|
|
|
) {
|
|
|
|
|
$value_index = count($iterator_atomic_type->type_params) - 1;
|
|
|
|
|
$value_type_part = $iterator_atomic_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_atomic_type->type_params[0];
|
|
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$codebase->classlikes->classOrInterfaceExists($iterator_atomic_type->value)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 00:35:41 +01:00
|
|
|
|
if (strtolower($iterator_atomic_type->value) === 'iteratoraggregate'
|
|
|
|
|
|| $codebase->classImplements(
|
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
'IteratorAggregate'
|
|
|
|
|
)
|
|
|
|
|
|| ($codebase->interfaceExists($iterator_atomic_type->value)
|
2018-11-21 23:56:04 +01:00
|
|
|
|
&& $codebase->interfaceExtends(
|
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
'IteratorAggregate'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
|
|
|
|
|
$foreach_expr,
|
|
|
|
|
new PhpParser\Node\Identifier('getIterator', $foreach_expr->getAttributes())
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-26 22:50:47 +01:00
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:56:04 +01:00
|
|
|
|
\Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$fake_method_call,
|
|
|
|
|
$context
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-26 22:50:47 +01:00
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:56:04 +01:00
|
|
|
|
$iterator_class_type = $fake_method_call->inferredType ?? null;
|
|
|
|
|
|
|
|
|
|
if ($iterator_class_type) {
|
|
|
|
|
foreach ($iterator_class_type->getTypes() as $array_atomic_type) {
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
|
|| $array_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
|
) {
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
|
$array_atomic_type = $array_atomic_type->getGenericArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$key_type_part = $array_atomic_type->type_params[0];
|
|
|
|
|
$value_type_part = $array_atomic_type->type_params[1];
|
|
|
|
|
} elseif ($array_atomic_type instanceof Type\Atomic\TGenericObject) {
|
|
|
|
|
$type_param_count = count($array_atomic_type->type_params);
|
|
|
|
|
|
|
|
|
|
$value_type_part = $array_atomic_type->type_params[$type_param_count - 1];
|
|
|
|
|
$key_type_part = $type_param_count > 1
|
|
|
|
|
? $array_atomic_type->type_params[0]
|
|
|
|
|
: Type::getMixed();
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = Type::getMixed();
|
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
|
$key_type = $key_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
|
$value_type = $value_type_part;
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
} elseif ($codebase->classImplements(
|
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
'Iterator'
|
|
|
|
|
) ||
|
|
|
|
|
(
|
|
|
|
|
$codebase->interfaceExists($iterator_atomic_type->value)
|
|
|
|
|
&& $codebase->interfaceExtends(
|
|
|
|
|
$iterator_atomic_type->value,
|
|
|
|
|
'Iterator'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
|
|
|
|
|
$foreach_expr,
|
|
|
|
|
new PhpParser\Node\Identifier('current', $foreach_expr->getAttributes())
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-26 22:50:47 +01:00
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:56:04 +01:00
|
|
|
|
\Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer::analyze(
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$fake_method_call,
|
|
|
|
|
$context
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-26 22:50:47 +01:00
|
|
|
|
if (!in_array('PossiblyInvalidMethodCall', $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyInvalidMethodCall']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:56:04 +01:00
|
|
|
|
$iterator_class_type = $fake_method_call->inferredType ?? null;
|
|
|
|
|
|
|
|
|
|
if ($iterator_class_type) {
|
|
|
|
|
if (!$value_type) {
|
|
|
|
|
$value_type = $iterator_class_type;
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $iterator_class_type);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
}
|