2016-11-01 16:37:58 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
2016-12-07 00:27:22 +01:00
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Stmt\PropertyProperty;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
|
|
|
use Psalm\Checker\InterfaceChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
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-05-25 07:32:34 +02:00
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Issue\InvalidPropertyAssignment;
|
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;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Issue\MixedPropertyAssignment;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
|
|
|
use Psalm\Issue\NullPropertyAssignment;
|
2017-11-15 17:15:03 +01:00
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyAssignment;
|
2017-02-11 23:55:08 +01:00
|
|
|
use Psalm\Issue\PossiblyNullPropertyAssignment;
|
2017-02-23 06:25:28 +01:00
|
|
|
use Psalm\Issue\ReferenceConstraintViolation;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedPropertyAssignment;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyAssignment;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Type;
|
2017-11-19 18:33:43 +01:00
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TObject;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
$var_comment = null;
|
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) {
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
|
|
|
$var_comment = CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$statements_checker->getAliases(),
|
|
|
|
null,
|
|
|
|
$came_from_line_number
|
|
|
|
);
|
|
|
|
} 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
|
|
|
|
2017-10-07 16:22:52 +02:00
|
|
|
if ($var_comment) {
|
|
|
|
$comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
|
|
|
Type::parseString($var_comment->type),
|
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$comment_type->setFromDocblock();
|
|
|
|
|
|
|
|
if ($var_comment->var_id && $var_comment->var_id !== $var_id) {
|
|
|
|
$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-11-01 16:37:58 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if ($assign_value && ExpressionChecker::analyze($statements_checker, $assign_value, $context) === false) {
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
2017-04-02 01:04:49 +02:00
|
|
|
if ($array_var_id) {
|
2017-04-02 21:26:10 +02:00
|
|
|
$context->removeDescendents($array_var_id, null, $assign_value_type);
|
2017-04-02 01:04:49 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
// if we're not exiting immediately, make everything mixed
|
2017-05-25 07:32:34 +02:00
|
|
|
$context->vars_in_scope[$var_id] =
|
2017-10-07 16:22:52 +02:00
|
|
|
$var_comment && (!$var_comment->var_id || $var_comment->var_id === $var_id) && $comment_type
|
|
|
|
? $comment_type
|
2017-05-25 07:32:34 +02:00
|
|
|
: Type::getMixed();
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:22:52 +02:00
|
|
|
if ($var_comment && (!$var_comment->var_id || $var_comment->var_id === $var_id) && $comment_type) {
|
|
|
|
$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
|
|
|
/** @var Type\Union */
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
if ($assign_value_type->isMixed()) {
|
|
|
|
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
|
|
|
|
}
|
2017-02-23 06:25:28 +01:00
|
|
|
} elseif ($var_id && isset($context->byref_constraints[$var_id])) {
|
|
|
|
if (!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2017-02-23 06:25:28 +01:00
|
|
|
$assign_value_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$context->byref_constraints[$var_id]->type
|
2017-02-23 06:25:28 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
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;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($var_id)) {
|
|
|
|
$statements_checker->registerVariable($var_id, new CodeLocation($statements_checker, $assign_var));
|
|
|
|
}
|
2016-12-04 19:35:38 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\List_
|
|
|
|
|| $assign_var instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
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_
|
|
|
|
&& isset($assign_value->items[$offset]->value->inferredType)
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyze(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$var,
|
|
|
|
$assign_value->items[$offset]->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
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($assign_value_type->types['array']) &&
|
2017-01-25 08:11:24 +01:00
|
|
|
$assign_value_type->types['array'] instanceof Type\Atomic\ObjectLike &&
|
|
|
|
!$assign_var_item->key &&
|
|
|
|
isset($assign_value_type->types['array']->properties[$offset]) // if object-like has int offsets
|
|
|
|
) {
|
|
|
|
self::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$var,
|
|
|
|
null,
|
2017-11-16 02:45:53 +01:00
|
|
|
$assign_value_type->types['array']->properties[(string)$offset],
|
2017-01-25 08:11:24 +01:00
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
|
2016-11-01 19:32:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
$list_var_id = ExpressionChecker::getVarId(
|
|
|
|
$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;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($list_var_id)) {
|
2017-04-28 06:31:55 +02:00
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$list_var_id,
|
|
|
|
new CodeLocation($statements_checker, $var)
|
|
|
|
);
|
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;
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
if (isset($assign_value_type->types['array'])) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($assign_value_type->types['array'] instanceof Type\Atomic\TArray) {
|
2017-08-19 05:14:38 +02:00
|
|
|
$new_assign_type = clone $assign_value_type->types['array']->type_params[1];
|
2017-01-15 01:06:58 +01:00
|
|
|
} elseif ($assign_value_type->types['array'] 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)
|
2016-12-04 20:14:00 +01:00
|
|
|
&& isset($assign_value_type->types['array']->properties[$assign_var_item->key->value])
|
|
|
|
) {
|
2017-08-19 05:14:38 +02:00
|
|
|
$new_assign_type =
|
2016-12-04 20:14:00 +01:00
|
|
|
clone $assign_value_type->types['array']->properties[$assign_var_item->key->value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:14:38 +02:00
|
|
|
if ($context->hasVariable($list_var_id)) {
|
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$list_var_id,
|
|
|
|
$context->vars_in_scope[$list_var_id],
|
|
|
|
$new_assign_type,
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeArrayAssignment(
|
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
|
|
|
$context,
|
|
|
|
$assign_value_type
|
|
|
|
) === false
|
|
|
|
) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-15 01:19:25 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\PropertyFetch && is_string($assign_var->name)) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyzePropertyAssignment(
|
2016-12-07 00:27:22 +01:00
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
|
|
|
$assign_var->name,
|
|
|
|
$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-02 07:29:00 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\StaticPropertyFetch &&
|
|
|
|
$assign_var->class instanceof PhpParser\Node\Name &&
|
|
|
|
is_string($assign_var->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) {
|
|
|
|
self::analyzeStaticPropertyAssignment(
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
$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
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::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
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($result_type && $array_var_id) {
|
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
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
|
|
|
|
&& $var_type->hasNumericType()
|
|
|
|
&& $expr_type->hasNumericType()
|
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());
|
2016-12-24 03:30:32 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp\Concat) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::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;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
/**
|
2016-12-07 00:27:22 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PropertyFetch|PropertyProperty $stmt
|
|
|
|
* @param string $prop_name
|
|
|
|
* @param PhpParser\Node\Expr|null $assignment_value
|
|
|
|
* @param Type\Union $assignment_value_type
|
|
|
|
* @param Context $context
|
2017-12-17 16:58:03 +01:00
|
|
|
* @param bool $direct_assignment whether the variable is assigned explictly
|
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 analyzePropertyAssignment(
|
2016-11-01 16:37:58 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
$stmt,
|
|
|
|
$prop_name,
|
2017-05-05 03:57:26 +02:00
|
|
|
$assignment_value,
|
2016-12-07 00:27:22 +01:00
|
|
|
Type\Union $assignment_value_type,
|
2017-12-17 16:58:03 +01:00
|
|
|
Context $context,
|
|
|
|
$direct_assignment = true
|
2016-11-01 16:37:58 +01:00
|
|
|
) {
|
|
|
|
$class_property_types = [];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-11-03 17:27:01 +01:00
|
|
|
$property_exists = false;
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
if ($stmt instanceof PropertyProperty) {
|
2016-12-06 22:33:47 +01:00
|
|
|
if (!$context->self || !$stmt->default) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_id = $context->self . '::$' . $prop_name;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassLikeChecker::propertyExists($project_checker, $property_id)) {
|
2017-01-02 01:09:17 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-03 17:27:01 +01:00
|
|
|
$property_exists = true;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty($project_checker, $property_id);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$declaring_property_class);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$class_property_type = $class_storage->properties[$prop_name]->type;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type ? clone $class_property_type : Type::getMixed();
|
|
|
|
|
|
|
|
$var_id = '$this->' . $prop_name;
|
2016-12-15 01:19:25 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
2016-12-15 01:19:25 +01:00
|
|
|
return false;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 02:05:32 +01:00
|
|
|
$lhs_type = isset($stmt->var->inferredType) ? $stmt->var->inferredType : null;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2016-12-25 02:08:58 +01:00
|
|
|
if ($lhs_type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
$lhs_var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->var,
|
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-12-07 00:27:22 +01:00
|
|
|
);
|
|
|
|
|
2016-11-01 19:20:01 +01:00
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-01 19:20:01 +01:00
|
|
|
);
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
2017-12-17 16:58:03 +01:00
|
|
|
|
|
|
|
if ($direct_assignment && 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(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
if ($lhs_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyAssignment(
|
2016-12-07 00:27:22 +01:00
|
|
|
$lhs_var_id . ' of type mixed cannot be assigned to',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($lhs_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyAssignment(
|
2016-12-07 00:27:22 +01:00
|
|
|
$lhs_var_id . ' of type null cannot be assigned to',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-09 22:38:58 +02:00
|
|
|
if ($lhs_type->isNullable() && !$lhs_type->ignore_nullable_issues) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2017-02-11 23:55:08 +01:00
|
|
|
new PossiblyNullPropertyAssignment(
|
2016-12-07 00:27:22 +01:00
|
|
|
$lhs_var_id . ' with possibly null type \'' . $lhs_type . '\' cannot be assigned to',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = false;
|
|
|
|
|
2017-11-15 17:15:03 +01:00
|
|
|
$invalid_assignment_types = [];
|
|
|
|
|
|
|
|
$has_valid_assignment_type = false;
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
foreach ($lhs_type->types as $lhs_type_part) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($lhs_type_part instanceof TNull) {
|
2016-11-01 16:37:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$lhs_type_part instanceof TObject && !$lhs_type_part instanceof TNamedObject) {
|
2017-11-15 17:15:03 +01:00
|
|
|
$invalid_assignment_types[] = (string)$lhs_type_part;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-15 17:15:03 +01:00
|
|
|
$has_valid_assignment_type = true;
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
// stdClass and SimpleXMLElement are special cases where we cannot infer the return types
|
|
|
|
// but we don't want to throw an error
|
|
|
|
// Hack has a similar issue: https://github.com/facebook/hhvm/issues/5164
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($lhs_type_part instanceof TObject ||
|
2017-12-01 01:00:09 +01:00
|
|
|
(
|
|
|
|
$lhs_type_part instanceof TNamedObject &&
|
2017-01-15 01:06:58 +01:00
|
|
|
in_array(
|
|
|
|
strtolower($lhs_type_part->value),
|
2017-06-29 05:37:02 +02:00
|
|
|
['stdclass', 'simplexmlelement', 'dateinterval', 'domdocument', 'domnode'],
|
|
|
|
true
|
2017-01-15 01:06:58 +01:00
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
)
|
|
|
|
) {
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($var_id) {
|
|
|
|
if ($lhs_type_part instanceof TNamedObject &&
|
|
|
|
strtolower($lhs_type_part->value) === 'stdclass'
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ExpressionChecker::isMock($lhs_type_part->value)) {
|
2017-01-15 16:24:38 +01:00
|
|
|
$has_regular_setter = true;
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassChecker::classExists($project_checker, $lhs_type_part->value)) {
|
|
|
|
if (InterfaceChecker::interfaceExists($project_checker, $lhs_type_part->value)) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot set properties of undefined class ' . $lhs_type_part->value,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 00:41:33 +02:00
|
|
|
if ($lhs_var_id !== '$this' &&
|
2017-07-29 21:05:06 +02:00
|
|
|
MethodChecker::methodExists($project_checker, $lhs_type_part . '::__set')
|
2017-05-05 00:41:33 +02:00
|
|
|
) {
|
2017-11-17 02:47:58 +01:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$lhs_type_part);
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-11-17 02:47:58 +01:00
|
|
|
if ($var_id) {
|
2017-05-05 17:20:05 +02:00
|
|
|
if (isset($class_storage->pseudo_property_set_types['$' . $prop_name])) {
|
2017-05-05 03:57:26 +02:00
|
|
|
$class_property_types[] =
|
2017-05-05 17:20:05 +02:00
|
|
|
clone $class_storage->pseudo_property_set_types['$' . $prop_name];
|
2017-05-05 00:35:05 +02:00
|
|
|
$has_regular_setter = true;
|
2017-11-03 17:27:01 +01:00
|
|
|
$property_exists = true;
|
2017-05-05 00:35:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
2017-11-17 02:47:58 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have an explicit list of all allowed magic properties on the class, and we're
|
|
|
|
* not in that list, fall through
|
|
|
|
*/
|
|
|
|
if (!$var_id || !$class_storage->sealed_properties) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-15 16:24:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = true;
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $prop_name;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassLikeChecker::propertyExists($project_checker, $property_id)) {
|
2017-11-03 17:27:01 +01:00
|
|
|
$has_regular_setter = true;
|
|
|
|
|
2016-12-15 01:19:25 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable && $stmt->var->name === 'this') {
|
2017-09-24 14:17:27 +02:00
|
|
|
// if this is a proper error, we'll see it on the first pass
|
|
|
|
if ($context->collect_mutations) {
|
2017-11-03 17:27:01 +01:00
|
|
|
continue;
|
2017-09-24 14:17:27 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyAssignment(
|
2017-01-02 01:09:17 +01:00
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
2017-01-02 01:09:17 +01:00
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:27:01 +01:00
|
|
|
$property_exists = true;
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
if (ClassLikeChecker::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 01:09:17 +01:00
|
|
|
$lhs_type_part->value . '::$' . $prop_name
|
|
|
|
);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$declaring_property_class);
|
|
|
|
|
2017-11-16 02:45:53 +01:00
|
|
|
$property_storage = $class_storage->properties[$prop_name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
if ($property_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedProperty(
|
|
|
|
$property_id . ' is marked deprecated',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$class_property_type = $property_storage->type;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
if ($class_property_type === false) {
|
2017-04-15 05:09:34 +02:00
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
|
|
|
|
if (!$assignment_value_type->isMixed()) {
|
|
|
|
if ($property_storage->suggested_type) {
|
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
$assignment_value_type,
|
|
|
|
$property_storage->suggested_type
|
|
|
|
);
|
|
|
|
} else {
|
2017-04-15 05:26:58 +02:00
|
|
|
$property_storage->suggested_type =
|
|
|
|
$lhs_var_id === '$this' &&
|
|
|
|
($context->inside_constructor || $context->collect_initializations)
|
|
|
|
? $assignment_value_type
|
|
|
|
: Type::combineUnionTypes(Type::getNull(), $assignment_value_type);
|
2017-04-15 05:09:34 +02:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_property_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$class_property_type,
|
2016-12-11 00:24:28 +01:00
|
|
|
$lhs_type_part->value
|
|
|
|
);
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type;
|
|
|
|
}
|
|
|
|
|
2017-11-15 17:15:03 +01:00
|
|
|
if ($invalid_assignment_types) {
|
|
|
|
$invalid_assignment_type = $invalid_assignment_types[0];
|
|
|
|
|
|
|
|
if (!$has_valid_assignment_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
|
|
|
$lhs_var_id . ' with non-object type \'' . $invalid_assignment_type .
|
|
|
|
'\' cannot treated as an object',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyAssignment(
|
|
|
|
$lhs_var_id . ' with possible non-object type \'' . $invalid_assignment_type .
|
|
|
|
'\' cannot treated as an object',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
if (!$has_regular_setter) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($var_id) {
|
|
|
|
// because we don't want to be assigning for property declarations
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id && count($class_property_types) === 1 && isset($class_property_types[0]->types['stdClass'])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:27:01 +01:00
|
|
|
if (!$property_exists) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
if ($assignment_value_type->isMixed()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 17:15:03 +01:00
|
|
|
$invalid_assignment_value_types = [];
|
|
|
|
|
|
|
|
$has_valid_assignment_value_type = false;
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
foreach ($class_property_types as $class_property_type) {
|
|
|
|
if ($class_property_type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
2017-05-22 20:50:03 +02:00
|
|
|
$assignment_value_type->ignore_nullable_issues
|
2017-01-02 21:31:18 +01:00
|
|
|
)) {
|
2017-11-15 17:15:03 +01:00
|
|
|
$invalid_assignment_value_types[] = [
|
|
|
|
(string)$class_property_type,
|
|
|
|
(string)$assignment_value_type,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$has_valid_assignment_value_type = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_assignment_value_types) {
|
|
|
|
list($class_property_type, $invalid_assignment_value_type)
|
|
|
|
= $invalid_assignment_value_types[0];
|
|
|
|
|
|
|
|
if (!$has_valid_assignment_value_type) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
2017-11-15 17:15:03 +01:00
|
|
|
$var_id . ' with declared type \'' . $class_property_type .
|
|
|
|
'\' cannot be assigned type \'' . $invalid_assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyAssignment(
|
|
|
|
$var_id . ' with declared type \'' . $class_property_type .
|
|
|
|
'\' cannot be assigned possibly different type \'' .
|
|
|
|
$invalid_assignment_value_type . '\'',
|
2016-12-06 22:33:47 +01:00
|
|
|
new CodeLocation(
|
|
|
|
$statements_checker->getSource(),
|
2017-06-30 07:24:45 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
2016-12-06 22:33:47 +01:00
|
|
|
)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
2016-12-07 00:27:22 +01:00
|
|
|
* @param PhpParser\Node\Expr|null $assignment_value
|
|
|
|
* @param Type\Union $assignment_value_type
|
2016-11-02 07:29:00 +01:00
|
|
|
* @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
|
|
|
protected static function analyzeStaticPropertyAssignment(
|
2016-11-01 16:37:58 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\StaticPropertyFetch $stmt,
|
2017-05-05 03:57:26 +02:00
|
|
|
$assignment_value,
|
2016-12-07 00:27:22 +01:00
|
|
|
Type\Union $assignment_value_type,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt,
|
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
|
|
|
);
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = (string)$stmt->class->inferredType;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
$prop_name = $stmt->name;
|
|
|
|
|
2016-12-29 06:32:12 +01:00
|
|
|
if (!is_string($prop_name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassLikeChecker::propertyExists($project_checker, $property_id)) {
|
2017-01-02 01:09:17 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts[0] === 'this') {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2017-01-02 01:09:17 +01:00
|
|
|
new UndefinedThisPropertyAssignment(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-02 01:09:17 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassLikeChecker::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 01:09:17 +01:00
|
|
|
$fq_class_name . '::$' . $prop_name
|
|
|
|
);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$declaring_property_class);
|
|
|
|
|
2017-11-16 02:45:53 +01:00
|
|
|
$property_storage = $class_storage->properties[$prop_name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$class_property_type = $property_storage->type;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
if ($class_property_type === false) {
|
|
|
|
$class_property_type = Type::getMixed();
|
2017-04-15 05:09:34 +02:00
|
|
|
|
|
|
|
if (!$assignment_value_type->isMixed()) {
|
|
|
|
if ($property_storage->suggested_type) {
|
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
$assignment_value_type,
|
|
|
|
$property_storage->suggested_type
|
|
|
|
);
|
|
|
|
} else {
|
2017-04-15 05:26:58 +02:00
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
Type::getNull(),
|
|
|
|
$assignment_value_type
|
|
|
|
);
|
2017-04-15 05:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
$class_property_type = clone $class_property_type;
|
|
|
|
}
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
if ($assignment_value_type->isMixed()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_property_type->isMixed()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_property_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$class_property_type,
|
|
|
|
$fq_class_name
|
|
|
|
);
|
2016-11-05 23:31:09 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
$assignment_value_type,
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_property_type
|
2017-01-02 21:31:18 +01:00
|
|
|
)) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
2016-11-02 07:29:00 +01:00
|
|
|
$var_id . ' with declared type \'' . $class_property_type . '\' cannot be assigned type \'' .
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$assignment_value ?: $stmt
|
|
|
|
)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\ArrayDimFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param Type\Union $assignment_value_type
|
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
|
|
|
* @psalm-suppress MixedMethodCall - some funky logic here
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeArrayAssignment(
|
2016-11-01 16:37:58 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\ArrayDimFetch $stmt,
|
|
|
|
Context $context,
|
|
|
|
Type\Union $assignment_value_type
|
|
|
|
) {
|
|
|
|
$nesting = 0;
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt->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
|
|
|
$nesting
|
|
|
|
);
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
AssignmentChecker::updateArrayType(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt,
|
|
|
|
$assignment_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($stmt->var->inferredType) && $var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\ArrayDimFetch $stmt
|
|
|
|
* @param Type\Union $assignment_type
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function updateArrayType(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\ArrayDimFetch $stmt,
|
|
|
|
Type\Union $assignment_type,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$root_array_expr = $stmt;
|
|
|
|
|
|
|
|
$child_stmts = [];
|
|
|
|
|
|
|
|
while ($root_array_expr->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
$child_stmts[] = $root_array_expr;
|
|
|
|
$root_array_expr = $root_array_expr->var;
|
|
|
|
}
|
|
|
|
|
|
|
|
$child_stmts[] = $root_array_expr;
|
|
|
|
$root_array_expr = $root_array_expr->var;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze(
|
2016-11-01 16:37:58 +01:00
|
|
|
$statements_checker,
|
2017-11-19 18:33:43 +01:00
|
|
|
$root_array_expr,
|
2016-11-01 16:37:58 +01:00
|
|
|
$context,
|
2017-11-19 18:33:43 +01:00
|
|
|
true
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2017-11-19 18:33:43 +01:00
|
|
|
// fall through
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$root_type = isset($root_array_expr->inferredType) ? $root_array_expr->inferredType : Type::getMixed();
|
|
|
|
|
|
|
|
if ($root_type->isMixed()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$child_stmts = array_reverse($child_stmts);
|
|
|
|
|
|
|
|
$current_type = $root_type;
|
|
|
|
|
|
|
|
$current_dim = $stmt->dim;
|
|
|
|
|
|
|
|
$reversed_child_stmts = [];
|
|
|
|
|
|
|
|
// gets a variable id that *may* contain array keys
|
|
|
|
$root_var_id = ExpressionChecker::getRootVarId(
|
|
|
|
$root_array_expr,
|
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-11-19 18:33:43 +01:00
|
|
|
$var_id_additions = [];
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-20 23:10:05 +01:00
|
|
|
$real_var_id = true;
|
|
|
|
|
2017-11-20 17:49:26 +01:00
|
|
|
$child_stmt = null;
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
// First go from the root element up, and go as far as we can to figure out what
|
|
|
|
// array types there are
|
|
|
|
while ($child_stmts) {
|
2017-11-24 18:10:30 +01:00
|
|
|
/** @var PhpParser\Node\Expr\ArrayDimFetch */
|
2017-11-19 18:33:43 +01:00
|
|
|
$child_stmt = array_shift($child_stmts);
|
2017-01-20 06:23:58 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if (count($child_stmts)) {
|
|
|
|
array_unshift($reversed_child_stmts, $child_stmt);
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($child_stmt->dim) {
|
|
|
|
if (ExpressionChecker::analyze(
|
|
|
|
$statements_checker,
|
|
|
|
$child_stmt->dim,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
2017-01-20 06:23:58 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if (!isset($child_stmt->dim->inferredType)) {
|
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
2017-11-19 18:33:43 +01:00
|
|
|
|
|
|
|
if ($child_stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$var_id_additions[] = '[\'' . $child_stmt->dim->value . '\']';
|
|
|
|
} else {
|
|
|
|
$var_id_additions[] = '[' . $child_stmt->dim->inferredType . ']';
|
2017-11-20 23:10:05 +01:00
|
|
|
$real_var_id = false;
|
2016-12-12 05:40:46 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-11-19 18:33:43 +01:00
|
|
|
$var_id_additions[] = '';
|
2017-11-20 23:10:05 +01:00
|
|
|
$real_var_id = false;
|
2017-11-19 18:33:43 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if (!isset($child_stmt->var->inferredType)) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-17 18:27:16 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($child_stmt->var->inferredType->isEmpty()) {
|
|
|
|
$child_stmt->var->inferredType = Type::getEmptyArray();
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$array_var_id = $root_var_id . implode('', $var_id_additions);
|
2017-11-17 18:27:16 +01:00
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$child_stmt->inferredType = FetchChecker::getArrayAccessTypeGivenOffset(
|
|
|
|
$statements_checker,
|
|
|
|
$child_stmt,
|
|
|
|
$child_stmt->var->inferredType,
|
|
|
|
isset($child_stmt->dim->inferredType) ? $child_stmt->dim->inferredType : Type::getInt(),
|
|
|
|
true,
|
|
|
|
$array_var_id,
|
|
|
|
$child_stmts ? null : $assignment_type
|
|
|
|
);
|
|
|
|
|
2017-11-20 03:24:29 +01:00
|
|
|
if (!$child_stmts) {
|
|
|
|
$child_stmt->inferredType = $assignment_type;
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$current_type = $child_stmt->inferredType;
|
|
|
|
$current_dim = $child_stmt->dim;
|
|
|
|
|
|
|
|
if ($child_stmt->var->inferredType->isMixed()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:10:05 +01:00
|
|
|
if ($root_var_id
|
|
|
|
&& $real_var_id
|
|
|
|
&& isset($child_stmt->var->inferredType)
|
|
|
|
&& !$child_stmt->var->inferredType->hasObjectType()
|
|
|
|
) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$array_var_id = $root_var_id . implode('', $var_id_additions);
|
2017-11-20 03:24:29 +01:00
|
|
|
$context->vars_in_scope[$array_var_id] = clone $assignment_type;
|
2017-11-19 18:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// only update as many child stmts are we were able to process above
|
|
|
|
foreach ($reversed_child_stmts as $child_stmt) {
|
2017-11-20 03:24:29 +01:00
|
|
|
if (!isset($child_stmt->inferredType)) {
|
|
|
|
throw new \InvalidArgumentException('Should never get here');
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($current_dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $current_dim instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
$key_value = $current_dim->value;
|
2017-11-19 18:33:43 +01:00
|
|
|
|
2017-11-20 03:24:29 +01:00
|
|
|
$has_matching_objectlike_property = false;
|
|
|
|
|
|
|
|
foreach ($child_stmt->inferredType->types as $type) {
|
|
|
|
if ($type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
if (isset($type->properties[$key_value])) {
|
2017-11-20 03:24:29 +01:00
|
|
|
$has_matching_objectlike_property = true;
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
$type->properties[$key_value] = clone $current_type;
|
2017-11-20 03:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_matching_objectlike_property) {
|
|
|
|
$array_assignment_type = new Type\Union([
|
2017-12-19 00:47:17 +01:00
|
|
|
new ObjectLike([$key_value => $current_type]),
|
2017-11-20 03:24:29 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$new_child_type = Type::combineUnionTypes(
|
|
|
|
$child_stmt->inferredType,
|
|
|
|
$array_assignment_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_child_type = $child_stmt->inferredType; // noop
|
|
|
|
}
|
2017-11-19 18:33:43 +01:00
|
|
|
} else {
|
|
|
|
$array_assignment_type = new Type\Union([
|
|
|
|
new TArray([
|
|
|
|
isset($current_dim->inferredType) ? $current_dim->inferredType : Type::getInt(),
|
|
|
|
$current_type,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
|
2017-11-20 03:24:29 +01:00
|
|
|
$new_child_type = Type::combineUnionTypes(
|
2017-11-19 20:38:00 +01:00
|
|
|
$child_stmt->inferredType,
|
|
|
|
$array_assignment_type
|
|
|
|
);
|
|
|
|
}
|
2017-11-19 18:33:43 +01:00
|
|
|
|
2017-11-20 22:19:40 +01:00
|
|
|
unset($new_child_type->types['null']);
|
|
|
|
|
2017-11-20 03:24:29 +01:00
|
|
|
if (!$child_stmt->inferredType->hasObjectType()) {
|
|
|
|
$child_stmt->inferredType = $new_child_type;
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$current_type = $child_stmt->inferredType;
|
|
|
|
$current_dim = $child_stmt->dim;
|
|
|
|
|
|
|
|
array_pop($var_id_additions);
|
|
|
|
|
|
|
|
if ($root_var_id) {
|
|
|
|
$array_var_id = $root_var_id . implode('', $var_id_additions);
|
|
|
|
$context->vars_in_scope[$array_var_id] = clone $child_stmt->inferredType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
$root_is_string = array_keys($root_type->types) === ['string'];
|
|
|
|
|
|
|
|
if (($current_dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $current_dim instanceof PhpParser\Node\Scalar\LNumber)
|
|
|
|
&& ($current_dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| !$root_is_string)
|
|
|
|
) {
|
|
|
|
$key_value = $current_dim->value;
|
2017-11-19 18:33:43 +01:00
|
|
|
|
2017-11-20 04:28:52 +01:00
|
|
|
$has_matching_objectlike_property = false;
|
|
|
|
|
|
|
|
foreach ($root_type->types as $type) {
|
|
|
|
if ($type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
if (isset($type->properties[$key_value])) {
|
2017-11-20 04:28:52 +01:00
|
|
|
$has_matching_objectlike_property = true;
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
$type->properties[$key_value] = clone $current_type;
|
2017-11-20 04:28:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_matching_objectlike_property) {
|
|
|
|
$array_assignment_type = new Type\Union([
|
2017-12-19 00:47:17 +01:00
|
|
|
new ObjectLike([$key_value => $current_type]),
|
2017-11-20 04:28:52 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$new_child_type = Type::combineUnionTypes(
|
|
|
|
$root_type,
|
|
|
|
$array_assignment_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_child_type = $root_type; // noop
|
|
|
|
}
|
2017-12-19 00:47:17 +01:00
|
|
|
} elseif (!$root_is_string) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$array_assignment_type = new Type\Union([
|
|
|
|
new TArray([
|
|
|
|
isset($current_dim->inferredType) ? $current_dim->inferredType : Type::getInt(),
|
|
|
|
$current_type,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
|
2017-11-20 04:28:52 +01:00
|
|
|
$new_child_type = Type::combineUnionTypes(
|
2017-11-19 20:38:00 +01:00
|
|
|
$root_type,
|
|
|
|
$array_assignment_type
|
|
|
|
);
|
2017-11-20 23:10:05 +01:00
|
|
|
} else {
|
|
|
|
$new_child_type = $root_type;
|
2017-11-19 20:38:00 +01:00
|
|
|
}
|
2017-11-19 18:33:43 +01:00
|
|
|
|
2017-11-20 22:19:40 +01:00
|
|
|
unset($new_child_type->types['null']);
|
|
|
|
|
2017-11-20 04:28:52 +01:00
|
|
|
if (!$root_type->hasObjectType()) {
|
|
|
|
$root_type = $new_child_type;
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
$root_array_expr->inferredType = $root_type;
|
|
|
|
|
|
|
|
if ($root_array_expr instanceof PhpParser\Node\Expr\PropertyFetch && is_string($root_array_expr->name)) {
|
|
|
|
self::analyzePropertyAssignment(
|
|
|
|
$statements_checker,
|
|
|
|
$root_array_expr,
|
|
|
|
$root_array_expr->name,
|
|
|
|
null,
|
|
|
|
$root_type,
|
2017-12-17 16:58:03 +01:00
|
|
|
$context,
|
|
|
|
false
|
2017-11-19 18:33:43 +01:00
|
|
|
);
|
|
|
|
} elseif ($root_var_id) {
|
|
|
|
if ($context->hasVariable($root_var_id)) {
|
|
|
|
$context->vars_in_scope[$root_var_id] = $root_type;
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$root_var_id] = $root_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|