1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 03:17:02 +01:00
psalm/src/Psalm/Checker/Statements/Expression/FetchChecker.php

1133 lines
45 KiB
PHP
Raw Normal View History

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;
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;
use Psalm\Issue\InaccessibleClassConstant;
use Psalm\Issue\InaccessibleProperty;
2016-11-01 19:14:35 +01:00
use Psalm\Issue\MissingPropertyType;
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;
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;
class FetchChecker
{
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\PropertyFetch $stmt
* @param Context $context
* @param bool $array_assignment
2016-11-05 02:14:04 +01:00
* @return false|null
2016-11-01 19:14:35 +01:00
*/
public static function checkPropertyFetch(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\PropertyFetch $stmt,
Context $context,
$array_assignment = false
) {
if (!is_string($stmt->name)) {
if (ExpressionChecker::check($statements_checker, $stmt->name, $context) === false) {
return false;
}
}
$var_id = null;
if (ExpressionChecker::check($statements_checker, $stmt->var, $context) === false) {
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(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
$var_id = ExpressionChecker::getVarId(
$stmt,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +01:00
$var_name = is_string($stmt->name) ? $stmt->name : null;
$stmt_var_type = null;
if ($var_id && isset($context->vars_in_scope[$var_id])) {
// 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
}
if ($stmt_var_id && isset($context->vars_in_scope[$stmt_var_id])) {
$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,
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,
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,
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->isNullable()) {
if (IssueBuffer::accepts(
new NullPropertyFetch(
'Cannot get property on possibly null variable ' . $stmt_var_id,
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
$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) {
if ($lhs_type_part->isNull()) {
continue;
}
if (!$lhs_type_part->isObjectType()) {
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(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +01:00
if (IssueBuffer::accepts(
new InvalidPropertyFetch(
'Cannot fetch property on non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-01 19:14:35 +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
2016-11-02 07:29:00 +01:00
if ($lhs_type_part->isObject() ||
in_array(strtolower($lhs_type_part->value), ['stdclass', 'simplexmlelement'])
) {
2016-11-01 19:14:35 +01:00
$stmt->inferredType = Type::getMixed();
continue;
}
if (MethodChecker::methodExists($lhs_type_part . '::__get')) {
$stmt->inferredType = Type::getMixed();
continue;
}
if (!ClassChecker::classExists($lhs_type_part->value)) {
if (InterfaceChecker::interfaceExists($lhs_type_part->value)) {
if (IssueBuffer::accepts(
new NoInterfaceProperties(
'Interfaces cannot have properties',
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,
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
continue;
}
if ($var_name === 'this'
|| $lhs_type_part->value === $context->self
2016-11-02 07:29:00 +01:00
|| (
$statements_checker->getSource()->getSource() instanceof TraitChecker &&
2016-11-08 01:16:51 +01:00
$lhs_type_part->value === $statements_checker->getSource()->getFQCLN()
2016-11-02 07:29:00 +01:00
)
2016-11-01 19:14:35 +01:00
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
2016-11-02 07:29:00 +01:00
} elseif ($context->self && ClassChecker::classExtends($lhs_type_part->value, $context->self)) {
2016-11-01 19:14:35 +01:00
$class_visibility = \ReflectionProperty::IS_PROTECTED;
2016-11-02 07:29:00 +01:00
} else {
2016-11-01 19:14:35 +01:00
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
$class_properties = ClassLikeChecker::getInstancePropertiesForClass(
$lhs_type_part->value,
$class_visibility
);
if (!$class_properties || !isset($class_properties[$stmt->name])) {
$stmt->inferredType = Type::getMixed();
$all_class_properties = ClassLikeChecker::getInstancePropertiesForClass(
$lhs_type_part->value,
\ReflectionProperty::IS_PRIVATE
);
2016-11-01 19:14:35 +01:00
if ($var_id) {
$context->vars_in_scope[$var_id] = Type::getMixed();
}
if ($all_class_properties && isset($all_class_properties[$stmt->name])) {
IssueBuffer::add(
new InaccessibleProperty(
'Property ' . $var_id . ' is not visible in this context',
new CodeLocation($statements_checker->getSource(), $stmt)
)
);
} elseif ($stmt_var_id === '$this') {
2016-11-01 19:14:35 +01:00
if (IssueBuffer::accepts(
new UndefinedThisPropertyFetch(
'Instance property ' . $lhs_type_part->value .'::$' . $stmt->name . ' is not defined',
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(
'Instance property ' . $lhs_type_part->value .'::$' . $stmt->name . ' is not defined',
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
}
$class_property_type = $class_properties[$stmt->name];
if ($class_property_type === false) {
if (IssueBuffer::accepts(
new MissingPropertyType(
'Property ' . $lhs_type_part->value . '::$' . $stmt->name . ' does not have a declared type',
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;
}
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
*/
2016-11-02 07:29:00 +01:00
public static function checkConstFetch(
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,
$stmt->name instanceof PhpParser\Node\Name\FullyQualified
);
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',
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
*/
2016-11-02 07:29:00 +01:00
public static function checkClassConstFetch(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\ClassConstFetch $stmt,
Context $context
) {
if ($context->check_consts &&
$stmt->class instanceof PhpParser\Node\Name &&
$stmt->class->parts !== ['static']
) {
2016-11-01 19:14:35 +01:00
if ($stmt->class->parts === ['self']) {
$fq_class_name = (string)$context->self;
2016-12-08 23:19:06 +01:00
} elseif ($stmt->class->parts[0] === 'parent') {
$fq_class_name = $statements_checker->getParentClass();
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,
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-08 01:16:51 +01:00
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
$fq_class_name,
2016-12-08 23:15:51 +01:00
new CodeLocation($statements_checker->getSource(), $stmt->class),
2016-11-02 07:29:00 +01:00
$statements_checker->getSuppressedIssues()
) === false) {
2016-11-01 19:14:35 +01:00
return false;
}
}
$const_id = $fq_class_name . '::' . $stmt->name;
2016-11-01 19:14:35 +01:00
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
}
if ($fq_class_name === $context->self
|| (
$statements_checker->getSource()->getSource() instanceof TraitChecker &&
$fq_class_name === $statements_checker->getSource()->getFQCLN()
)
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self && ClassChecker::classExtends($context->self, $fq_class_name)) {
$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])) {
$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
}
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) {
if (ExpressionChecker::check($statements_checker, $stmt->class, $context) === false) {
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
*/
2016-11-02 07:29:00 +01:00
public static function checkStaticPropertyFetch(
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
}
$method_id = null;
$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') {
$fq_class_name = $statements_checker->getParentClass();
2016-11-05 02:14:04 +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',
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 {
$fq_class_name = ($statements_checker->getNamespace()
2016-11-02 07:29:00 +01:00
? $statements_checker->getNamespace() . '\\'
: '') . $statements_checker->getClassName();
2016-11-01 19:14:35 +01:00
}
if ($context->isPhantomClass($fq_class_name)) {
2016-11-01 19:14:35 +01:00
return null;
}
2016-11-02 07:29:00 +01:00
} elseif ($context->check_classes) {
2016-11-08 01:16:51 +01:00
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
2016-11-02 07:29:00 +01:00
$stmt->class,
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +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
}
2016-11-08 01:16:51 +01:00
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
$fq_class_name,
2016-12-08 23:15:51 +01:00
new CodeLocation($statements_checker->getSource(), $stmt->class),
2016-11-02 07:29:00 +01:00
$statements_checker->getSuppressedIssues()
) === false) {
2016-11-01 19:14:35 +01:00
return false;
}
}
$stmt->class->inferredType = $fq_class_name ? new Type\Union([new Type\Atomic($fq_class_name)]) : null;
2016-11-01 19:14:35 +01:00
}
if ($fq_class_name &&
2016-11-02 07:29:00 +01:00
$context->check_variables &&
is_string($stmt->name) &&
!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(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +01:00
if ($var_id && isset($context->vars_in_scope[$var_id])) {
// 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
}
if ($fq_class_name === $context->self
2016-11-02 07:29:00 +01:00
|| (
$statements_checker->getSource()->getSource() instanceof TraitChecker &&
2016-11-08 01:16:51 +01:00
$fq_class_name === $statements_checker->getSource()->getFQCLN()
2016-11-02 07:29:00 +01:00
)
2016-11-01 19:14:35 +01:00
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self && ClassChecker::classExtends($context->self, $fq_class_name)) {
2016-11-01 19:14:35 +01:00
$class_visibility = \ReflectionProperty::IS_PROTECTED;
2016-11-02 07:29:00 +01:00
} else {
2016-11-01 19:14:35 +01:00
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}
$visible_class_properties = ClassLikeChecker::getStaticPropertiesForClass(
$fq_class_name,
2016-11-01 19:14:35 +01:00
$class_visibility
);
if (!isset($visible_class_properties[$stmt->name])) {
$all_class_properties = [];
if ($fq_class_name !== $context->self) {
2016-11-01 19:14:35 +01:00
$all_class_properties = ClassLikeChecker::getStaticPropertiesForClass(
$fq_class_name,
2016-11-01 19:14:35 +01:00
\ReflectionProperty::IS_PRIVATE
);
}
if ($all_class_properties && isset($all_class_properties[$stmt->name])) {
IssueBuffer::add(
new InaccessibleProperty(
2016-11-02 07:29:00 +01:00
'Static property ' . $var_id . ' is not visible in this context',
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-02 07:29:00 +01:00
)
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
IssueBuffer::add(
2016-11-02 07:29:00 +01:00
new UndefinedPropertyFetch(
'Static property ' . $var_id . ' does not exist',
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-02 07:29:00 +01:00
)
2016-11-01 19:14:35 +01:00
);
}
return false;
}
$visible_class_property = $visible_class_properties[$stmt->name];
2016-11-02 07:29:00 +01:00
$context->vars_in_scope[$var_id] = $visible_class_property
? clone $visible_class_property
: 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
*/
public static function checkArrayAccess(
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;
$key_value = null;
$nesting = 0;
$var_id = ExpressionChecker::getVarId(
$stmt->var,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-11-01 19:14:35 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses(),
$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();
$array_var_id = ExpressionChecker::getArrayVarId(
$stmt->var,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +01:00
$keyed_array_var_id = ExpressionChecker::getArrayVarId(
$stmt,
$statements_checker->getFQCLN(),
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-01 19:14:35 +01:00
if ($stmt->dim && ExpressionChecker::check($statements_checker, $stmt->dim, $context) === false) {
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_) {
$key_value = $stmt->dim->value;
}
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 =
$keyed_array_var_id && isset($context->vars_in_scope[$keyed_array_var_id])
? $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([
new Type\ObjectLike(
'array',
[
$assignment_key_value => $assignment_value_type
]
)
]);
2016-11-02 07:29:00 +01:00
} else {
2016-11-01 19:14:35 +01:00
$keyed_assignment_type = Type::getEmptyArray();
/** @var Type\Generic */
$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 {
2016-11-01 19:14:35 +01:00
foreach ($keyed_assignment_type->types as &$type) {
if ($type->isScalarType() && !$type->isString()) {
if (IssueBuffer::accepts(
new InvalidArrayAssignment(
'Cannot assign value on variable ' . $var_id . ' of scalar type ' . $type->value,
new CodeLocation($statements_checker->getSource(), $stmt)
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
continue;
}
if ($type instanceof Type\Generic) {
$refined_type = self::refineArrayType(
$statements_checker,
$type,
$assignment_key_type,
$assignment_value_type,
$var_id,
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;
2016-11-02 07:29:00 +01:00
} elseif ($type instanceof Type\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;
}
}
}
}
}
2016-11-02 07:29:00 +01:00
if (ExpressionChecker::check(
$statements_checker,
$stmt->var,
$context,
$array_assignment,
$key_type,
$keyed_assignment_type,
$key_value
) === false) {
2016-11-01 19:14:35 +01:00
return false;
}
/** @var Type\Union|null */
$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;
foreach ($var_type->types as &$type) {
if ($type instanceof Type\Generic || $type instanceof Type\ObjectLike) {
$value_index = null;
if ($type instanceof Type\Generic) {
// 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];
}
if ($inferred_key_type) {
Type::combineUnionTypes($key_type, $type->type_params[0]);
} 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)
if ($keyed_assignment_type) {
if ($keyed_array_var_id) {
if (isset($context->vars_in_scope[$keyed_array_var_id])) {
$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) {
2016-11-02 07:29:00 +01:00
if ($type instanceof Type\ObjectLike ||
($type->isGenericArray() && !$key_type->hasInt() && $type->type_params[1]->isEmpty())
) {
2016-11-01 19:14:35 +01:00
$properties = $key_value ? [$key_value => $keyed_assignment_type] : [];
$assignment_type = new Type\Union([
new Type\ObjectLike(
'array',
$properties
)
]);
2016-11-02 07:29:00 +01:00
} else {
2016-11-01 19:14:35 +01:00
$assignment_type = new Type\Union([
new Type\Generic(
'array',
[
$key_type,
$keyed_assignment_type
]
)
]);
}
if (isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
$context->vars_in_scope[$var_id],
$assignment_type
);
2016-11-02 07:29:00 +01:00
} else {
2016-11-01 19:14:35 +01:00
$context->vars_in_scope[$var_id] = $assignment_type;
}
}
if ($type instanceof Type\Generic && $type->type_params[$value_index]->isEmpty()) {
$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']) &&
$array_type->types['array'] instanceof Type\Generic
) {
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;
/** @var Type\Generic */
$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;
}
2016-11-02 07:29:00 +01:00
} elseif ($type instanceof Type\Generic && $value_index !== null) {
2016-11-01 19:14:35 +01:00
$stmt->inferredType = $type->type_params[$value_index];
2016-11-02 07:29:00 +01:00
} elseif ($type instanceof Type\ObjectLike) {
2016-11-01 19:14:35 +01:00
if ($key_value && isset($type->properties[$key_value])) {
$stmt->inferredType = clone $type->properties[$key_value];
2016-11-02 07:29:00 +01:00
} elseif ($key_type->hasInt()) {
2016-11-01 19:14:35 +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 {
2016-11-01 19:14:35 +01:00
$last_key = array_pop($object_like_keys);
2016-11-02 07:29:00 +01:00
$expected_keys_string = '\'' . implode('\', \'', $object_like_keys) .
'\' or \'' . $last_key . '\'';
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
$expected_keys_string = 'string';
}
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,
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
} elseif ($type->isString()) {
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();
}
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();
} elseif ($type->isNull()) {
if (IssueBuffer::accepts(
2016-11-22 17:11:47 +01:00
new NullArrayAccess(
'Cannot access array value on possibly null variable ' . $array_var_id . ' of type ' . $var_type,
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
2016-11-22 17:11:47 +01:00
if (isset($stmt->inferredType)) {
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull());
} else {
2016-11-22 17:11:47 +01:00
$stmt->inferredType = Type::getNull();
}
continue;
}
2016-11-21 23:49:07 +01:00
} elseif ($type->isMixed() || $type->isEmpty()) {
if (IssueBuffer::accepts(
new MixedArrayAccess(
'Cannot access array value on mixed variable ' . $array_var_id,
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
$stmt->inferredType = Type::getMixed();
break;
}
} elseif (strtolower($type->value) !== 'simplexmlelement' &&
!ClassChecker::classImplements($type->value, 'ArrayAccess')
) {
if (IssueBuffer::accepts(
new InvalidArrayAccess(
'Cannot access array value on non-array variable ' . $array_var_id . ' of type ' . $var_type,
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
2016-11-22 00:38:56 +01:00
$stmt->inferredType = Type::getMixed();
break;
}
2016-11-01 19:14:35 +01:00
}
}
}
if ($keyed_array_var_id && isset($context->vars_in_scope[$keyed_array_var_id])) {
$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([
new Type\Atomic('int'),
new Type\Atomic('string')
]);
}
if ($stmt->dim) {
if (isset($stmt->dim->inferredType) && $key_type && !$key_type->isEmpty()) {
foreach ($stmt->dim->inferredType->types as $at) {
if (($at->isMixed() || $at->isEmpty()) &&
$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 ' .
$inferred_key_type,
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
} elseif (!$at->isIn($key_type)) {
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,
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
* @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,
CodeLocation $code_location
2016-11-01 19:14:35 +01:00
) {
if ($type->value === 'null') {
if (IssueBuffer::accepts(
new NullReference(
2016-11-05 02:14:04 +01:00
'Cannot assign value on possibly null array' . ($var_id ? ' ' . $var_id : ''),
$code_location
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
return $type;
}
if ($type->value === 'string' && $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
}
if (!$type->isArray() && !ClassChecker::classImplements($type->value, 'ArrayAccess')) {
if (IssueBuffer::accepts(
new InvalidArrayAssignment(
2016-11-05 02:14:04 +01:00
'Cannot assign value on variable' . ($var_id ? ' ' . $var_id : '') . ' of type ' . $type->value . ' that does not ' .
2016-11-02 07:29:00 +01:00
'implement ArrayAccess',
$code_location
2016-11-01 19:14:35 +01:00
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
return $type;
}
if ($type instanceof Type\Generic && $type->value === 'array') {
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;
}
}