2016-11-01 16:37:58 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\Assignment\ArrayAssignmentChecker;
|
|
|
|
use Psalm\Checker\Statements\Expression\Assignment\PropertyAssignmentChecker;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Checker\StatementsChecker;
|
2017-01-02 06:08:35 +01:00
|
|
|
use Psalm\Checker\TypeChecker;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Context;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2017-11-28 06:46:41 +01:00
|
|
|
use Psalm\Issue\AssignmentToVoid;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\InvalidScope;
|
2017-12-17 16:58:03 +01:00
|
|
|
use Psalm\Issue\LoopInvalidation;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\MissingDocblockType;
|
2016-12-17 00:56:23 +01:00
|
|
|
use Psalm\Issue\MixedAssignment;
|
2017-02-23 06:25:28 +01:00
|
|
|
use Psalm\Issue\ReferenceConstraintViolation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class AssignmentChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-12-04 20:14:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr $assign_var
|
2016-12-07 00:27:22 +01:00
|
|
|
* @param PhpParser\Node\Expr|null $assign_value This has to be null to support list destructuring
|
2016-12-04 20:14:00 +01:00
|
|
|
* @param Type\Union|null $assign_value_type
|
|
|
|
* @param Context $context
|
|
|
|
* @param string $doc_comment
|
2017-04-11 21:34:05 +02:00
|
|
|
* @param int|null $came_from_line_number
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 16:37:58 +01:00
|
|
|
* @return false|Type\Union
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyze(
|
2016-11-01 16:37:58 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr $assign_var,
|
2017-05-05 03:57:26 +02:00
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context,
|
2017-02-23 06:25:28 +01:00
|
|
|
$doc_comment,
|
2017-04-11 21:34:05 +02:00
|
|
|
$came_from_line_number = null
|
2016-11-01 16:37:58 +01:00
|
|
|
) {
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$assign_var,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
2017-08-19 05:14:38 +02:00
|
|
|
// gets a variable id that *may* contain array keys
|
2016-11-01 16:37:58 +01:00
|
|
|
$array_var_id = ExpressionChecker::getArrayVarId(
|
|
|
|
$assign_var,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = null;
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($doc_comment) {
|
2018-09-24 19:08:23 +02:00
|
|
|
$file_path = $statements_checker->getRootFilePath();
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
$file_storage_provider = $project_checker->file_storage_provider;
|
|
|
|
|
|
|
|
$file_storage = $file_storage_provider->get($file_path);
|
|
|
|
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = CommentChecker::getTypeFromComment(
|
2017-11-15 03:43:31 +01:00
|
|
|
$doc_comment,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$statements_checker->getAliases(),
|
|
|
|
null,
|
2018-09-24 19:08:23 +02:00
|
|
|
$came_from_line_number,
|
|
|
|
null,
|
|
|
|
$file_storage->type_aliases
|
2017-11-15 03:43:31 +01:00
|
|
|
);
|
|
|
|
} catch (IncorrectDocblockException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingDocblockType(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
foreach ($var_comments as $var_comment) {
|
2018-03-27 17:34:48 +02:00
|
|
|
try {
|
|
|
|
$var_comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
|
|
|
$context->self
|
|
|
|
);
|
2017-10-07 16:22:52 +02:00
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
$var_comment_type->setFromDocblock();
|
2017-10-07 16:22:52 +02:00
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
|
|
|
|
$comment_type = $var_comment_type;
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-08 05:33:31 +01:00
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2018-10-04 22:42:40 +02:00
|
|
|
if ($assign_value) {
|
|
|
|
if ($var_id && $assign_value instanceof PhpParser\Node\Expr\Closure) {
|
|
|
|
foreach ($assign_value->uses as $closure_use) {
|
|
|
|
if ($closure_use->byRef
|
|
|
|
&& is_string($closure_use->var->name)
|
|
|
|
&& $var_id === '$' . $closure_use->var->name
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getClosure();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2017-04-02 01:04:49 +02:00
|
|
|
}
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2018-10-04 22:42:40 +02:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $assign_value, $context) === false) {
|
|
|
|
if ($var_id) {
|
|
|
|
if ($array_var_id) {
|
|
|
|
$context->removeDescendents($array_var_id, null, $assign_value_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're not exiting immediately, make everything mixed
|
|
|
|
$context->vars_in_scope[$var_id] = $comment_type ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
if ($comment_type) {
|
2017-10-07 16:22:52 +02:00
|
|
|
$assign_value_type = $comment_type;
|
2016-12-17 01:22:30 +01:00
|
|
|
} elseif (!$assign_value_type) {
|
|
|
|
if (isset($assign_value->inferredType)) {
|
2016-12-04 20:14:00 +01:00
|
|
|
$assign_value_type = $assign_value->inferredType;
|
|
|
|
} else {
|
|
|
|
$assign_value_type = Type::getMixed();
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-02 01:04:49 +02:00
|
|
|
if ($array_var_id && isset($context->vars_in_scope[$array_var_id])) {
|
2017-04-02 21:26:10 +02:00
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$array_var_id,
|
|
|
|
$context->vars_in_scope[$array_var_id],
|
|
|
|
$assign_value_type,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker
|
2017-04-02 21:26:10 +02:00
|
|
|
);
|
2017-10-11 05:01:52 +02:00
|
|
|
} else {
|
|
|
|
$root_var_id = ExpressionChecker::getRootVarId(
|
|
|
|
$assign_var,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($root_var_id && isset($context->vars_in_scope[$root_var_id])) {
|
|
|
|
$context->removeVarFromConflictingClauses(
|
|
|
|
$root_var_id,
|
|
|
|
$context->vars_in_scope[$root_var_id],
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
2017-04-02 01:04:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:08:52 +01:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
if ($assign_value_type->isMixed()) {
|
2018-05-30 22:19:18 +02:00
|
|
|
$codebase->analyzer->incrementMixedCount($statements_checker->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-04-07 18:13:30 +02:00
|
|
|
if (!$assign_var instanceof PhpParser\Node\Expr\PropertyFetch) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedAssignment(
|
|
|
|
'Cannot assign ' . $var_id . ' to a mixed type',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
} else {
|
2018-05-30 22:19:18 +02:00
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_checker->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
|
|
if ($var_id
|
|
|
|
&& isset($context->byref_constraints[$var_id])
|
|
|
|
&& ($outer_constraint_type = $context->byref_constraints[$var_id]->type)
|
2017-02-23 06:25:28 +01:00
|
|
|
) {
|
2018-01-31 22:08:52 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase,
|
2018-01-31 22:08:52 +01:00
|
|
|
$assign_value_type,
|
|
|
|
$outer_constraint_type,
|
|
|
|
$assign_value_type->ignore_nullable_issues,
|
|
|
|
$assign_value_type->ignore_falsable_issues
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ReferenceConstraintViolation(
|
|
|
|
'Variable ' . $var_id . ' is limited to values of type '
|
|
|
|
. $context->byref_constraints[$var_id]->type
|
|
|
|
. ' because it is passed by reference',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 05:40:46 +01:00
|
|
|
if ($var_id === '$this' && IssueBuffer::accepts(
|
|
|
|
new InvalidScope(
|
|
|
|
'Cannot re-assign ' . $var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
if (isset($context->protected_var_ids[$var_id])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new LoopInvalidation(
|
|
|
|
'Variable ' . $var_id . ' has already been assigned in a for/foreach loop',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
if ($assign_var instanceof PhpParser\Node\Expr\Variable && is_string($assign_var->name) && $var_id) {
|
2016-12-04 20:14:00 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $assign_value_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2017-11-25 17:21:45 +01:00
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
2018-05-18 17:02:50 +02:00
|
|
|
$context->possibly_assigned_var_ids[$var_id] = true;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
$location = new CodeLocation($statements_checker, $assign_var);
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
if (!$statements_checker->hasVariable($var_id)) {
|
2018-01-21 22:24:20 +01:00
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$var_id,
|
2018-01-28 23:28:34 +01:00
|
|
|
$location,
|
2018-01-21 22:24:20 +01:00
|
|
|
$context->branch_point
|
|
|
|
);
|
2018-01-28 23:28:34 +01:00
|
|
|
} else {
|
|
|
|
$statements_checker->registerVariableAssignment(
|
|
|
|
$var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($context->byref_constraints[$var_id])) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$statements_checker->registerVariableUses([$location->getHash() => $location]);
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2016-12-04 19:35:38 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\List_
|
2017-12-19 05:13:18 +01:00
|
|
|
|| $assign_var instanceof PhpParser\Node\Expr\Array_
|
2016-12-04 19:35:38 +01:00
|
|
|
) {
|
2017-01-25 08:11:24 +01:00
|
|
|
/** @var int $offset */
|
2016-12-04 04:41:45 +01:00
|
|
|
foreach ($assign_var->items as $offset => $assign_var_item) {
|
2017-01-02 21:31:18 +01:00
|
|
|
// $assign_var_item can be null e.g. list($a, ) = ['a', 'b']
|
|
|
|
if (!$assign_var_item) {
|
2016-11-01 16:37:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$var = $assign_var_item->value;
|
|
|
|
|
2016-11-01 19:32:19 +01:00
|
|
|
if ($assign_value instanceof PhpParser\Node\Expr\Array_
|
2018-01-14 00:33:32 +01:00
|
|
|
&& isset($assign_var_item->value->inferredType)
|
2016-11-01 19:32:19 +01:00
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyze(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$var,
|
2018-01-14 00:33:32 +01:00
|
|
|
$assign_var_item->value,
|
2016-12-04 20:14:00 +01:00
|
|
|
null,
|
2016-11-02 07:29:00 +01:00
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
|
2017-01-25 08:11:24 +01:00
|
|
|
continue;
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
if (isset($assign_value_type->getTypes()['array'])
|
|
|
|
&& ($array_atomic_type = $assign_value_type->getTypes()['array'])
|
|
|
|
&& $array_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
&& !$assign_var_item->key
|
|
|
|
&& isset($array_atomic_type->properties[$offset]) // if object-like has int offsets
|
2017-01-25 08:11:24 +01:00
|
|
|
) {
|
|
|
|
self::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$var,
|
|
|
|
null,
|
2018-01-09 21:05:48 +01:00
|
|
|
$array_atomic_type->properties[(string)$offset],
|
2017-01-25 08:11:24 +01:00
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
|
2016-11-01 19:32:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:13:18 +01:00
|
|
|
if ($var instanceof PhpParser\Node\Expr\List_
|
|
|
|
|| $var instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
2018-03-21 13:48:30 +01:00
|
|
|
/** @var Type\Atomic\ObjectLike|Type\Atomic\TArray|null */
|
|
|
|
$array_value_type = isset($assign_value_type->getTypes()['array'])
|
|
|
|
? $assign_value_type->getTypes()['array']
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($array_value_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$array_value_type = $array_value_type->getGenericArrayType();
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:13:18 +01:00
|
|
|
self::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$var,
|
|
|
|
null,
|
2018-03-21 13:48:30 +01:00
|
|
|
$array_value_type ? clone $array_value_type->type_params[1] : Type::getMixed(),
|
2017-12-19 05:13:18 +01:00
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-21 13:48:30 +01:00
|
|
|
$list_var_id = ExpressionChecker::getArrayVarId(
|
2016-11-01 16:37:58 +01:00
|
|
|
$var,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($list_var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$list_var_id] = true;
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->assigned_var_ids[$list_var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$list_var_id] = true;
|
|
|
|
|
|
|
|
$already_in_scope = isset($context->vars_in_scope[$var_id]);
|
2017-02-08 00:09:12 +01:00
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
if (strpos($list_var_id, '-') === false && strpos($list_var_id, '[') === false) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$location = new CodeLocation($statements_checker, $var);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
|
|
if ($context->collect_references) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->unreferenced_vars[$list_var_id] = [$location->getHash() => $location];
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($list_var_id)) {
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$list_var_id,
|
|
|
|
$location,
|
|
|
|
$context->branch_point
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$statements_checker->registerVariableAssignment(
|
|
|
|
$list_var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($context->byref_constraints[$list_var_id])) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$statements_checker->registerVariableUses([$location->getHash() => $location]);
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2016-12-04 20:14:00 +01:00
|
|
|
|
2017-08-19 05:14:38 +02:00
|
|
|
$new_assign_type = null;
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
if (isset($assign_value_type->getTypes()['array'])) {
|
|
|
|
$array_atomic_type = $assign_value_type->getTypes()['array'];
|
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\TArray) {
|
|
|
|
$new_assign_type = clone $array_atomic_type->type_params[1];
|
|
|
|
} elseif ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
2016-12-04 20:14:00 +01:00
|
|
|
if ($assign_var_item->key
|
2017-12-19 00:47:17 +01:00
|
|
|
&& ($assign_var_item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $assign_var_item->key instanceof PhpParser\Node\Scalar\LNumber)
|
2018-01-09 21:05:48 +01:00
|
|
|
&& isset($array_atomic_type->properties[$assign_var_item->key->value])
|
2016-12-04 20:14:00 +01:00
|
|
|
) {
|
2017-08-19 05:14:38 +02:00
|
|
|
$new_assign_type =
|
2018-01-09 21:05:48 +01:00
|
|
|
clone $array_atomic_type->properties[$assign_var_item->key->value];
|
2016-12-04 20:14:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
if ($already_in_scope) {
|
2017-08-19 05:14:38 +02:00
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$list_var_id,
|
|
|
|
$context->vars_in_scope[$list_var_id],
|
|
|
|
$new_assign_type,
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-21 04:11:01 +02:00
|
|
|
foreach ($var_comments as $var_comment) {
|
|
|
|
try {
|
|
|
|
if ($var_comment->var_id === $list_var_id) {
|
|
|
|
$var_comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$var_comment_type->setFromDocblock();
|
|
|
|
|
|
|
|
$new_assign_type = $var_comment_type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:14:38 +02:00
|
|
|
$context->vars_in_scope[$list_var_id] = $new_assign_type ?: Type::getMixed();
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2018-03-07 17:16:56 +01:00
|
|
|
ArrayAssignmentChecker::analyze(
|
2017-01-07 21:09:47 +01:00
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
|
|
|
$context,
|
|
|
|
$assign_value_type
|
2018-03-07 17:16:56 +01:00
|
|
|
);
|
2017-12-19 04:35:03 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2018-05-09 03:21:22 +02:00
|
|
|
if (!$assign_var->name instanceof PhpParser\Node\Identifier) {
|
2018-10-26 22:17:15 +02:00
|
|
|
// this can happen when the user actually means to type $this-><autocompleted>, but there's
|
|
|
|
// a variable on the next line
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $assign_var->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $assign_var->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($assign_var->name instanceof PhpParser\Node\Identifier) {
|
2018-05-09 03:21:22 +02:00
|
|
|
$prop_name = $assign_var->name->name;
|
|
|
|
} elseif (isset($assign_var->name->inferredType)
|
|
|
|
&& $assign_var->name->inferredType->isSingleStringLiteral()
|
|
|
|
) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$prop_name = $assign_var->name->inferredType->getSingleStringLiteral()->value;
|
2018-05-09 03:21:22 +02:00
|
|
|
} else {
|
|
|
|
$prop_name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prop_name) {
|
2018-01-14 18:09:40 +01:00
|
|
|
PropertyAssignmentChecker::analyzeInstance(
|
2017-12-19 04:35:03 +01:00
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
2018-05-09 03:21:22 +02:00
|
|
|
$prop_name,
|
2017-12-19 04:35:03 +01:00
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $assign_var->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\StaticPropertyFetch &&
|
2018-04-17 18:16:25 +02:00
|
|
|
$assign_var->class instanceof PhpParser\Node\Name
|
2016-11-01 16:37:58 +01:00
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $assign_var, $context) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:34:17 +01:00
|
|
|
if ($context->check_classes) {
|
2018-01-14 18:09:40 +01:00
|
|
|
PropertyAssignmentChecker::analyzeStatic(
|
2017-01-18 05:34:17 +01:00
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 06:20:58 +01:00
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->isVoid()) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2017-11-28 06:46:41 +01:00
|
|
|
new AssignmentToVoid(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Cannot assign ' . $var_id . ' to type void',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $assign_var)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-11-01 16:37:58 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-09 06:29:43 +01:00
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
|
|
|
|
return Type::getMixed();
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
return $assign_value_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\AssignOp $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-11-01 16:37:58 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeAssignmentOperation(
|
2016-11-01 16:37:58 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\AssignOp $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-11-13 21:39:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$array_var_id = ExpressionChecker::getArrayVarId(
|
2016-11-13 21:39:16 +01:00
|
|
|
$stmt->var,
|
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-13 21:39:16 +01:00
|
|
|
);
|
|
|
|
|
2018-06-17 05:40:25 +02:00
|
|
|
if ($array_var_id && $context->collect_references && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
$location = new CodeLocation($statements_checker, $stmt->var);
|
|
|
|
$context->assigned_var_ids[$array_var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$array_var_id] = true;
|
|
|
|
$statements_checker->registerVariableAssignment(
|
|
|
|
$array_var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
$context->unreferenced_vars[$array_var_id] = [$location->getHash() => $location];
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:39:16 +01:00
|
|
|
$var_type = isset($stmt->var->inferredType) ? clone $stmt->var->inferredType : null;
|
|
|
|
$expr_type = isset($stmt->expr->inferredType) ? $stmt->expr->inferredType : null;
|
|
|
|
|
2016-12-08 22:37:14 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Plus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\AssignOp\Minus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\AssignOp\Mod ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\AssignOp\Mul ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\AssignOp\Pow
|
|
|
|
) {
|
2018-01-14 18:09:40 +01:00
|
|
|
BinaryOpChecker::analyzeNonDivArithmenticOp(
|
2016-12-24 03:30:32 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
|
|
|
$stmt,
|
2017-09-07 03:44:26 +02:00
|
|
|
$result_type,
|
|
|
|
$context
|
2016-12-24 03:30:32 +01:00
|
|
|
);
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2018-05-03 02:10:08 +02:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
ArrayAssignmentChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt->var,
|
|
|
|
$context,
|
2018-05-07 22:22:04 +02:00
|
|
|
$result_type ?: Type::getMixed(true)
|
2018-05-03 02:10:08 +02:00
|
|
|
);
|
|
|
|
} elseif ($result_type && $array_var_id) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
2018-06-17 05:40:25 +02:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$array_var_id];
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
2016-12-08 22:37:14 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp\Div
|
|
|
|
&& $var_type
|
|
|
|
&& $expr_type
|
2018-05-07 07:26:06 +02:00
|
|
|
&& $var_type->hasDefinitelyNumericType()
|
|
|
|
&& $expr_type->hasDefinitelyNumericType()
|
2017-11-19 18:33:43 +01:00
|
|
|
&& $array_var_id
|
2016-12-08 22:37:14 +01:00
|
|
|
) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$context->vars_in_scope[$array_var_id] = Type::combineUnionTypes(Type::getFloat(), Type::getInt());
|
2018-06-17 05:40:25 +02:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$array_var_id];
|
2016-12-24 03:30:32 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp\Concat) {
|
2018-01-14 18:09:40 +01:00
|
|
|
BinaryOpChecker::analyzeConcatOp(
|
2016-12-24 03:30:32 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
2017-09-03 01:23:00 +02:00
|
|
|
$context,
|
2016-12-24 03:30:32 +01:00
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($result_type && $array_var_id) {
|
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
2018-06-17 05:40:25 +02:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$array_var_id];
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-04 04:07:14 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\AssignRef $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-04 04:07:14 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeAssignmentRef(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\AssignRef $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-02-23 06:25:28 +01:00
|
|
|
if (self::analyze(
|
|
|
|
$statements_checker,
|
2017-02-04 04:07:14 +01:00
|
|
|
$stmt->var,
|
2017-02-23 06:25:28 +01:00
|
|
|
$stmt->expr,
|
|
|
|
null,
|
2017-02-04 04:07:14 +01:00
|
|
|
$context,
|
2017-11-09 05:27:51 +01:00
|
|
|
(string)$stmt->getDocComment()
|
2017-02-23 06:25:28 +01:00
|
|
|
) === false) {
|
2017-02-04 04:07:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-11 16:39:21 +01:00
|
|
|
|
|
|
|
$lhs_var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->var,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
$rhs_var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->expr,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($lhs_var_id) {
|
|
|
|
$context->vars_in_scope[$lhs_var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rhs_var_id) {
|
|
|
|
$context->vars_in_scope[$rhs_var_id] = Type::getMixed();
|
|
|
|
}
|
2017-02-04 04:07:14 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|