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\StatementsChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
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;
|
|
|
|
use Psalm\Issue\FailedTypeResolution;
|
|
|
|
use Psalm\Issue\InvalidArrayAssignment;
|
|
|
|
use Psalm\Issue\InvalidPropertyAssignment;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\InvalidScope;
|
2016-12-04 07:44:33 +01:00
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Issue\MissingPropertyDeclaration;
|
|
|
|
use Psalm\Issue\MissingPropertyType;
|
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\MixedStringOffsetAssignment;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
|
|
|
use Psalm\Issue\NullPropertyAssignment;
|
|
|
|
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-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\Generic;
|
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\Scalar;
|
|
|
|
use Psalm\Type\Atomic\TNumeric;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
|
|
|
use Psalm\Type\Atomic\TVoid;
|
|
|
|
use Psalm\Type\Atomic\TFloat;
|
|
|
|
use Psalm\Type\Atomic\TString;
|
|
|
|
use Psalm\Type\Atomic\TBool;
|
|
|
|
use Psalm\Type\Atomic\TFalse;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TEmpty;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TObject;
|
|
|
|
use Psalm\Type\Atomic\TResource;
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
|
|
|
use Psalm\Type\Atomic\TNumericString;
|
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
|
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,
|
2016-12-04 20:14:00 +01:00
|
|
|
PhpParser\Node\Expr $assign_value = null,
|
|
|
|
Type\Union $assign_value_type = null,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context,
|
|
|
|
$doc_comment
|
|
|
|
) {
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
|
|
|
if ($array_var_id) {
|
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents($array_var_id);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$type_in_comments = CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment,
|
|
|
|
$context,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
$var_id
|
|
|
|
);
|
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) {
|
2016-11-01 16:37:58 +01:00
|
|
|
// if we're not exiting immediately, make everything mixed
|
|
|
|
$context->vars_in_scope[$var_id] = $type_in_comments ?: Type::getMixed();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 01:22:30 +01:00
|
|
|
if ($type_in_comments) {
|
|
|
|
$assign_value_type = $type_in_comments;
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$statements_checker->registerVariable($var_id, $assign_var->getLine());
|
2016-12-04 19:35:38 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\List_
|
|
|
|
|| $assign_var instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
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
|
|
|
|
);
|
|
|
|
|
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;
|
|
|
|
$statements_checker->registerVariable($list_var_id, $var->getLine());
|
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) {
|
2016-12-04 20:14:00 +01:00
|
|
|
$context->vars_in_scope[$list_var_id] = clone $assign_value_type->types['array']->type_params[1];
|
|
|
|
|
|
|
|
continue;
|
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
|
|
|
|
&& $assign_var_item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
&& isset($assign_value_type->types['array']->properties[$assign_var_item->key->value])
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$list_var_id] =
|
|
|
|
clone $assign_value_type->types['array']->properties[$assign_var_item->key->value];
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
$context->vars_in_scope[$list_var_id] = 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
|
|
|
|
|
|
|
$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-07 21:09:47 +01:00
|
|
|
self::analyzeStaticPropertyAssignment(
|
2016-12-07 00:27:22 +01:00
|
|
|
$statements_checker,
|
|
|
|
$assign_var,
|
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
|
|
|
$context
|
|
|
|
);
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->isVoid()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new FailedTypeResolution(
|
|
|
|
'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
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$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,
|
|
|
|
$result_type
|
|
|
|
);
|
2016-11-13 21:39:16 +01:00
|
|
|
|
|
|
|
if ($result_type && $var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $result_type;
|
|
|
|
}
|
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()
|
|
|
|
&& $var_id
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$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,
|
|
|
|
$stmt,
|
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type && $var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $result_type;
|
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
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
|
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,
|
2016-12-07 00:27:22 +01:00
|
|
|
PhpParser\Node\Expr $assignment_value = null,
|
|
|
|
Type\Union $assignment_value_type,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$class_property_types = [];
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = $statements_checker->getFileChecker();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!ClassLikeChecker::propertyExists($property_id)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty($property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_storage = ClassLikeChecker::$storage[strtolower((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();
|
|
|
|
|
2016-12-06 22:33:47 +01:00
|
|
|
$assignment_var = $stmt->default;
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
$var_id = '$this->' . $prop_name;
|
2016-12-15 01:19:25 +01:00
|
|
|
} else {
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_var = $stmt;
|
2016-12-06 22:33:47 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if ($lhs_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyAssignment(
|
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;
|
|
|
|
|
|
|
|
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) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
2017-01-15 01:06:58 +01:00
|
|
|
$lhs_var_id . ' with possible non-object type \'' . $lhs_type_part .
|
|
|
|
'\' cannot treated as an object',
|
2016-12-07 00:27:22 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->var)
|
2016-11-01 16:37:58 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ||
|
|
|
|
($lhs_type_part instanceof TNamedObject &&
|
|
|
|
in_array(
|
|
|
|
strtolower($lhs_type_part->value),
|
|
|
|
['stdclass', 'simplexmlelement', 'dateinterval', 'domdocument', 'domnode']
|
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
)
|
|
|
|
) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($lhs_type_part instanceof TNamedObject && strtolower($lhs_type_part->value) === 'stdclass') {
|
2017-01-02 01:09:17 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
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;
|
2016-11-01 16:37:58 +01:00
|
|
|
$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-01-02 21:31:18 +01:00
|
|
|
if (!ClassChecker::classExists($lhs_type_part->value, $file_checker)) {
|
|
|
|
if (InterfaceChecker::interfaceExists($lhs_type_part->value, $file_checker)) {
|
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-01-15 16:24:38 +01:00
|
|
|
if (MethodChecker::methodExists($lhs_type_part . '::__set')) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = true;
|
|
|
|
|
|
|
|
if (($stmt->var instanceof PhpParser\Node\Expr\Variable && $stmt->var->name === 'this')
|
|
|
|
|| $lhs_type_part->value === $context->self
|
|
|
|
) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
} elseif ($context->self &&
|
|
|
|
ClassChecker::classExtends($lhs_type_part->value, $context->self)
|
|
|
|
) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
} else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
|
|
|
|
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-01-02 01:09:17 +01:00
|
|
|
if (!ClassLikeChecker::propertyExists($property_id)) {
|
2016-12-15 01:19:25 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable && $stmt->var->name === 'this') {
|
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-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(
|
|
|
|
$lhs_type_part->value . '::$' . $prop_name
|
|
|
|
);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$property_storage =
|
|
|
|
ClassLikeChecker::$storage[strtolower((string)$declaring_property_class)]->properties[$stmt->name];
|
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) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
2016-12-29 06:32:12 +01:00
|
|
|
'Property ' . $lhs_type_part->value . '::$' . $prop_name . ' does not have a declared ' .
|
2016-11-02 07:29:00 +01:00
|
|
|
'type',
|
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()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-12-11 00:24:28 +01:00
|
|
|
$class_property_type = ExpressionChecker::fleshOutTypes(
|
|
|
|
clone $class_property_type,
|
|
|
|
[],
|
|
|
|
$lhs_type_part->value
|
|
|
|
);
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_regular_setter) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// because we don't want to be assigning for property declarations
|
2016-12-07 00:27:22 +01:00
|
|
|
$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();
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_property_types) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyDeclaration(
|
|
|
|
'Missing property declaration for ' . $var_id,
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
|
|
|
$file_checker
|
|
|
|
)) {
|
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 . '\'',
|
2016-12-06 22:33:47 +01:00
|
|
|
new CodeLocation(
|
|
|
|
$statements_checker->getSource(),
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_value ?: $stmt
|
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
|
|
|
|
* @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,
|
2016-12-07 00:27:22 +01:00
|
|
|
PhpParser\Node\Expr $assignment_value = null,
|
|
|
|
Type\Union $assignment_value_type,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$class_property_types = [];
|
|
|
|
|
|
|
|
$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
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if (($stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts[0] === 'this') ||
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name === $context->self
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-01 16:37:58 +01:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
2017-01-02 21:31:18 +01:00
|
|
|
} elseif ($context->self &&
|
|
|
|
ClassChecker::classExtends($fq_class_name, $context->self)
|
|
|
|
) {
|
2016-11-01 16:37:58 +01:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-01-02 01:09:17 +01:00
|
|
|
if (!ClassLikeChecker::propertyExists($property_id)) {
|
|
|
|
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(
|
|
|
|
$fq_class_name . '::$' . $prop_name
|
|
|
|
);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$property_storage =
|
|
|
|
ClassLikeChecker::$storage[strtolower((string)$declaring_property_class)]->properties[$stmt->name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
$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) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
2016-11-07 23:29:51 +01:00
|
|
|
'Property ' . $fq_class_name . '::$' . $prop_name . ' does not have a declared type',
|
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()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
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
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$class_property_type = ExpressionChecker::fleshOutTypes($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(
|
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
|
|
|
$statements_checker->getFileChecker()
|
|
|
|
)) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 00:27:22 +01:00
|
|
|
$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
|
|
|
|
* @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
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if ($stmt->dim && ExpressionChecker::analyze($statements_checker, $stmt->dim, $context, false) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$assignment_key_type = null;
|
|
|
|
$assignment_key_value = null;
|
|
|
|
|
|
|
|
if ($stmt->dim) {
|
|
|
|
if (isset($stmt->dim->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$assignment_key_type = $stmt->dim->inferredType;
|
|
|
|
|
|
|
|
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$assignment_key_value = $stmt->dim->value;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
$assignment_key_type = Type::getMixed();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
$assignment_key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
);
|
|
|
|
|
|
|
|
// checks whether or not the thing we're looking at implements ArrayAccess
|
|
|
|
$is_object = $var_id
|
2016-11-02 07:29:00 +01:00
|
|
|
&& isset($context->vars_in_scope[$var_id])
|
|
|
|
&& $context->vars_in_scope[$var_id]->hasObjectType();
|
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,
|
|
|
|
$stmt->var,
|
|
|
|
$context,
|
|
|
|
!$is_object,
|
|
|
|
$assignment_key_type,
|
|
|
|
$assignment_value_type,
|
|
|
|
$assignment_key_value
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$array_var_id = ExpressionChecker::getArrayVarId(
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
$is_string = isset($stmt->var->inferredType)
|
|
|
|
&& $stmt->var->inferredType->hasString();
|
|
|
|
|
|
|
|
$has_scalar = isset($stmt->var->inferredType)
|
|
|
|
&& $stmt->var->inferredType->hasScalarType();
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
$keyed_array_var_id = $array_var_id && $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
2016-11-02 07:29:00 +01:00
|
|
|
? $array_var_id . '[\'' . $stmt->dim->value . '\']'
|
|
|
|
: null;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
$return_type = $stmt->var->inferredType;
|
|
|
|
|
|
|
|
if ($is_object) {
|
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($is_string) {
|
2016-11-01 16:37:58 +01:00
|
|
|
foreach ($assignment_value_type->types as $value_type) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$value_type instanceof TString) {
|
|
|
|
if ($value_type instanceof TMixed) {
|
2016-11-01 16:37:58 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedStringOffsetAssignment(
|
|
|
|
'Cannot assign a mixed variable to a string offset for ' . $var_id,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
|
|
|
'Cannot assign string offset for ' . $var_id . ' of type ' . $value_type,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 05:40:46 +01:00
|
|
|
} elseif ($has_scalar) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
|
|
|
'Cannot assign value on variable ' . $var_id . ' of scalar type ' . $context->vars_in_scope[$var_id],
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
// we want to support multiple array types:
|
|
|
|
// - Dictionaries (which have the type array<string,T>)
|
|
|
|
// - pseudo-objects (which have the type array<string,mixed>)
|
|
|
|
// - typed arrays (which have the type array<int,T>)
|
|
|
|
// and completely freeform arrays
|
|
|
|
//
|
|
|
|
// When making assignments, we generally only know the shape of the array
|
|
|
|
// as it is being created.
|
|
|
|
if ($keyed_array_var_id) {
|
|
|
|
// when we have a pattern like
|
|
|
|
// $a = [];
|
|
|
|
// $a['b']['c']['d'] = 1;
|
|
|
|
// $a['c'] = 2;
|
|
|
|
// we need to create each type in turn
|
|
|
|
// so we get
|
|
|
|
// typeof $a['b']['c']['d'] => int
|
|
|
|
// typeof $a['b']['c'] => array{d:int}
|
|
|
|
// typeof $a['b'] => array{c:array{d:int}}
|
|
|
|
// typeof $a['c'] => int
|
|
|
|
// typeof $a => array{b:array{c:array{d:int}},c:int}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = $assignment_value_type;
|
|
|
|
|
|
|
|
$stmt->inferredType = $assignment_value_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$nesting) {
|
2017-01-15 01:06:58 +01:00
|
|
|
/** @var Type\Atomic\TArray|null */
|
2016-11-01 16:37:58 +01:00
|
|
|
$array_type = isset($context->vars_in_scope[$var_id]->types['array'])
|
2017-01-15 01:06:58 +01:00
|
|
|
&& $context->vars_in_scope[$var_id]->types['array'] instanceof Type\Atomic\TArray
|
2016-11-01 16:37:58 +01:00
|
|
|
? $context->vars_in_scope[$var_id]->types['array']
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($assignment_key_type->hasString()
|
|
|
|
&& $assignment_key_value
|
|
|
|
&& (!isset($context->vars_in_scope[$var_id])
|
|
|
|
|| $context->vars_in_scope[$var_id]->hasObjectLike()
|
|
|
|
|| ($array_type && $array_type->type_params[0]->isEmpty()))
|
|
|
|
) {
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_value_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\ObjectLike([
|
|
|
|
$assignment_key_value => $assignment_value_type
|
|
|
|
])
|
2016-11-01 16:37:58 +01:00
|
|
|
]);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_value_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\TArray([
|
|
|
|
$assignment_key_type,
|
|
|
|
$assignment_value_type
|
|
|
|
])
|
2016-11-01 16:37:58 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
2016-12-07 00:27:22 +01:00
|
|
|
$assignment_value_type
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-12-07 00:27:22 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 16:37:58 +01:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|