2016-11-01 19:14:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\InterfaceChecker;
|
|
|
|
use Psalm\Checker\MethodChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\TraitChecker;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\InvalidArrayAccess;
|
|
|
|
use Psalm\Issue\InvalidArrayAssignment;
|
|
|
|
use Psalm\Issue\InvalidPropertyFetch;
|
2016-12-04 07:44:33 +01:00
|
|
|
use Psalm\Issue\InaccessibleClassConstant;
|
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\MissingPropertyType;
|
2016-11-22 01:07:56 +01:00
|
|
|
use Psalm\Issue\MixedArrayAccess;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\MixedArrayOffset;
|
|
|
|
use Psalm\Issue\MixedPropertyFetch;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
2016-11-22 17:11:47 +01:00
|
|
|
use Psalm\Issue\NullArrayAccess;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\NullPropertyFetch;
|
|
|
|
use Psalm\Issue\NullReference;
|
2016-11-05 02:14:04 +01:00
|
|
|
use Psalm\Issue\ParentNotFound;
|
2017-02-11 23:55:08 +01:00
|
|
|
use Psalm\Issue\PossiblyNullArrayAccess;
|
|
|
|
use Psalm\Issue\PossiblyNullPropertyFetch;
|
|
|
|
use Psalm\Issue\PossiblyNullReference;
|
2016-11-01 19:14:35 +01:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedConstant;
|
|
|
|
use Psalm\Issue\UndefinedPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyFetch;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
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 19:14:35 +01:00
|
|
|
|
|
|
|
class FetchChecker
|
|
|
|
{
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\PropertyFetch $stmt
|
|
|
|
* @param Context $context
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return false|null
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzePropertyFetch(
|
2016-11-01 19:14:35 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
2017-02-12 01:30:06 +01:00
|
|
|
Context $context
|
2016-11-01 19:14:35 +01:00
|
|
|
) {
|
|
|
|
if (!is_string($stmt->name)) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = null;
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt_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-02 07:29:00 +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-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
$stmt_var_type = null;
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($var_id && $context->hasVariable($var_id)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($stmt_var_id && $context->hasVariable($stmt_var_id)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt_var_type = $context->vars_in_scope[$stmt_var_id];
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif (isset($stmt->var->inferredType)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
/** @var Type\Union */
|
|
|
|
$stmt_var_type = $stmt->var->inferredType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmt_var_type) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-22 17:11:47 +01:00
|
|
|
new NullPropertyFetch(
|
2016-11-01 19:14:35 +01:00
|
|
|
'Cannot get property on null variable ' . $stmt_var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isEmpty()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-22 17:11:47 +01:00
|
|
|
new MixedPropertyFetch(
|
2016-11-01 19:14:35 +01:00
|
|
|
'Cannot fetch property on empty var ' . $stmt_var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyFetch(
|
|
|
|
'Cannot fetch property on mixed var ' . $stmt_var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2017-01-28 22:12:04 +01:00
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2017-02-11 23:55:08 +01:00
|
|
|
new PossiblyNullPropertyFetch(
|
2017-01-02 01:09:17 +01:00
|
|
|
'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2017-01-28 22:12:04 +01:00
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_string($stmt->name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt_var_type->types as $lhs_type_part) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($lhs_type_part instanceof TNull) {
|
2016-11-01 19:14:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$lhs_type_part->isObjectType()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyFetch(
|
|
|
|
'Cannot fetch property on non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2017-01-28 22:12:04 +01:00
|
|
|
// fall through
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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'])
|
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = $statements_checker->getFileChecker();
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$lhs_type_part instanceof TNamedObject) {
|
|
|
|
// @todo deal with this
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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 19:14:35 +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 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot get 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 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-30 05:44:05 +01:00
|
|
|
if (MethodChecker::methodExists($lhs_type_part->value . '::__get', $file_checker)) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $stmt->name;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
if (!ClassLikeChecker::propertyExists($property_id)) {
|
|
|
|
if ($stmt_var_id === '$this') {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyFetch(
|
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 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyFetch(
|
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 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty($property_id);
|
|
|
|
|
2017-02-10 06:14:44 +01:00
|
|
|
$declaring_class_storage = ClassLikeChecker::$storage[strtolower((string)$declaring_property_class)];
|
|
|
|
|
|
|
|
$property_storage = $declaring_class_storage->properties[$stmt->name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$class_property_type = $property_storage->type;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if ($class_property_type === false) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
|
|
|
'Property ' . $lhs_type_part->value . '::$' . $stmt->name . ' does not have a declared type',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$class_property_type = clone $class_property_type;
|
2017-02-10 06:14:44 +01:00
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
|
|
|
$class_storage = ClassLikeChecker::$storage[strtolower($lhs_type_part->value)];
|
|
|
|
|
|
|
|
if ($class_storage->template_types) {
|
|
|
|
$class_template_params = [];
|
|
|
|
|
|
|
|
/** @var array<int, string> */
|
|
|
|
$reversed_class_template_types = array_reverse(array_keys($class_storage->template_types));
|
|
|
|
|
|
|
|
$provided_type_param_count = count($lhs_type_part->type_params);
|
|
|
|
|
|
|
|
foreach ($reversed_class_template_types as $i => $type_name) {
|
|
|
|
if (isset($lhs_type_part->type_params[$provided_type_param_count - 1 - $i])) {
|
|
|
|
$class_template_params[$type_name] =
|
|
|
|
(string)$lhs_type_part->type_params[$provided_type_param_count - 1 - $i];
|
|
|
|
} else {
|
|
|
|
$class_template_params[$type_name] = 'mixed';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_tokens = Type::tokenize((string)$class_property_type);
|
|
|
|
|
|
|
|
foreach ($type_tokens as &$type_token) {
|
|
|
|
if (isset($class_template_params[$type_token])) {
|
|
|
|
$type_token = $class_template_params[$type_token];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::parseString(implode('', $type_tokens));
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($class_property_type, $stmt->inferredType);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $class_property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = isset($stmt->inferredType) ? $stmt->inferredType : Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\ConstFetch $stmt
|
|
|
|
* @param Context $context
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return false|null
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeConstFetch(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\ConstFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$const_name = implode('\\', $stmt->name->parts);
|
2016-11-01 19:14:35 +01:00
|
|
|
switch (strtolower($const_name)) {
|
|
|
|
case 'null':
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'false':
|
|
|
|
// false is a subtype of bool
|
|
|
|
$stmt->inferredType = Type::getFalse();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'true':
|
|
|
|
$stmt->inferredType = Type::getBool();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-11-21 03:49:06 +01:00
|
|
|
$const_type = $statements_checker->getConstType(
|
|
|
|
$const_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
$stmt->name instanceof PhpParser\Node\Name\FullyQualified,
|
|
|
|
$context
|
2016-11-21 03:49:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($const_type) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = clone $const_type;
|
2016-11-21 03:49:06 +01:00
|
|
|
} elseif ($context->check_consts) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new UndefinedConstant(
|
|
|
|
'Const ' . $const_name . ' is not defined',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-11-01 19:14:35 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\ClassConstFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return null|false
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeClassConstFetch(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\ClassConstFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if ($context->check_consts &&
|
|
|
|
$stmt->class instanceof PhpParser\Node\Name &&
|
2017-02-04 02:14:48 +01:00
|
|
|
$stmt->class->parts !== ['static'] &&
|
|
|
|
is_string($stmt->name)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($stmt->class->parts === ['self']) {
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!$context->self) {
|
|
|
|
throw new \UnexpectedValueException('$context->self cannot be null');
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = (string)$context->self;
|
2016-12-08 23:19:06 +01:00
|
|
|
} elseif ($stmt->class->parts[0] === 'parent') {
|
2017-01-07 20:35:07 +01:00
|
|
|
$fq_class_name = $statements_checker->getParentFQCLN();
|
2016-12-08 23:19:06 +01:00
|
|
|
|
|
|
|
if ($fq_class_name === null) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ParentNotFound(
|
|
|
|
'Cannot check property fetch on parent as this class does not extend another',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-08 01:16:51 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->class,
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
|
|
|
|
2017-01-07 23:24:43 +01:00
|
|
|
// edge case when evaluating single files
|
|
|
|
if ($stmt->name === 'class' &&
|
|
|
|
$statements_checker->getFileChecker()->containsUnEvaluatedClassLike($fq_class_name)
|
|
|
|
) {
|
|
|
|
$stmt->inferredType = Type::getString();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-08 01:16:51 +01:00
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker->getFileChecker(),
|
2016-12-08 23:15:51 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2017-02-12 01:30:06 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->name === 'class') {
|
|
|
|
$stmt->inferredType = Type::getString();
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 01:21:33 +01:00
|
|
|
// if we're ignoring that the class doesn't exist, exit anyway
|
|
|
|
if (!ClassLikeChecker::classOrInterfaceExists($fq_class_name, $statements_checker->getFileChecker())) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:43 +01:00
|
|
|
$const_id = $fq_class_name . '::' . $stmt->name;
|
|
|
|
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($fq_class_name === $context->self
|
|
|
|
|| (
|
|
|
|
$statements_checker->getSource()->getSource() instanceof TraitChecker &&
|
|
|
|
$fq_class_name === $statements_checker->getSource()->getFQCLN()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
2017-01-02 21:31:18 +01:00
|
|
|
} elseif ($context->self &&
|
|
|
|
ClassChecker::classExtends($context->self, $fq_class_name)
|
|
|
|
) {
|
2016-12-04 07:44:33 +01:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
} else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_constants = ClassLikeChecker::getConstantsForClass($fq_class_name, $class_visibility);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if (!isset($class_constants[$stmt->name])) {
|
2016-12-04 07:44:33 +01:00
|
|
|
$all_class_constants = [];
|
|
|
|
|
|
|
|
if ($fq_class_name !== $context->self) {
|
|
|
|
$all_class_constants = ClassLikeChecker::getConstantsForClass(
|
|
|
|
$fq_class_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($all_class_constants && isset($all_class_constants[$stmt->name])) {
|
|
|
|
IssueBuffer::add(
|
|
|
|
new InaccessibleClassConstant(
|
|
|
|
'Constant ' . $const_id . ' is not visible in this context',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
IssueBuffer::add(
|
|
|
|
new UndefinedConstant(
|
|
|
|
'Constant ' . $const_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2016-12-04 07:44:33 +01:00
|
|
|
|
|
|
|
return false;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $class_constants[$stmt->name];
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->class, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return null|false
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeStaticPropertyFetch(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\StaticPropertyFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable ||
|
|
|
|
$stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// @todo check this
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
|
|
|
if ($stmt->class->parts[0] === 'parent') {
|
2017-01-07 20:35:07 +01:00
|
|
|
$fq_class_name = $statements_checker->getParentFQCLN();
|
2016-11-05 02:14:04 +01:00
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name === null) {
|
2016-11-05 02:14:04 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ParentNotFound(
|
|
|
|
'Cannot check property fetch on parent as this class does not extend another',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-05 02:14:04 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
} else {
|
2017-02-11 02:41:18 +01:00
|
|
|
$fq_class_name = (string)$context->self;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-18 05:34:17 +01:00
|
|
|
} else {
|
2016-11-08 01:16:51 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->class,
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 05:34:17 +01:00
|
|
|
if ($context->check_classes) {
|
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
|
|
|
$fq_class_name,
|
|
|
|
$statements_checker->getFileChecker(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2017-02-12 01:30:06 +01:00
|
|
|
$statements_checker->getSuppressedIssues()
|
2017-01-18 05:34:17 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$stmt->class->inferredType = $fq_class_name ? new Type\Union([new TNamedObject($fq_class_name)]) : null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
if ($fq_class_name &&
|
2017-01-18 05:34:17 +01:00
|
|
|
$context->check_classes &&
|
2016-11-02 07:29:00 +01:00
|
|
|
$context->check_variables &&
|
|
|
|
is_string($stmt->name) &&
|
2016-11-07 23:29:51 +01:00
|
|
|
!ExpressionChecker::isMock($fq_class_name)
|
2016-11-02 07:29:00 +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-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($var_id && $context->hasVariable($var_id)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $stmt->name;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
if (!ClassLikeChecker::propertyExists($property_id)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyFetch(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
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) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty(
|
|
|
|
$fq_class_name . '::$' . $stmt->name
|
|
|
|
);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$property =
|
|
|
|
ClassLikeChecker::$storage[strtolower((string)$declaring_property_class)]->properties[$stmt->name];
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $property->type
|
|
|
|
? clone $property->type
|
2016-11-02 07:29:00 +01:00
|
|
|
: Type::getMixed();
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$var_id];
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\ArrayDimFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param bool $array_assignment
|
|
|
|
* @param Type\Union|null $assignment_key_type
|
|
|
|
* @param Type\Union|null $assignment_value_type
|
|
|
|
* @param string|null $assignment_key_value
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return false|null
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeArrayAccess(
|
2016-11-01 19:14:35 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\ArrayDimFetch $stmt,
|
|
|
|
Context $context,
|
|
|
|
$array_assignment = false,
|
|
|
|
Type\Union $assignment_key_type = null,
|
|
|
|
Type\Union $assignment_value_type = null,
|
|
|
|
$assignment_key_value = null
|
|
|
|
) {
|
|
|
|
$var_type = null;
|
|
|
|
$key_type = null;
|
2017-01-25 08:11:24 +01:00
|
|
|
$string_key_value = null;
|
|
|
|
$int_key_value = null;
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
$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 19:14:35 +01:00
|
|
|
$nesting
|
|
|
|
);
|
|
|
|
|
|
|
|
// checks whether or not the thing we're looking at implements ArrayAccess
|
|
|
|
$is_object = $var_id
|
2017-02-01 05:24:33 +01:00
|
|
|
&& $context->hasVariable($var_id)
|
2016-11-02 07:29:00 +01:00
|
|
|
&& $context->vars_in_scope[$var_id]->hasObjectType();
|
|
|
|
|
|
|
|
$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-02 07:29:00 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-12-11 00:24:28 +01:00
|
|
|
$keyed_array_var_id = ExpressionChecker::getArrayVarId(
|
|
|
|
$stmt,
|
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-12-11 00:24:28 +01:00
|
|
|
);
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if ($stmt->dim && ExpressionChecker::analyze($statements_checker, $stmt->dim, $context) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->dim) {
|
|
|
|
if (isset($stmt->dim->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$key_type = $stmt->dim->inferredType;
|
|
|
|
|
|
|
|
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
2017-01-25 08:11:24 +01:00
|
|
|
$string_key_value = $stmt->dim->value;
|
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
$int_key_value = $stmt->dim->value;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$key_type = Type::getMixed();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$keyed_assignment_type = null;
|
|
|
|
|
|
|
|
if ($array_assignment && $assignment_key_type && $assignment_value_type) {
|
|
|
|
$keyed_assignment_type =
|
2017-02-01 05:24:33 +01:00
|
|
|
$keyed_array_var_id && $context->hasVariable($keyed_array_var_id)
|
2016-11-01 19:14:35 +01:00
|
|
|
? $context->vars_in_scope[$keyed_array_var_id]
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!$keyed_assignment_type || $keyed_assignment_type->isEmpty()) {
|
|
|
|
if (!$assignment_key_type->isMixed() && !$assignment_key_type->hasInt() && $assignment_key_value) {
|
|
|
|
$keyed_assignment_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 19:14:35 +01:00
|
|
|
]);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$keyed_assignment_type = Type::getEmptyArray();
|
2017-01-15 01:06:58 +01:00
|
|
|
/** @var Type\Atomic\TArray */
|
2016-11-01 19:14:35 +01:00
|
|
|
$keyed_assignment_type_array = $keyed_assignment_type->types['array'];
|
|
|
|
$keyed_assignment_type_array->type_params[0] = $assignment_key_type;
|
|
|
|
$keyed_assignment_type_array->type_params[1] = $assignment_value_type;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($keyed_assignment_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
|
|
|
'Cannot assign value on null array' . ($var_id ? ' ' . $var_id : ''),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($keyed_assignment_type->types as &$type) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof TInt || $type instanceof TFloat || $type instanceof TBool) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
2017-01-15 01:06:58 +01:00
|
|
|
'Cannot assign value on variable ' . $var_id . ' of scalar type ' . $type->getKey(),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof TString || $type instanceof Type\Atomic\TArray) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$refined_type = self::refineArrayType(
|
|
|
|
$statements_checker,
|
|
|
|
$type,
|
|
|
|
$assignment_key_type,
|
|
|
|
$assignment_value_type,
|
|
|
|
$var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($refined_type === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($refined_type === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $refined_type;
|
2017-01-15 01:06:58 +01:00
|
|
|
} elseif ($type instanceof Type\Atomic\ObjectLike && $assignment_key_value) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (isset($type->properties[$assignment_key_value])) {
|
|
|
|
$type->properties[$assignment_key_value] = Type::combineUnionTypes(
|
|
|
|
$type->properties[$assignment_key_value],
|
|
|
|
$assignment_value_type
|
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$type->properties[$assignment_key_value] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->var,
|
|
|
|
$context,
|
|
|
|
$array_assignment,
|
|
|
|
$key_type,
|
|
|
|
$keyed_assignment_type,
|
2017-01-25 08:11:24 +01:00
|
|
|
$string_key_value
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-11-01 19:14:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
/** @var Type\Union|null */
|
2016-12-15 07:28:36 +01:00
|
|
|
$inferred_key_type = null;
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$var_type = $stmt->var->inferredType;
|
|
|
|
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($var_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullArrayAccess(
|
|
|
|
'Cannot access array value on null variable ' . $array_var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull());
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($var_type->types as &$type) {
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($type instanceof TNull) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullArrayAccess(
|
|
|
|
'Cannot access array value on possibly null variable ' . $array_var_id .
|
|
|
|
' of type ' . $var_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull());
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof Type\Atomic\TArray || $type instanceof Type\Atomic\ObjectLike) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$value_index = null;
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof Type\Atomic\TArray) {
|
2016-11-01 19:14:35 +01:00
|
|
|
// create a union type to pass back to the statement
|
|
|
|
$value_index = count($type->type_params) - 1;
|
|
|
|
|
|
|
|
if ($value_index) {
|
|
|
|
// if we're assigning to an empty array with a key offset, refashion that array
|
|
|
|
if ($array_assignment && $type->type_params[0]->isEmpty()) {
|
|
|
|
if ($key_type) {
|
|
|
|
$type->type_params[0] = $key_type;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($key_type) {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $type->type_params[0]);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$key_type = $type->type_params[0];
|
|
|
|
}
|
2016-12-15 07:28:36 +01:00
|
|
|
|
|
|
|
if ($inferred_key_type) {
|
2017-01-25 08:11:24 +01:00
|
|
|
$inferred_key_type = Type::combineUnionTypes(
|
|
|
|
$inferred_key_type,
|
|
|
|
$type->type_params[0]
|
|
|
|
);
|
2016-12-15 07:28:36 +01:00
|
|
|
} else {
|
|
|
|
$inferred_key_type = $type->type_params[0];
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($array_assignment && !$is_object) {
|
|
|
|
// if we're in an array assignment then we need to create some variables
|
|
|
|
// e.g.
|
|
|
|
// $a = [];
|
|
|
|
// $a['b']['c']['d'] = 3;
|
|
|
|
//
|
|
|
|
// means we need add $a['b'], $a['b']['c'] to the current context
|
|
|
|
// (but not $a['b']['c']['d'], which is handled in checkArrayAssignment)
|
2016-12-05 01:33:14 +01:00
|
|
|
if ($keyed_assignment_type) {
|
|
|
|
if ($keyed_array_var_id) {
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($context->hasVariable($keyed_array_var_id)) {
|
2016-12-05 01:33:14 +01:00
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = Type::combineUnionTypes(
|
|
|
|
$keyed_assignment_type,
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = $keyed_assignment_type;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $keyed_assignment_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($array_var_id === $var_id) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof Type\Atomic\ObjectLike ||
|
|
|
|
(
|
|
|
|
$type instanceof TArray &&
|
|
|
|
!$key_type->hasInt() &&
|
|
|
|
$type->type_params[1]->isEmpty()
|
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2017-02-12 00:25:44 +01:00
|
|
|
$properties = $keyed_assignment_type && $string_key_value
|
|
|
|
? [$string_key_value => $keyed_assignment_type]
|
|
|
|
: [];
|
2016-11-01 19:14:35 +01:00
|
|
|
|
|
|
|
$assignment_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\ObjectLike($properties)
|
2016-11-01 19:14:35 +01:00
|
|
|
]);
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-12-24 19:23:22 +01:00
|
|
|
if (!$keyed_assignment_type) {
|
|
|
|
throw new \UnexpectedValueException('$keyed_assignment_type cannot be null');
|
|
|
|
}
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
$assignment_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\TArray([
|
|
|
|
$key_type,
|
|
|
|
$keyed_assignment_type
|
|
|
|
])
|
2016-11-01 19:14:35 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($context->hasVariable($var_id)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$assignment_type
|
|
|
|
);
|
2017-02-01 05:24:33 +01:00
|
|
|
} elseif ($var_id) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof Type\Atomic\TArray &&
|
|
|
|
$type->type_params[$value_index]->isEmpty()
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$empty_type = Type::getEmptyArray();
|
|
|
|
|
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
// if in array assignment and the referenced variable does not have
|
|
|
|
// an array at this level, create one
|
|
|
|
$stmt->inferredType = $empty_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context_type = clone $context->vars_in_scope[$var_id];
|
|
|
|
|
|
|
|
$array_type = $context_type;
|
|
|
|
|
|
|
|
for ($i = 0; $i < $nesting + 1; $i++) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if (isset($array_type->types['array']) &&
|
2017-01-15 01:06:58 +01:00
|
|
|
$array_type->types['array'] instanceof Type\Atomic\TArray
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$atomic_array = $array_type->types['array'];
|
|
|
|
|
|
|
|
if ($i < $nesting) {
|
|
|
|
if ($atomic_array->type_params[1]->isEmpty()) {
|
|
|
|
$new_empty = clone $empty_type;
|
2017-01-15 01:06:58 +01:00
|
|
|
/** @var Type\Atomic\TArray */
|
2016-11-01 19:14:35 +01:00
|
|
|
$new_atomic_empty = $new_empty->types['array'];
|
|
|
|
$new_atomic_empty->type_params[0] = $key_type;
|
|
|
|
|
|
|
|
$atomic_array->type_params[1] = $new_empty;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$array_type = $atomic_array->type_params[1];
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$atomic_array->type_params[0] = $key_type;
|
2016-11-06 01:17:22 +01:00
|
|
|
|
|
|
|
if ($nesting === 0 && $keyed_assignment_type) {
|
|
|
|
$atomic_array->type_params[1] = $keyed_assignment_type;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $context_type;
|
|
|
|
}
|
2017-01-15 01:06:58 +01:00
|
|
|
} elseif ($type instanceof Type\Atomic\TArray && $value_index !== null) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $type->type_params[$value_index];
|
2017-01-15 01:06:58 +01:00
|
|
|
} elseif ($type instanceof Type\Atomic\ObjectLike) {
|
2017-01-25 08:11:24 +01:00
|
|
|
$object_like_keys = array_keys($type->properties);
|
|
|
|
if ($object_like_keys) {
|
|
|
|
if (count($object_like_keys) === 1) {
|
|
|
|
$expected_keys_string = '\'' . $object_like_keys[0] . '\'';
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-25 08:11:24 +01:00
|
|
|
$last_key = array_pop($object_like_keys);
|
|
|
|
$expected_keys_string = '\'' . implode('\', \'', $object_like_keys) .
|
|
|
|
'\' or \'' . $last_key . '\'';
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2017-01-25 08:11:24 +01:00
|
|
|
} else {
|
|
|
|
$expected_keys_string = 'string';
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2017-01-25 08:11:24 +01:00
|
|
|
if ($string_key_value && isset($type->properties[$string_key_value])) {
|
|
|
|
$stmt->inferredType = clone $type->properties[$string_key_value];
|
|
|
|
} elseif ($int_key_value !== null && isset($type->properties[$int_key_value])) {
|
|
|
|
$stmt->inferredType = clone $type->properties[$int_key_value];
|
|
|
|
} elseif ($key_type->hasInt()) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAccess(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Cannot access value on array variable ' . $var_id . ' using int offset - ' .
|
|
|
|
'expecting ' . $expected_keys_string,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 23:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof TString) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($key_type) {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, Type::getInt());
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-01 19:14:35 +01:00
|
|
|
$key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
2016-12-15 07:28:36 +01:00
|
|
|
if (!$inferred_key_type) {
|
|
|
|
$inferred_key_type = Type::getInt();
|
|
|
|
} else {
|
|
|
|
$inferred_key_type = Type::combineUnionTypes($inferred_key_type, Type::getInt());
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = Type::getString();
|
2017-02-11 23:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof TMixed || $type instanceof TEmpty) {
|
2016-11-22 01:07:56 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedArrayAccess(
|
|
|
|
'Cannot access array value on mixed variable ' . $array_var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-22 01:07:56 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
break;
|
|
|
|
}
|
2017-02-11 23:55:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type instanceof TNamedObject ||
|
2017-01-15 01:06:58 +01:00
|
|
|
(strtolower($type->value) !== 'simplexmlelement' &&
|
2017-01-20 00:59:14 +01:00
|
|
|
ClassChecker::classExists($type->value, $statements_checker->getFileChecker()) &&
|
2017-01-15 01:06:58 +01:00
|
|
|
!ClassChecker::classImplements($type->value, 'ArrayAccess')
|
|
|
|
)
|
2016-11-22 01:07:56 +01:00
|
|
|
) {
|
2016-11-21 22:44:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAccess(
|
2017-01-15 01:06:58 +01:00
|
|
|
'Cannot access array value on non-array variable ' .
|
|
|
|
$array_var_id . ' of type ' . $var_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-21 22:44:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2016-11-22 00:38:56 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-11-22 01:07:56 +01:00
|
|
|
break;
|
2016-11-21 22:44:35 +01:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($keyed_array_var_id && $context->hasVariable($keyed_array_var_id)) {
|
2016-11-01 19:14:35 +01:00
|
|
|
$stmt->inferredType = $context->vars_in_scope[$keyed_array_var_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new TInt,
|
|
|
|
new TString
|
2016-11-01 19:14:35 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->dim) {
|
|
|
|
if (isset($stmt->dim->inferredType) && $key_type && !$key_type->isEmpty()) {
|
|
|
|
foreach ($stmt->dim->inferredType->types as $at) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (($at instanceof TMixed || $at instanceof TEmpty) &&
|
2016-12-17 00:56:23 +01:00
|
|
|
$inferred_key_type &&
|
|
|
|
!$inferred_key_type->isMixed() &&
|
|
|
|
!$inferred_key_type->isEmpty()
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedArrayOffset(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Cannot access value on variable ' . $var_id . ' using mixed offset - expecting ' .
|
2016-12-17 00:56:23 +01:00
|
|
|
$inferred_key_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
} elseif (!$at->isIn($key_type, $statements_checker->getFileChecker())) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAccess(
|
2016-11-02 07:29:00 +01:00
|
|
|
'Cannot access value on variable ' . $var_id . ' using ' . $at . ' offset - ' .
|
|
|
|
'expecting ' . $key_type,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param Type\Atomic $type
|
|
|
|
* @param Type\Union $assignment_key_type
|
|
|
|
* @param Type\Union $assignment_value_type
|
2016-11-05 02:14:04 +01:00
|
|
|
* @param string|null $var_id
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return Type\Atomic|null|false
|
2016-11-01 19:14:35 +01:00
|
|
|
*/
|
|
|
|
protected static function refineArrayType(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
Type\Atomic $type,
|
|
|
|
Type\Union $assignment_key_type,
|
|
|
|
Type\Union $assignment_value_type,
|
|
|
|
$var_id,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location
|
2016-11-01 19:14:35 +01:00
|
|
|
) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof TNull) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2017-02-11 23:55:08 +01:00
|
|
|
new PossiblyNullReference(
|
2016-11-05 02:14:04 +01:00
|
|
|
'Cannot assign value on possibly null array' . ($var_id ? ' ' . $var_id : ''),
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof TString && $assignment_value_type->hasString() && !$assignment_key_type->hasString()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$type instanceof TArray &&
|
|
|
|
(!$type instanceof TNamedObject || !ClassChecker::classImplements($type->value, 'ArrayAccess'))
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
2017-01-15 01:06:58 +01:00
|
|
|
'Cannot assign value on variable' . ($var_id ? ' ' . $var_id : '') . ' of type ' . $type . ' that does not ' .
|
2016-11-02 07:29:00 +01:00
|
|
|
'implement ArrayAccess',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($type instanceof Type\Atomic\Generic && $type instanceof TArray) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($type->type_params[1]->isEmpty()) {
|
|
|
|
$type->type_params[0] = $assignment_key_type;
|
|
|
|
$type->type_params[1] = $assignment_value_type;
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) $type->type_params[0] !== (string) $assignment_key_type) {
|
|
|
|
$type->type_params[0] = Type::combineUnionTypes($type->type_params[0], $assignment_key_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) $type->type_params[1] !== (string) $assignment_value_type) {
|
|
|
|
$type->type_params[1] = Type::combineUnionTypes($type->type_params[1], $assignment_value_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|