2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Assignment;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Stmt\PropertyProperty;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
|
|
|
use Psalm\Issue\ImplicitToStringCast;
|
2018-12-02 00:37:49 +01:00
|
|
|
use Psalm\Issue\InternalProperty;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\InvalidPropertyAssignment;
|
|
|
|
use Psalm\Issue\InvalidPropertyAssignmentValue;
|
|
|
|
use Psalm\Issue\LoopInvalidation;
|
2018-04-07 18:13:30 +02:00
|
|
|
use Psalm\Issue\MixedAssignment;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\MixedPropertyAssignment;
|
|
|
|
use Psalm\Issue\MixedTypeCoercion;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
|
|
|
use Psalm\Issue\NullPropertyAssignment;
|
|
|
|
use Psalm\Issue\PossiblyFalsePropertyAssignmentValue;
|
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyAssignment;
|
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyAssignmentValue;
|
|
|
|
use Psalm\Issue\PossiblyNullPropertyAssignment;
|
|
|
|
use Psalm\Issue\PossiblyNullPropertyAssignmentValue;
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedPropertyAssignment;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyAssignment;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TObject;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class PropertyAssignmentAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PropertyFetch|PropertyProperty $stmt
|
|
|
|
* @param string $prop_name
|
|
|
|
* @param PhpParser\Node\Expr|null $assignment_value
|
|
|
|
* @param Type\Union $assignment_value_type
|
|
|
|
* @param Context $context
|
2018-08-02 06:40:51 +02:00
|
|
|
* @param bool $direct_assignment whether the variable is assigned explicitly
|
2018-01-14 18:09:40 +01:00
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeInstance(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
|
|
|
$prop_name,
|
|
|
|
$assignment_value,
|
|
|
|
Type\Union $assignment_value_type,
|
|
|
|
Context $context,
|
|
|
|
$direct_assignment = true
|
|
|
|
) {
|
|
|
|
$class_property_types = [];
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$property_exists = false;
|
|
|
|
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_ids = [];
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PropertyProperty) {
|
|
|
|
if (!$context->self || !$stmt->default) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_id = $context->self . '::$' . $prop_name;
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_ids[] = $property_id;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
if (!$codebase->properties->propertyExists($property_id)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_exists = true;
|
|
|
|
|
2019-01-19 20:26:27 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType($property_id, true);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type ? clone $class_property_type : Type::getMixed();
|
|
|
|
|
|
|
|
$var_id = '$this->' . $prop_name;
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type = isset($stmt->var->inferredType) ? $stmt->var->inferredType : null;
|
|
|
|
|
|
|
|
if ($lhs_type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$lhs_var_id = ExpressionAnalyzer::getVarId(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
|
|
|
|
|
|
|
if ($direct_assignment && isset($context->protected_var_ids[$var_id])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new LoopInvalidation(
|
|
|
|
'Variable ' . $var_id . ' has already been assigned in a for/foreach loop',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($lhs_type->hasMixed()) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyAssignment(
|
|
|
|
$lhs_var_id . ' of type mixed cannot be assigned to',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($lhs_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyAssignment(
|
|
|
|
$lhs_var_id . ' of type null cannot be assigned to',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lhs_type->isNullable() && !$lhs_type->ignore_nullable_issues) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullPropertyAssignment(
|
|
|
|
$lhs_var_id . ' with possibly null type \'' . $lhs_type . '\' cannot be assigned to',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = false;
|
|
|
|
|
|
|
|
$invalid_assignment_types = [];
|
|
|
|
|
|
|
|
$has_valid_assignment_type = false;
|
|
|
|
|
|
|
|
foreach ($lhs_type->getTypes() as $lhs_type_part) {
|
|
|
|
if ($lhs_type_part instanceof TNull) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$lhs_type_part instanceof TObject && !$lhs_type_part instanceof TNamedObject) {
|
|
|
|
$invalid_assignment_types[] = (string)$lhs_type_part;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_assignment_type = true;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if ($lhs_type_part instanceof TObject ||
|
|
|
|
(
|
|
|
|
in_array(
|
|
|
|
strtolower($lhs_type_part->value),
|
|
|
|
['stdclass', 'simplexmlelement', 'dateinterval', 'domdocument', 'domnode'],
|
|
|
|
true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if ($var_id) {
|
|
|
|
if ($lhs_type_part instanceof TNamedObject &&
|
|
|
|
strtolower($lhs_type_part->value) === 'stdclass'
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ExpressionAnalyzer::isMock($lhs_type_part->value)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
$intersection_types = $lhs_type_part->getIntersectionTypes() ?: [];
|
|
|
|
|
|
|
|
$fq_class_name = $lhs_type_part->value;
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
$override_property_visibility = false;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
$class_exists = false;
|
|
|
|
$interface_exists = false;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$codebase->classExists($lhs_type_part->value)) {
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($codebase->interfaceExists($lhs_type_part->value)) {
|
2019-01-23 22:58:50 +01:00
|
|
|
$interface_exists = true;
|
2018-11-25 00:31:00 +01:00
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get($lhs_type_part->value);
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
$override_property_visibility = $interface_storage->override_property_visibility;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
if ($intersection_type instanceof TNamedObject
|
|
|
|
&& $codebase->classExists($intersection_type->value)
|
|
|
|
) {
|
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
$class_exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
2019-01-23 22:58:50 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
2018-11-25 00:31:00 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
2019-01-23 22:58:50 +01:00
|
|
|
return null;
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$codebase->methodExists($fq_class_name . '::__set')) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$class_exists && !$interface_exists) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-11-25 00:31:00 +01:00
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot set properties of undefined class ' . $lhs_type_part->value,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
2018-11-25 00:31:00 +01:00
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
} else {
|
|
|
|
$class_exists = true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_ids[] = $property_id;
|
2018-05-09 02:25:39 +02:00
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
if ($codebase->methodExists($fq_class_name . '::__set')
|
2018-05-09 02:25:39 +02:00
|
|
|
&& (!$codebase->properties->propertyExists($property_id)
|
2018-06-10 16:02:46 +02:00
|
|
|
|| ($lhs_var_id !== '$this'
|
2018-11-25 00:31:00 +01:00
|
|
|
&& $fq_class_name !== $context->self
|
2018-11-06 03:57:36 +01:00
|
|
|
&& ClassLikeAnalyzer::checkPropertyVisibility(
|
2018-06-10 16:02:46 +02:00
|
|
|
$property_id,
|
|
|
|
$context->self,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-06-10 16:02:46 +02:00
|
|
|
false
|
|
|
|
) !== true)
|
2018-05-09 02:25:39 +02:00
|
|
|
)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2018-11-25 00:31:00 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
if (isset($class_storage->pseudo_property_set_types['$' . $prop_name])) {
|
|
|
|
$class_property_types[] =
|
|
|
|
clone $class_storage->pseudo_property_set_types['$' . $prop_name];
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$has_regular_setter = true;
|
|
|
|
$property_exists = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have an explicit list of all allowed magic properties on the class, and we're
|
|
|
|
* not in that list, fall through
|
|
|
|
*/
|
|
|
|
if (!$var_id || !$class_storage->sealed_properties) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
continue;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = true;
|
|
|
|
|
2018-10-10 17:45:56 +02:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $stmt->var->name === 'this'
|
|
|
|
&& $context->self
|
|
|
|
) {
|
|
|
|
$self_property_id = $context->self . '::$' . $prop_name;
|
|
|
|
|
|
|
|
if ($self_property_id !== $property_id
|
|
|
|
&& $codebase->properties->propertyExists($self_property_id)
|
|
|
|
) {
|
|
|
|
$property_id = $self_property_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:10:37 +01:00
|
|
|
if (!$codebase->properties->propertyExists(
|
|
|
|
$property_id,
|
|
|
|
$context->calling_method_id,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable && $stmt->var->name === 'this') {
|
|
|
|
// if this is a proper error, we'll see it on the first pass
|
|
|
|
if ($context->collect_mutations) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyAssignment(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_exists = true;
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
if (!$override_property_visibility) {
|
2018-11-25 00:31:00 +01:00
|
|
|
if (!$context->collect_mutations) {
|
|
|
|
if (ClassLikeAnalyzer::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ClassLikeAnalyzer::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
|
|
|
false
|
|
|
|
) !== true) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
$declaring_property_class = (string) $codebase->properties->getDeclaringClassForProperty(
|
2018-10-10 17:45:56 +02:00
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($declaring_property_class);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$property_storage = $class_storage->properties[$prop_name];
|
|
|
|
|
|
|
|
if ($property_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedProperty(
|
|
|
|
$property_id . ' is marked deprecated',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-05-11 06:07:41 +02:00
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
if ($property_storage->internal && $context->self) {
|
|
|
|
$self_root = preg_replace('/^([^\\\]+).*/', '$1', $context->self);
|
|
|
|
$declaring_root = preg_replace('/^([^\\\]+).*/', '$1', $declaring_property_class);
|
|
|
|
|
|
|
|
if (strtolower($self_root) !== strtolower($declaring_root)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalProperty(
|
|
|
|
$property_id . ' is marked internal',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 20:26:27 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType($property_id, true);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if (!$class_property_type) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!$assignment_value_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($property_storage->suggested_type) {
|
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
$assignment_value_type,
|
|
|
|
$property_storage->suggested_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$property_storage->suggested_type =
|
|
|
|
$lhs_var_id === '$this' &&
|
|
|
|
($context->inside_constructor || $context->collect_initializations)
|
|
|
|
? $assignment_value_type
|
|
|
|
: Type::combineUnionTypes(Type::getNull(), $assignment_value_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$class_property_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
$class_property_type,
|
2018-11-25 00:31:00 +01:00
|
|
|
$fq_class_name,
|
|
|
|
$lhs_type_part
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
2018-04-07 18:13:30 +02:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!$class_property_type->hasMixed() && $assignment_value_type->hasMixed()) {
|
2018-04-07 18:13:30 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedAssignment(
|
|
|
|
'Cannot assign ' . $var_id . ' to a mixed type',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-04-07 18:13:30 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-07 18:13:30 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_assignment_types) {
|
|
|
|
$invalid_assignment_type = $invalid_assignment_types[0];
|
|
|
|
|
|
|
|
if (!$has_valid_assignment_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
|
|
|
$lhs_var_id . ' with non-object type \'' . $invalid_assignment_type .
|
|
|
|
'\' cannot treated as an object',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyAssignment(
|
|
|
|
$lhs_var_id . ' with possible non-object type \'' . $invalid_assignment_type .
|
|
|
|
'\' cannot treated as an object',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->var)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has_regular_setter) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
// because we don't want to be assigning for property declarations
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$property_exists) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($assignment_value_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_assignment_value_types = [];
|
|
|
|
|
|
|
|
$has_valid_assignment_value_type = false;
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
&& count($class_property_types) === 1
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->name,
|
|
|
|
(string) $class_property_types[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
foreach ($class_property_types as $class_property_type) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($class_property_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed,
|
|
|
|
$to_string_cast
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($type_coerced) {
|
|
|
|
if ($type_coerced_from_mixed) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedTypeCoercion(
|
2019-01-02 12:58:49 +01:00
|
|
|
$var_id . ' expects \'' . $class_property_type->getId() . '\', '
|
|
|
|
. ' parent type `' . $assignment_value_type->getId() . '` provided',
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
2019-01-02 12:58:49 +01:00
|
|
|
$var_id . ' expects \'' . $class_property_type->getId() . '\', '
|
|
|
|
. ' parent type \'' . $assignment_value_type->getId() . '\' provided',
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($to_string_cast) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplicitToStringCast(
|
|
|
|
$var_id . ' expects \'' . $class_property_type . '\', '
|
|
|
|
. '\'' . $assignment_value_type . '\' provided with a __toString method',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type_match_found && !$type_coerced) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (TypeAnalyzer::canBeContainedBy(
|
|
|
|
$codebase,
|
2018-05-03 19:56:30 +02:00
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
)) {
|
|
|
|
$has_valid_assignment_value_type = true;
|
2018-03-21 04:44:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
$invalid_assignment_value_types[] = $class_property_type->getId();
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$has_valid_assignment_value_type = true;
|
|
|
|
}
|
2018-05-23 05:38:27 +02:00
|
|
|
|
|
|
|
if ($type_match_found) {
|
|
|
|
if (!$assignment_value_type->ignore_nullable_issues
|
|
|
|
&& $assignment_value_type->isNullable()
|
|
|
|
&& !$class_property_type->isNullable()
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullPropertyAssignmentValue(
|
|
|
|
$var_id . ' with non-nullable declared type \'' . $class_property_type .
|
|
|
|
'\' cannot be assigned nullable type \'' . $assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-05-23 05:38:27 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
),
|
|
|
|
$property_ids[0]
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-23 05:38:27 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$assignment_value_type->ignore_falsable_issues
|
|
|
|
&& $assignment_value_type->isFalsable()
|
|
|
|
&& !$class_property_type->hasBool()
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyFalsePropertyAssignmentValue(
|
|
|
|
$var_id . ' with non-falsable declared type \'' . $class_property_type .
|
|
|
|
'\' cannot be assigned possibly false type \'' . $assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-05-23 05:38:27 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
),
|
|
|
|
$property_ids[0]
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-23 05:38:27 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_assignment_value_types) {
|
|
|
|
$invalid_class_property_type = $invalid_assignment_value_types[0];
|
|
|
|
|
|
|
|
if (!$has_valid_assignment_value_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignmentValue(
|
|
|
|
$var_id . ' with declared type \'' . $invalid_class_property_type .
|
2018-05-03 19:56:30 +02:00
|
|
|
'\' cannot be assigned type \'' . $assignment_value_type->getId() . '\'',
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
2018-05-11 06:07:41 +02:00
|
|
|
),
|
|
|
|
$property_ids[0]
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyAssignmentValue(
|
|
|
|
$var_id . ' with declared type \'' . $invalid_class_property_type .
|
|
|
|
'\' cannot be assigned possibly different type \'' .
|
2018-05-03 19:56:30 +02:00
|
|
|
$assignment_value_type->getId() . '\'',
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
2018-05-11 06:07:41 +02:00
|
|
|
),
|
|
|
|
$property_ids[0]
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
|
|
|
* @param PhpParser\Node\Expr|null $assignment_value
|
|
|
|
* @param Type\Union $assignment_value_type
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeStatic(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\StaticPropertyFetch $stmt,
|
|
|
|
$assignment_value,
|
|
|
|
Type\Union $assignment_value_type,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$fq_class_name = (string)$stmt->class->inferredType;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$prop_name = $stmt->name;
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$prop_name instanceof PhpParser\Node\Identifier) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
if (!$codebase->properties->propertyExists($property_id, $context->calling_method_id)) {
|
2018-06-10 16:02:46 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-06-10 16:02:46 +02:00
|
|
|
$property_id
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-06-10 16:02:46 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ClassLikeAnalyzer::checkPropertyVisibility(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
|
|
|
$context->self,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
2018-04-17 18:16:25 +02:00
|
|
|
$fq_class_name . '::$' . $prop_name->name
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get((string)$declaring_property_class);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$property_storage = $class_storage->properties[$prop_name->name];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
|
2019-01-19 20:26:27 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType($property_id, true);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if (!$class_property_type) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!$assignment_value_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($property_storage->suggested_type) {
|
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
$assignment_value_type,
|
|
|
|
$property_storage->suggested_type
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$property_storage->suggested_type = Type::combineUnionTypes(
|
|
|
|
Type::getNull(),
|
|
|
|
$assignment_value_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$class_property_type = clone $class_property_type;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($assignment_value_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($class_property_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$class_property_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
$class_property_type,
|
2018-01-26 19:51:00 +01:00
|
|
|
$fq_class_name,
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name
|
|
|
|
);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$type_match_found = TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value_type,
|
2018-05-03 19:56:30 +02:00
|
|
|
$class_property_type,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed,
|
|
|
|
$to_string_cast
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($type_coerced) {
|
|
|
|
if ($type_coerced_from_mixed) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedTypeCoercion(
|
|
|
|
$var_id . ' expects \'' . $class_property_type . '\', '
|
|
|
|
. ' parent type `' . $assignment_value_type . '` provided',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-05-03 19:56:30 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:56:30 +02:00
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
|
|
|
$var_id . ' expects \'' . $class_property_type . '\', '
|
|
|
|
. ' parent type \'' . $assignment_value_type . '\' provided',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-05-03 19:56:30 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:56:30 +02:00
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($to_string_cast) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplicitToStringCast(
|
|
|
|
$var_id . ' expects \'' . $class_property_type . '\', '
|
|
|
|
. '\'' . $assignment_value_type . '\' provided with a __toString method',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-05-03 19:56:30 +02:00
|
|
|
$assignment_value ?: $stmt,
|
|
|
|
$context->include_location
|
|
|
|
)
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-03 19:56:30 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type_match_found && !$type_coerced) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (TypeAnalyzer::canBeContainedBy($codebase, $assignment_value_type, $class_property_type)) {
|
2018-03-21 04:19:26 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyAssignmentValue(
|
|
|
|
$var_id . ' with declared type \'' . $class_property_type . '\' cannot be assigned type \'' .
|
|
|
|
$assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-03-21 04:19:26 +01:00
|
|
|
$assignment_value ?: $stmt
|
2018-05-11 06:07:41 +02:00
|
|
|
),
|
|
|
|
$property_id
|
2018-03-21 04:19:26 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-21 04:19:26 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignmentValue(
|
|
|
|
$var_id . ' with declared type \'' . $class_property_type . '\' cannot be assigned type \'' .
|
|
|
|
$assignment_value_type . '\'',
|
|
|
|
new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
2018-03-21 04:19:26 +01:00
|
|
|
$assignment_value ?: $stmt
|
2018-05-11 06:07:41 +02:00
|
|
|
),
|
|
|
|
$property_id
|
2018-03-21 04:19:26 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-21 04:19:26 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|