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;
|
2020-07-08 01:32:44 +02:00
|
|
|
use Psalm\Internal\Analyzer\ClassAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
2019-05-14 23:30:16 +02:00
|
|
|
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
|
2020-06-18 17:53:24 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\InstancePropertyFetchAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-07-22 01:40:35 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
|
|
|
use Psalm\Issue\ImplicitToStringCast;
|
2020-07-23 08:40:35 +02:00
|
|
|
use Psalm\Issue\ImpurePropertyAssignment;
|
2019-08-11 22:01:37 +02:00
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
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;
|
2019-04-26 00:02:19 +02:00
|
|
|
use Psalm\Issue\MixedPropertyTypeCoercion;
|
2018-01-14 18:09:40 +01:00
|
|
|
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;
|
2019-04-26 00:02:19 +02:00
|
|
|
use Psalm\Issue\PropertyTypeCoercion;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedPropertyAssignment;
|
2020-01-06 03:07:26 +01:00
|
|
|
use Psalm\Issue\UndefinedMagicPropertyAssignment;
|
2018-01-14 18:09:40 +01:00
|
|
|
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;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function count;
|
|
|
|
use function in_array;
|
|
|
|
use function strtolower;
|
2020-09-21 05:59:52 +02:00
|
|
|
use Psalm\Internal\ControlFlow\ControlFlowNode;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-06-19 07:22:51 +02:00
|
|
|
class InstancePropertyAssignmentAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param PropertyFetch|PropertyProperty $stmt
|
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
|
|
|
|
*/
|
2020-06-19 07:22:51 +02:00
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-09-07 01:36:47 +02:00
|
|
|
PhpParser\NodeAbstract $stmt,
|
|
|
|
string $prop_name,
|
|
|
|
?PhpParser\Node\Expr $assignment_value,
|
2018-01-14 18:09:40 +01:00
|
|
|
Type\Union $assignment_value_type,
|
|
|
|
Context $context,
|
2020-09-07 01:36:47 +02:00
|
|
|
bool $direct_assignment = true
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?bool {
|
2018-01-14 18:09:40 +01:00
|
|
|
$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
|
|
|
|
|
|
|
$property_exists = true;
|
|
|
|
|
2020-07-01 16:30:10 +02:00
|
|
|
try {
|
|
|
|
$class_property_type = $codebase->properties->getPropertyType(
|
|
|
|
$property_id,
|
|
|
|
true,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
if ($class_property_type) {
|
2019-06-09 18:47:27 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($context->self);
|
|
|
|
|
2020-05-11 04:45:01 +02:00
|
|
|
$class_property_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2019-06-09 18:47:27 +02:00
|
|
|
$codebase,
|
2020-04-01 06:45:29 +02:00
|
|
|
clone $class_property_type,
|
2020-02-15 02:54:26 +01:00
|
|
|
$class_storage->name,
|
|
|
|
$class_storage->name,
|
2019-06-09 18:47:27 +02:00
|
|
|
$class_storage->parent_class
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_types[] = $class_property_type ?: Type::getMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$lhs_type = $statements_analyzer->node_data->getType($stmt->var);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($lhs_type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$lhs_var_id = ExpressionIdentifier::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
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$var_id = ExpressionIdentifier::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()) {
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2019-04-17 17:12:18 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier) {
|
2019-04-27 23:38:24 +02:00
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
'$' . $stmt->name->name,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
2019-04-27 23:38:24 +02:00
|
|
|
);
|
2019-04-17 17:12:18 +02: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;
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$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;
|
|
|
|
|
2020-05-19 23:31:05 +02:00
|
|
|
$lhs_atomic_types = $lhs_type->getAtomicTypes();
|
|
|
|
|
|
|
|
while ($lhs_atomic_types) {
|
|
|
|
$lhs_type_part = \array_pop($lhs_atomic_types);
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
$lhs_atomic_types = \array_merge(
|
|
|
|
$lhs_atomic_types,
|
|
|
|
$lhs_type_part->as->getAtomicTypes()
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($lhs_type_part instanceof TNull) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-11 20:53:21 +02:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TFalse
|
|
|
|
&& $lhs_type->ignore_falsable_issues
|
2020-01-04 18:20:26 +01:00
|
|
|
&& count($lhs_type->getAtomicTypes()) > 1
|
2019-04-11 20:53:21 +02:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
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;
|
2020-02-15 02:54:26 +01:00
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
strtolower($lhs_type_part->value)
|
|
|
|
);
|
2018-11-25 00:31:00 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if (!$codebase->methods->methodExists(
|
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$fq_class_name,
|
|
|
|
'__set'
|
|
|
|
)
|
|
|
|
)) {
|
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 (!$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
|
|
|
|
2020-01-06 03:58:18 +01:00
|
|
|
$has_magic_setter = false;
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$set_method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, '__set');
|
|
|
|
|
2020-05-13 04:39:18 +02:00
|
|
|
if ((!$codebase->properties->propertyExists($property_id, false, $statements_analyzer, $context)
|
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,
|
2019-03-01 14:57:10 +01:00
|
|
|
$context,
|
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
|
|
|
)
|
2020-05-13 04:39:18 +02:00
|
|
|
&& $codebase->methods->methodExists(
|
|
|
|
$set_method_id,
|
|
|
|
$context->calling_method_id,
|
|
|
|
$codebase->collect_locations
|
|
|
|
? new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
: null,
|
|
|
|
!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
? $statements_analyzer
|
|
|
|
: null,
|
|
|
|
$statements_analyzer->getFilePath()
|
|
|
|
)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2020-01-06 03:58:18 +01:00
|
|
|
$has_magic_setter = true;
|
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;
|
2019-08-06 19:05:34 +02:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
if (!$context->collect_initializations) {
|
|
|
|
self::taintProperty(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$property_id,
|
|
|
|
$class_storage,
|
|
|
|
$assignment_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 08:15:35 +02:00
|
|
|
if ($assignment_value) {
|
2019-07-06 06:00:17 +02:00
|
|
|
if ($var_id) {
|
|
|
|
$context->removeVarFromConflictingClauses(
|
|
|
|
$var_id,
|
|
|
|
Type::getMixed(),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
|
|
|
unset($context->vars_in_scope[$var_id]);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$old_data_provider = $statements_analyzer->node_data;
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
|
|
|
|
2019-05-24 08:15:35 +02:00
|
|
|
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
|
|
|
|
$stmt->var,
|
|
|
|
new PhpParser\Node\Identifier('__set', $stmt->name->getAttributes()),
|
|
|
|
[
|
|
|
|
new PhpParser\Node\Arg(
|
|
|
|
new PhpParser\Node\Scalar\String_(
|
|
|
|
$prop_name,
|
|
|
|
$stmt->name->getAttributes()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new PhpParser\Node\Arg(
|
|
|
|
$assignment_value
|
2019-05-24 08:12:58 +02:00
|
|
|
)
|
2019-05-24 08:15:35 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2019-05-24 20:09:01 +02:00
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
if (!in_array('PossiblyNullReference', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->addSuppressedIssues(['PossiblyNullReference']);
|
|
|
|
}
|
|
|
|
|
2019-05-24 08:15:35 +02:00
|
|
|
\Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$fake_method_call,
|
2019-05-27 15:18:34 +02:00
|
|
|
$context,
|
|
|
|
false
|
2019-05-24 08:15:35 +02:00
|
|
|
);
|
2019-05-24 20:09:01 +02:00
|
|
|
|
|
|
|
if (!in_array('PossiblyNullReference', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyNullReference']);
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
$statements_analyzer->node_data = $old_data_provider;
|
2019-05-24 08:15:35 +02:00
|
|
|
}
|
2019-05-24 08:12:58 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/*
|
|
|
|
* If we have an explicit list of all allowed magic properties on the class, and we're
|
|
|
|
* not in that list, fall through
|
|
|
|
*/
|
|
|
|
if (!$var_id || !$class_storage->sealed_properties) {
|
2020-06-19 07:22:51 +02:00
|
|
|
if (!$context->collect_initializations) {
|
|
|
|
self::taintProperty(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$property_id,
|
|
|
|
$class_storage,
|
|
|
|
$assignment_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2019-08-06 20:27:21 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
if (IssueBuffer::accepts(
|
2020-01-06 03:07:26 +01:00
|
|
|
new UndefinedMagicPropertyAssignment(
|
2020-01-07 07:06:51 +01:00
|
|
|
'Magic instance property ' . $property_id . ' is not defined',
|
2019-01-23 22:58:50 +01:00
|
|
|
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
|
2019-03-01 14:57:10 +01:00
|
|
|
&& $codebase->properties->propertyExists(
|
|
|
|
$self_property_id,
|
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context
|
|
|
|
)
|
2018-10-10 17:45:56 +02:00
|
|
|
) {
|
|
|
|
$property_id = $self_property_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
if ($statements_analyzer->control_flow_graph && !$context->collect_initializations) {
|
2020-06-19 07:22:51 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
self::taintProperty(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$property_id,
|
|
|
|
$class_storage,
|
|
|
|
$assignment_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2019-01-22 17:10:37 +01:00
|
|
|
if (!$codebase->properties->propertyExists(
|
|
|
|
$property_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2019-01-22 17:10:37 +01:00
|
|
|
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
|
|
|
)) {
|
2019-03-06 00:27:25 +01:00
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-06 03:58:18 +01:00
|
|
|
if ($has_magic_setter) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMagicPropertyAssignment(
|
|
|
|
'Magic instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2018-10-26 22:17:15 +02:00
|
|
|
) {
|
|
|
|
$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,
|
2019-03-01 14:57:10 +01:00
|
|
|
$context,
|
2018-11-25 00:31:00 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ClassLikeAnalyzer::checkPropertyVisibility(
|
|
|
|
$property_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
$context,
|
2018-11-25 00:31:00 +01:00
|
|
|
$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(
|
2019-03-01 14:57:10 +01:00
|
|
|
$property_id,
|
|
|
|
false
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2019-06-04 06:32:19 +02:00
|
|
|
if ($codebase->properties_to_rename) {
|
|
|
|
$declaring_property_id = strtolower($declaring_property_class) . '::$' . $prop_name;
|
|
|
|
|
|
|
|
foreach ($codebase->properties_to_rename as $original_property_id => $new_property_name) {
|
|
|
|
if ($declaring_property_id === $original_property_id) {
|
|
|
|
$file_manipulations = [
|
|
|
|
new \Psalm\FileManipulation(
|
|
|
|
(int) $stmt->name->getAttribute('startFilePos'),
|
|
|
|
(int) $stmt->name->getAttribute('endFilePos') + 1,
|
|
|
|
$new_property_name
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:18:21 +01:00
|
|
|
$declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_property_class);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-03-28 22:18:21 +01:00
|
|
|
if (isset($declaring_class_storage->properties[$prop_name])) {
|
|
|
|
$property_storage = $declaring_class_storage->properties[$prop_name];
|
2018-12-02 00:37:49 +01:00
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($property_storage->deprecated) {
|
2018-12-02 00:37:49 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2019-03-01 14:57:10 +01:00
|
|
|
new DeprecatedProperty(
|
|
|
|
$property_id . ' is marked deprecated',
|
2018-12-02 00:37:49 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 14:57:10 +01:00
|
|
|
|
2020-07-24 15:32:54 +02:00
|
|
|
if ($context->self && ! NamespaceAnalyzer::isWithin($context->self, $property_storage->internal)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalProperty(
|
2020-07-24 21:38:56 +02:00
|
|
|
$property_id . ' is internal to ' . $property_storage->internal
|
|
|
|
. ' but called from ' . $context->self,
|
2020-07-24 15:32:54 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2019-05-11 16:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:18:34 +02:00
|
|
|
self::trackPropertyImpurity(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
2020-08-25 01:24:27 +02:00
|
|
|
$property_id,
|
2020-08-26 18:18:34 +02:00
|
|
|
$property_storage,
|
|
|
|
$declaring_class_storage,
|
|
|
|
$context
|
2020-08-25 01:24:27 +02:00
|
|
|
);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
2020-08-25 01:24:27 +02:00
|
|
|
if (!$property_storage->readonly
|
|
|
|
&& !$context->collect_mutations
|
2020-07-23 08:40:35 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& isset($context->vars_in_scope[$lhs_var_id])
|
|
|
|
&& !$context->vars_in_scope[$lhs_var_id]->allow_mutations
|
|
|
|
) {
|
2020-08-23 16:28:26 +02:00
|
|
|
if ($context->mutation_free) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyAssignment(
|
|
|
|
'Cannot assign to a property from a mutation-free context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2020-08-28 18:42:55 +02:00
|
|
|
} elseif ($statements_analyzer->getSource()
|
2020-08-23 16:28:26 +02:00
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
2020-08-28 18:42:55 +02:00
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-23 16:28:26 +02:00
|
|
|
) {
|
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
2020-07-23 08:40:35 +02:00
|
|
|
}
|
2019-08-11 22:01:37 +02:00
|
|
|
}
|
2020-07-16 18:59:49 +02:00
|
|
|
|
|
|
|
if ($property_storage->getter_method) {
|
|
|
|
$getter_id = $lhs_var_id . '->' . $property_storage->getter_method . '()';
|
|
|
|
|
|
|
|
unset($context->vars_in_scope[$getter_id]);
|
|
|
|
}
|
2018-12-02 00:37:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType(
|
|
|
|
$property_id,
|
|
|
|
true,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-07-08 20:32:16 +02:00
|
|
|
if (!$class_property_type
|
|
|
|
|| (isset($declaring_class_storage->properties[$prop_name])
|
|
|
|
&& !$declaring_class_storage->properties[$prop_name]->type_location)
|
|
|
|
) {
|
|
|
|
if (!$class_property_type) {
|
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-07-08 01:32:44 +02:00
|
|
|
$source_analyzer = $statements_analyzer->getSource()->getSource();
|
|
|
|
|
2020-07-08 18:18:36 +02:00
|
|
|
if ($lhs_var_id === '$this'
|
2020-07-08 01:32:44 +02:00
|
|
|
&& $source_analyzer instanceof ClassAnalyzer
|
|
|
|
) {
|
|
|
|
if (isset($source_analyzer->inferred_property_types[$prop_name])) {
|
|
|
|
$source_analyzer->inferred_property_types[$prop_name] = Type::combineUnionTypes(
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value_type,
|
2020-07-08 01:32:44 +02:00
|
|
|
$source_analyzer->inferred_property_types[$prop_name]
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-07-08 20:43:36 +02:00
|
|
|
$source_analyzer->inferred_property_types[$prop_name] = $assignment_value_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-08 20:32:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_property_type->isMixed()) {
|
2020-07-25 23:26:07 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
2020-05-11 04:45:01 +02:00
|
|
|
$class_property_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2020-04-01 06:45:29 +02:00
|
|
|
clone $class_property_type,
|
2018-11-25 00:31:00 +01:00
|
|
|
$fq_class_name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$lhs_type_part,
|
2020-07-25 23:26:07 +02:00
|
|
|
$declaring_class_storage->parent_class,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
$class_storage->final
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
2018-04-07 18:13:30 +02:00
|
|
|
|
2019-10-04 19:51:28 +02:00
|
|
|
$class_property_type = \Psalm\Internal\Codebase\Methods::localizeType(
|
|
|
|
$codebase,
|
|
|
|
$class_property_type,
|
|
|
|
$fq_class_name,
|
|
|
|
$declaring_property_class
|
|
|
|
);
|
|
|
|
|
2020-03-28 22:18:21 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TGenericObject) {
|
2020-06-18 17:53:24 +02:00
|
|
|
$class_property_type = InstancePropertyFetchAnalyzer::localizePropertyType(
|
2020-03-28 22:18:21 +01:00
|
|
|
$codebase,
|
|
|
|
$class_property_type,
|
|
|
|
$lhs_type_part,
|
|
|
|
$class_storage,
|
|
|
|
$declaring_class_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:51:28 +02:00
|
|
|
$assignment_value_type = \Psalm\Internal\Codebase\Methods::localizeType(
|
|
|
|
$codebase,
|
|
|
|
$assignment_value_type,
|
|
|
|
$fq_class_name,
|
|
|
|
$declaring_property_class
|
|
|
|
);
|
|
|
|
|
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(
|
2019-03-16 17:53:54 +01:00
|
|
|
'Cannot assign' . ($var_id ? ' ' . $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) {
|
2019-03-20 00:43:12 +01:00
|
|
|
if ($context->collect_initializations
|
|
|
|
&& $lhs_var_id === '$this'
|
|
|
|
) {
|
|
|
|
$assignment_value_type->initialized_class = $context->self;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// 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;
|
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2018-10-26 22:17:15 +02:00
|
|
|
&& 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,
|
2020-02-23 23:03:27 +01:00
|
|
|
$class_property_types[0]->getId()
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
$union_comparison_results = new \Psalm\Internal\Type\Comparator\TypeComparisonResult();
|
2019-07-10 07:35:57 +02:00
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
$type_match_found = UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
$assignment_value_type,
|
|
|
|
$class_property_type,
|
|
|
|
true,
|
|
|
|
true,
|
2019-07-10 07:35:57 +02:00
|
|
|
$union_comparison_results
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2019-07-10 19:49:11 +02:00
|
|
|
if ($type_match_found && $union_comparison_results->replacement_union_type) {
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $union_comparison_results->replacement_union_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($union_comparison_results->type_coerced) {
|
|
|
|
if ($union_comparison_results->type_coerced_from_mixed) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
new MixedPropertyTypeCoercion(
|
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
|
2019-04-26 00:02:19 +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
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
new PropertyTypeCoercion(
|
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
|
2019-04-26 00:02:19 +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
|
|
|
)) {
|
|
|
|
// keep soldiering on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($union_comparison_results->to_string_cast) {
|
2018-01-14 18:09:40 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if (!$type_match_found && !$union_comparison_results->type_coerced) {
|
2020-07-22 01:40:35 +02:00
|
|
|
if (UnionTypeComparator::canBeContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
$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()
|
2019-04-03 16:59:27 +02:00
|
|
|
&& !$class_property_type->hasScalar()
|
2018-05-23 05:38:27 +02:00
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:18:34 +02:00
|
|
|
public static function trackPropertyImpurity(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
string $property_id,
|
|
|
|
\Psalm\Storage\PropertyStorage $property_storage,
|
|
|
|
\Psalm\Storage\ClassLikeStorage $declaring_class_storage,
|
|
|
|
Context $context
|
|
|
|
): void {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);
|
|
|
|
|
|
|
|
$property_var_pure_compatible = $stmt_var_type
|
|
|
|
&& $stmt_var_type->reference_free
|
|
|
|
&& $stmt_var_type->allow_mutations;
|
|
|
|
|
|
|
|
$appearing_property_class = $codebase->properties->getAppearingClassForProperty(
|
|
|
|
$property_id,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
|
|
|
|
|
|
|
if ($appearing_property_class && ($property_storage->readonly || $codebase->alter_code)) {
|
|
|
|
$can_set_readonly_property = $context->self
|
|
|
|
&& $context->calling_method_id
|
|
|
|
&& ($appearing_property_class === $context->self
|
|
|
|
|| $codebase->classExtends($context->self, $appearing_property_class))
|
|
|
|
&& (\strpos($context->calling_method_id, '::__construct')
|
|
|
|
|| \strpos($context->calling_method_id, '::unserialize')
|
|
|
|
|| \strpos($context->calling_method_id, '::__unserialize')
|
|
|
|
|| \strpos($context->calling_method_id, '::__clone')
|
|
|
|
|| $property_storage->allow_private_mutation
|
|
|
|
|| $property_var_pure_compatible);
|
|
|
|
|
|
|
|
if (!$can_set_readonly_property) {
|
|
|
|
if ($property_storage->readonly) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
$property_id . ' is marked readonly',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif (!$declaring_class_storage->mutation_free
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['MissingImmutableAnnotation'])
|
|
|
|
&& $statements_analyzer->getSource()
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addMutableClass($declaring_class_storage->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:56:23 +02:00
|
|
|
public static function analyzeStatement(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Stmt\Property $stmt,
|
|
|
|
Context $context
|
|
|
|
): void {
|
|
|
|
foreach ($stmt->props as $prop) {
|
|
|
|
if ($prop->default) {
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $prop->default, $context);
|
|
|
|
|
|
|
|
if ($prop_default_type = $statements_analyzer->node_data->getType($prop->default)) {
|
2020-06-19 07:22:51 +02:00
|
|
|
if (self::analyze(
|
2020-05-19 18:56:23 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$prop,
|
|
|
|
$prop->name->name,
|
|
|
|
$prop->default,
|
|
|
|
$prop_default_type,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 05:19:22 +02:00
|
|
|
private static function taintProperty(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
string $property_id,
|
2020-06-19 07:22:51 +02:00
|
|
|
\Psalm\Storage\ClassLikeStorage $class_storage,
|
|
|
|
Type\Union $assignment_value_type,
|
|
|
|
Context $context
|
2019-08-06 05:19:22 +02:00
|
|
|
) : void {
|
2020-09-21 05:59:52 +02:00
|
|
|
if (!$statements_analyzer->control_flow_graph) {
|
2019-08-06 05:19:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph = $statements_analyzer->control_flow_graph;
|
2020-09-21 00:42:21 +02:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_location = new CodeLocation($statements_analyzer->getSource(), $stmt->var);
|
|
|
|
$property_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
if ($class_storage->specialize_instance) {
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
|
|
|
$stmt->var,
|
|
|
|
null,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_property_id = ExpressionIdentifier::getArrayVarId(
|
|
|
|
$stmt,
|
|
|
|
null,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2019-08-09 18:55:33 +02:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
if ($var_id) {
|
2020-07-02 17:53:44 +02:00
|
|
|
if (\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
$context->vars_in_scope[$var_id]->parent_nodes = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$var_node = ControlFlowNode::getForAssignment(
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_id,
|
|
|
|
$var_location
|
2019-04-27 23:38:24 +02:00
|
|
|
);
|
2019-04-18 20:47:58 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($var_node);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$property_node = ControlFlowNode::getForAssignment(
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_property_id ?: $var_id . '->$property',
|
|
|
|
$property_location
|
|
|
|
);
|
2019-06-04 06:32:19 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($property_node);
|
2019-06-04 22:36:32 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath(
|
2020-06-19 07:22:51 +02:00
|
|
|
$property_node,
|
|
|
|
$var_node,
|
|
|
|
'property-assignment'
|
|
|
|
. ($stmt->name instanceof PhpParser\Node\Identifier ? '-' . $stmt->name : '')
|
|
|
|
);
|
2019-06-04 06:32:19 +02:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
if ($assignment_value_type->parent_nodes) {
|
|
|
|
foreach ($assignment_value_type->parent_nodes as $parent_node) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($parent_node, $property_node, '=');
|
2019-06-04 22:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
$stmt_var_type = clone $context->vars_in_scope[$var_id];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
if ($context->vars_in_scope[$var_id]->parent_nodes) {
|
|
|
|
foreach ($context->vars_in_scope[$var_id]->parent_nodes as $parent_node) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($parent_node, $var_node, '=');
|
2020-06-19 07:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$stmt_var_type->parent_nodes = [$var_node->id => $var_node];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
$context->vars_in_scope[$var_id] = $stmt_var_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-02 18:08:42 +02:00
|
|
|
if (\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
$assignment_value_type->parent_nodes = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
$code_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$localized_property_node = new ControlFlowNode(
|
2020-06-19 07:22:51 +02:00
|
|
|
$property_id . '-' . $code_location->file_name . ':' . $code_location->raw_file_start,
|
|
|
|
$property_id,
|
|
|
|
$code_location,
|
|
|
|
null
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($localized_property_node);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$property_node = new ControlFlowNode(
|
2020-06-19 07:22:51 +02:00
|
|
|
$property_id,
|
|
|
|
$property_id,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($property_node);
|
2018-05-03 19:56:30 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($localized_property_node, $property_node, 'property-assignment');
|
2020-06-19 07:22:51 +02:00
|
|
|
|
|
|
|
if ($assignment_value_type->parent_nodes) {
|
|
|
|
foreach ($assignment_value_type->parent_nodes as $parent_node) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($parent_node, $localized_property_node, '=');
|
2018-03-21 04:19:26 +01:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|