2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Fetch;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2020-10-07 15:56:21 +02:00
|
|
|
use Psalm\Config;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
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-03-28 22:18:21 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
|
2020-08-26 18:18:34 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\InstancePropertyAssignmentAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-04-07 06:13:56 +02:00
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
2020-08-23 16:57:24 +02:00
|
|
|
use Psalm\Issue\ImpurePropertyFetch;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\InvalidPropertyFetch;
|
2018-12-02 00:37:49 +01:00
|
|
|
use Psalm\Issue\InternalProperty;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\MissingPropertyType;
|
|
|
|
use Psalm\Issue\MixedPropertyFetch;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
|
|
|
use Psalm\Issue\NullPropertyFetch;
|
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyFetch;
|
|
|
|
use Psalm\Issue\PossiblyNullPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedClass;
|
2019-05-16 21:06:56 +02:00
|
|
|
use Psalm\Issue\UndefinedDocblockClass;
|
2020-01-06 03:07:26 +01:00
|
|
|
use Psalm\Issue\UndefinedMagicPropertyFetch;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\UndefinedPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyFetch;
|
2019-02-13 19:32:19 +01:00
|
|
|
use Psalm\Issue\UninitializedProperty;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
2020-03-28 22:18:21 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TObject;
|
2019-01-18 06:56:24 +01:00
|
|
|
use Psalm\Type\Atomic\TObjectWithProperties;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function array_values;
|
|
|
|
use function in_array;
|
|
|
|
use function array_keys;
|
2020-10-13 22:49:03 +02:00
|
|
|
use Psalm\Internal\DataFlow\DataFlowNode;
|
2020-09-30 18:28:13 +02:00
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-06-18 17:53:24 +02:00
|
|
|
class InstancePropertyFetchAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
2020-06-18 17:53:24 +02:00
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
2020-06-25 06:24:37 +02:00
|
|
|
Context $context,
|
|
|
|
bool $in_assignment = false
|
2020-05-18 21:13:27 +02:00
|
|
|
) : bool {
|
2020-09-30 18:28:13 +02:00
|
|
|
$was_inside_use = $context->inside_use;
|
|
|
|
$context->inside_use = true;
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->name, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
$context->inside_use = $was_inside_use;
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
$prop_name = $stmt->name->name;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif (($stmt_name_type = $statements_analyzer->node_data->getType($stmt->name))
|
|
|
|
&& $stmt_name_type->isSingleStringLiteral()
|
2018-05-09 03:21:22 +02:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$prop_name = $stmt_name_type->getSingleStringLiteral()->value;
|
2018-05-09 03:21:22 +02:00
|
|
|
} else {
|
|
|
|
$prop_name = null;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$stmt_var_id = ExpressionIdentifier::getArrayVarId(
|
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::getArrayVarId(
|
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
|
|
|
);
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if ($var_id && $context->hasVariable($var_id)) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = $context->vars_in_scope[$var_id];
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// we don't need to check anything
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
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-04-20 16:52:23 +02:00
|
|
|
|
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->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
|
|
|
$stmt_type->getId()
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:32:19 +01:00
|
|
|
if ($stmt_var_id === '$this'
|
2019-11-25 17:44:54 +01:00
|
|
|
&& !$stmt_type->initialized
|
2019-02-13 19:32:19 +01:00
|
|
|
&& $context->collect_initializations
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var))
|
|
|
|
&& $stmt_var_type->hasObjectType()
|
2019-02-13 19:32:19 +01:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
) {
|
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
|
|
|
|
$property_id = null;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_var_type->getAtomicTypes() as $lhs_type_part) {
|
2019-02-13 19:32:19 +01:00
|
|
|
if ($lhs_type_part instanceof TNamedObject) {
|
|
|
|
if (!$codebase->classExists($lhs_type_part->value)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_id = $lhs_type_part->value . '::$' . $stmt->name->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_id
|
|
|
|
&& $source instanceof FunctionLikeAnalyzer
|
|
|
|
&& $source->getMethodName() === '__construct'
|
2019-06-01 01:49:24 +02:00
|
|
|
&& !$context->inside_unset
|
2019-02-13 19:32:19 +01:00
|
|
|
) {
|
2020-03-22 00:23:24 +01:00
|
|
|
if ($context->inside_isset
|
|
|
|
|| ($context->inside_assignment
|
|
|
|
&& isset($context->vars_in_scope[$var_id])
|
|
|
|
&& $context->vars_in_scope[$var_id]->isNullable()
|
|
|
|
)
|
|
|
|
) {
|
2019-11-28 15:11:51 +01:00
|
|
|
$stmt_type->initialized = true;
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UninitializedProperty(
|
|
|
|
'Cannot use uninitialized property ' . $var_id,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$var_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-02-13 19:32:19 +01:00
|
|
|
|
2019-11-28 15:11:51 +01:00
|
|
|
$stmt_type->addType(new Type\Atomic\TNull);
|
|
|
|
}
|
2019-02-13 19:32:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var))
|
|
|
|
&& $stmt_var_type->hasObjectType()
|
2018-04-17 18:16:25 +02:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
|
|
|
// log the appearance
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_var_type->getAtomicTypes() as $lhs_type_part) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($lhs_type_part instanceof TNamedObject) {
|
2018-02-01 06:50:01 +01:00
|
|
|
if (!$codebase->classExists($lhs_type_part->value)) {
|
2018-01-25 21:40:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $stmt->name->name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-08-23 16:57:24 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($lhs_type_part->value);
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
self::processTaints(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt_type,
|
|
|
|
$property_id,
|
2020-08-23 16:57:24 +02:00
|
|
|
$class_storage,
|
2020-06-25 06:24:37 +02:00
|
|
|
$in_assignment
|
2020-06-19 07:22:51 +02:00
|
|
|
);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$codebase->properties->propertyExists(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
2020-01-05 23:19:57 +01:00
|
|
|
true,
|
2019-03-01 14:57:10 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2020-03-28 21:30:56 +01:00
|
|
|
$codebase->collect_locations
|
2018-11-11 18:01:14 +01:00
|
|
|
? new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2018-10-09 19:49:57 +02:00
|
|
|
: null
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$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
|
|
|
|
);
|
|
|
|
}
|
2020-08-23 16:57:24 +02:00
|
|
|
|
|
|
|
if (!$context->collect_mutations
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !($class_storage->external_mutation_free
|
|
|
|
&& $stmt_type->allow_mutations)
|
|
|
|
) {
|
|
|
|
if ($context->pure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyFetch(
|
|
|
|
'Cannot access a property on a mutable object from a pure 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:57:24 +02:00
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
2020-08-28 18:42:55 +02:00
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-23 16:57:24 +02:00
|
|
|
) {
|
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if ($stmt_var_id && $context->hasVariable($stmt_var_id)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt_var_type = $context->vars_in_scope[$stmt_var_id];
|
2019-11-25 17:44:54 +01:00
|
|
|
} else {
|
|
|
|
$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmt_var_type) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyFetch(
|
|
|
|
'Cannot get property on null variable ' . $stmt_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isEmpty()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyFetch(
|
|
|
|
'Cannot fetch property on empty var ' . $stmt_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($stmt_var_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 MixedPropertyFetch(
|
|
|
|
'Cannot fetch property on mixed var ' . $stmt_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-23 23:03:27 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-02-23 23:03:27 +01:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$stmt_var_type->getId()
|
|
|
|
);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
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->getRootFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2019-11-06 22:14:46 +01:00
|
|
|
if ($stmt_var_type->isNullable() && !$stmt_var_type->ignore_nullable_issues) {
|
|
|
|
if (!$context->inside_isset) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullPropertyFetch(
|
|
|
|
'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getNull());
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if (!$prop_name) {
|
2019-04-20 23:49:49 +02:00
|
|
|
if ($stmt_var_type->hasObjectType() && !$context->ignore_variable_property) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_var_type->getAtomicTypes() as $type) {
|
2019-04-18 20:47:58 +02:00
|
|
|
if ($type instanceof Type\Atomic\TNamedObject) {
|
2019-04-27 23:38:24 +02:00
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
strtolower($type->value) . '::$',
|
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-18 20:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_fetch_types = [];
|
|
|
|
$has_valid_fetch_type = false;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_var_type->getAtomicTypes() as $lhs_type_part) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($lhs_type_part instanceof TNull) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:40:06 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TTemplateParam) {
|
2019-01-08 22:55:53 +01:00
|
|
|
$extra_types = $lhs_type_part->extra_types;
|
|
|
|
|
|
|
|
$lhs_type_part = array_values(
|
2020-01-04 18:20:26 +01:00
|
|
|
$lhs_type_part->as->getAtomicTypes()
|
2019-01-08 22:55:53 +01:00
|
|
|
)[0];
|
|
|
|
|
|
|
|
$lhs_type_part->from_docblock = true;
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TNamedObject) {
|
|
|
|
$lhs_type_part->extra_types = $extra_types;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TMixed) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2018-12-08 19:18:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-08 22:57:16 +02:00
|
|
|
if ($lhs_type_part instanceof Type\Atomic\TFalse && $stmt_var_type->ignore_falsable_issues) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if (!$lhs_type_part instanceof TNamedObject && !$lhs_type_part instanceof TObject) {
|
|
|
|
$invalid_fetch_types[] = (string)$lhs_type_part;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_fetch_type = true;
|
|
|
|
|
2019-01-18 06:56:24 +01:00
|
|
|
if ($lhs_type_part instanceof TObjectWithProperties
|
|
|
|
&& isset($lhs_type_part->properties[$prop_name])
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_type = $statements_analyzer->node_data->getType($stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
Type::combineUnionTypes(
|
|
|
|
$lhs_type_part->properties[$prop_name],
|
|
|
|
$stmt_type
|
|
|
|
)
|
2019-01-18 06:56:24 +01:00
|
|
|
);
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $lhs_type_part->properties[$prop_name]);
|
2019-01-18 06:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// 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
|
2018-02-07 19:57:45 +01:00
|
|
|
if ($lhs_type_part instanceof TObject
|
2020-10-07 15:56:21 +02:00
|
|
|
|| in_array(strtolower($lhs_type_part->value), Config::getInstance()->getUniversalObjectCrates(), true)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2019-01-18 06:56:24 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ExpressionAnalyzer::isMock($lhs_type_part->value)) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2018-01-14 18:09:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-06 03:58:18 +01:00
|
|
|
$has_magic_getter = false;
|
|
|
|
|
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()
|
|
|
|
)) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$codebase->methodExists($fq_class_name . '::__set')) {
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-01-23 22:58:50 +01:00
|
|
|
}
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$class_exists && !$interface_exists) {
|
2019-05-16 21:06:56 +02:00
|
|
|
if ($lhs_type_part->from_docblock) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedDocblockClass(
|
|
|
|
'Cannot set properties of undefined docblock class ' . $lhs_type_part->value,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot set properties of undefined class ' . $lhs_type_part->value,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
} else {
|
|
|
|
$class_exists = true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:04:30 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2018-11-25 00:31:00 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-05-10 15:04:30 +02:00
|
|
|
$naive_property_exists = $codebase->properties->propertyExists(
|
|
|
|
$property_id,
|
|
|
|
true,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
$codebase->collect_locations ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
|
|
|
|
);
|
|
|
|
|
|
|
|
// add method before changing fq_class_name
|
2020-02-15 02:54:26 +01:00
|
|
|
$get_method_id = new \Psalm\Internal\MethodIdentifier($fq_class_name, '__get');
|
2020-05-10 15:04:30 +02:00
|
|
|
|
|
|
|
if (!$naive_property_exists
|
2020-08-05 21:49:19 +02:00
|
|
|
&& $class_storage->namedMixins
|
2020-05-10 15:04:30 +02:00
|
|
|
) {
|
2020-08-05 21:49:19 +02:00
|
|
|
foreach ($class_storage->namedMixins as $mixin) {
|
|
|
|
$new_property_id = $mixin->value . '::$' . $prop_name;
|
2020-05-25 06:19:46 +02:00
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
try {
|
|
|
|
$new_class_storage = $codebase->classlike_storage_provider->get($mixin->value);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$new_class_storage = null;
|
|
|
|
}
|
2020-05-25 06:19:46 +02:00
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
if ($new_class_storage
|
|
|
|
&& ($codebase->properties->propertyExists(
|
|
|
|
$new_property_id,
|
|
|
|
true,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
$codebase->collect_locations
|
|
|
|
? new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
|| isset($new_class_storage->pseudo_property_get_types['$' . $prop_name]))
|
|
|
|
) {
|
|
|
|
$fq_class_name = $mixin->value;
|
|
|
|
$lhs_type_part = clone $mixin;
|
|
|
|
$class_storage = $new_class_storage;
|
2020-05-10 15:04:30 +02:00
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
if (!isset($new_class_storage->pseudo_property_get_types['$' . $prop_name])) {
|
|
|
|
$naive_property_exists = true;
|
|
|
|
}
|
2020-05-10 15:04:30 +02:00
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
$property_id = $new_property_id;
|
|
|
|
}
|
2020-05-10 15:04:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:45:21 +02:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
|
|
|
$property_id,
|
|
|
|
true,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
2020-05-13 04:39:18 +02:00
|
|
|
if ((!$naive_property_exists
|
2018-06-08 17:08:57 +02:00
|
|
|
|| ($stmt_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-08 17:08:57 +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-08 17:08:57 +02:00
|
|
|
false
|
|
|
|
) !== true)
|
2018-05-09 02:25:39 +02:00
|
|
|
)
|
2020-05-13 04:39:18 +02:00
|
|
|
&& $codebase->methods->methodExists(
|
|
|
|
$get_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-05-09 02:25:39 +02:00
|
|
|
) {
|
2020-01-06 03:58:18 +01:00
|
|
|
$has_magic_getter = true;
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if (isset($class_storage->pseudo_property_get_types['$' . $prop_name])) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = clone $class_storage->pseudo_property_get_types['$' . $prop_name];
|
|
|
|
|
2020-08-10 18:45:21 +02:00
|
|
|
if ($class_storage->template_types) {
|
|
|
|
if (!$lhs_type_part instanceof TGenericObject) {
|
|
|
|
$type_params = [];
|
|
|
|
|
|
|
|
foreach ($class_storage->template_types as $type_map) {
|
|
|
|
$type_params[] = clone array_values($type_map)[0][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type_part = new TGenericObject($lhs_type_part->value, $type_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt_type = self::localizePropertyType(
|
|
|
|
$codebase,
|
|
|
|
$stmt_type,
|
|
|
|
$lhs_type_part,
|
|
|
|
$class_storage,
|
|
|
|
$declaring_property_class
|
|
|
|
? $codebase->classlike_storage_provider->get(
|
|
|
|
$declaring_property_class
|
|
|
|
) : $class_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
self::processTaints(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt_type,
|
|
|
|
$property_id,
|
2020-06-25 06:24:37 +02:00
|
|
|
$class_storage,
|
|
|
|
$in_assignment
|
2020-06-19 07:22:51 +02:00
|
|
|
);
|
2018-05-09 02:25:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
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-23 18:59:09 +02:00
|
|
|
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
|
|
|
|
$stmt->var,
|
|
|
|
new PhpParser\Node\Identifier('__get', $stmt->name->getAttributes()),
|
|
|
|
[
|
|
|
|
new PhpParser\Node\Arg(
|
|
|
|
new PhpParser\Node\Scalar\String_(
|
|
|
|
$prop_name,
|
|
|
|
$stmt->name->getAttributes()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
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-30 14:50:43 +02:00
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->addSuppressedIssues(['InternalMethod']);
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:59:09 +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-23 18:59:09 +02:00
|
|
|
);
|
|
|
|
|
2019-05-24 20:09:01 +02:00
|
|
|
if (!in_array('PossiblyNullReference', $suppressed_issues, true)) {
|
|
|
|
$statements_analyzer->removeSuppressedIssues(['PossiblyNullReference']);
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:53:20 +02:00
|
|
|
if (!in_array('InternalMethod', $suppressed_issues, true)) {
|
2019-05-30 14:50:43 +02:00
|
|
|
$statements_analyzer->removeSuppressedIssues(['InternalMethod']);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$fake_method_call_type = $statements_analyzer->node_data->getType($fake_method_call);
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = $old_data_provider;
|
|
|
|
|
|
|
|
if ($fake_method_call_type) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $fake_method_call_type);
|
|
|
|
} else {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
|
|
|
}
|
2019-05-23 18:59:09 +02:00
|
|
|
|
2019-08-06 05:19:22 +02:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $prop_name;
|
|
|
|
|
2018-05-09 02:25:39 +02:00
|
|
|
/*
|
|
|
|
* If we have an explicit list of all allowed magic properties on the class, and we're
|
|
|
|
* not in that list, fall through
|
|
|
|
*/
|
2018-11-25 17:11:33 +01:00
|
|
|
if (!$class_storage->sealed_properties && !$override_property_visibility) {
|
2018-05-09 02:25:39 +02:00
|
|
|
continue;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
|
2020-05-11 03:09:48 +02:00
|
|
|
if (!$class_exists) {
|
2020-05-10 15:04:30 +02:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $prop_name;
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2020-01-06 03:07:26 +01:00
|
|
|
new UndefinedMagicPropertyFetch(
|
|
|
|
'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
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:04:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-23 22:58:50 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
2019-01-23 22:58:50 +01:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:23:30 +01:00
|
|
|
$config = $statements_analyzer->getProjectAnalyzer()->getConfig();
|
|
|
|
|
2020-05-10 15:04:30 +02:00
|
|
|
if (!$naive_property_exists) {
|
2020-02-02 20:23:30 +01:00
|
|
|
if ($config->use_phpdoc_property_without_magic_or_parent
|
|
|
|
&& isset($class_storage->pseudo_property_get_types['$' . $prop_name])
|
|
|
|
) {
|
|
|
|
$stmt_type = clone $class_storage->pseudo_property_get_types['$' . $prop_name];
|
|
|
|
|
2020-08-10 18:45:21 +02:00
|
|
|
if ($class_storage->template_types) {
|
|
|
|
if (!$lhs_type_part instanceof TGenericObject) {
|
|
|
|
$type_params = [];
|
|
|
|
|
|
|
|
foreach ($class_storage->template_types as $type_map) {
|
|
|
|
$type_params[] = clone array_values($type_map)[0][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type_part = new TGenericObject($lhs_type_part->value, $type_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt_type = self::localizePropertyType(
|
|
|
|
$codebase,
|
|
|
|
$stmt_type,
|
|
|
|
$lhs_type_part,
|
|
|
|
$class_storage,
|
|
|
|
$declaring_property_class
|
|
|
|
? $codebase->classlike_storage_provider->get(
|
|
|
|
$declaring_property_class
|
|
|
|
) : $class_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-02 20:23:30 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
self::processTaints(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt_type,
|
|
|
|
$property_id,
|
2020-06-25 06:24:37 +02:00
|
|
|
$class_storage,
|
|
|
|
$in_assignment
|
2020-06-19 07:22:51 +02:00
|
|
|
);
|
2020-02-02 20:23:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
if ($fq_class_name !== $context->self
|
|
|
|
&& $context->self
|
2020-06-23 19:12:46 +02:00
|
|
|
&& $codebase->classlikes->classExtends($fq_class_name, $context->self)
|
2019-01-31 23:48:48 +01:00
|
|
|
&& $codebase->properties->propertyExists(
|
|
|
|
$context->self . '::$' . $prop_name,
|
2020-01-05 23:19:57 +01:00
|
|
|
true,
|
2019-03-01 14:57:10 +01:00
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2020-03-28 21:30:56 +01:00
|
|
|
$codebase->collect_locations
|
|
|
|
? new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
: null
|
2019-01-31 23:48:48 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
$property_id = $context->self . '::$' . $prop_name;
|
|
|
|
} else {
|
2019-02-13 20:04:10 +01:00
|
|
|
if ($context->inside_isset || $context->collect_initializations) {
|
2020-08-24 00:50:17 +02:00
|
|
|
if ($context->pure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyFetch(
|
|
|
|
'Cannot access a property on a mutable object from a pure context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2020-08-26 18:18:34 +02:00
|
|
|
} elseif ($context->inside_isset
|
2020-08-24 00:50:17 +02:00
|
|
|
&& $statements_analyzer->getSource()
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
2020-08-28 18:42:55 +02:00
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-24 00:50:17 +02:00
|
|
|
) {
|
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
if ($stmt_var_id === '$this') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyFetch(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-06 03:58:18 +01:00
|
|
|
if ($has_magic_getter) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedMagicPropertyFetch(
|
|
|
|
'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 UndefinedPropertyFetch(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-01-31 23:48:48 +01:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = Type::getMixed();
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-05-09 18:54:31 +02:00
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
if ($var_id) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $stmt_type;
|
2019-01-31 23:48:48 +01:00
|
|
|
}
|
2018-05-09 18:54:31 +02:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
2019-01-31 23:48:48 +01:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
if (!$override_property_visibility) {
|
2018-11-25 00:31:00 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
2019-03-01 14:57:10 +01:00
|
|
|
$property_id,
|
2020-01-06 00:37:07 +01:00
|
|
|
true,
|
|
|
|
$statements_analyzer
|
2019-03-01 14:57:10 +01:00
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($declaring_property_class === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$declaring_class_storage = $codebase->classlike_storage_provider->get(
|
2018-12-02 00:37:49 +01:00
|
|
|
$declaring_property_class
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2019-03-01 14:57:10 +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:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-26 18:18:34 +02:00
|
|
|
|
|
|
|
if ($context->inside_unset) {
|
|
|
|
InstancePropertyAssignmentAnalyzer::trackPropertyImpurity(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$property_id,
|
|
|
|
$property_storage,
|
|
|
|
$declaring_class_storage,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2018-12-02 00:37:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType(
|
|
|
|
$property_id,
|
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-01-11 23:21:50 +01:00
|
|
|
if (!$class_property_type) {
|
2020-03-06 13:57:00 +01:00
|
|
|
if ($declaring_class_storage->location
|
|
|
|
&& $config->isInProjectDirs(
|
|
|
|
$declaring_class_storage->location->file_path
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
|
|
|
'Property ' . $fq_class_name . '::$' . $prop_name
|
|
|
|
. ' does not have a declared type',
|
2020-09-01 15:21:03 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
2020-03-06 13:57:00 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
} else {
|
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,
|
2018-01-14 18:09:40 +01:00
|
|
|
clone $class_property_type,
|
2020-02-15 02:54:26 +01:00
|
|
|
$declaring_class_storage->name,
|
|
|
|
$declaring_class_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$declaring_class_storage->parent_class
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2020-05-03 05:37:59 +02:00
|
|
|
if ($declaring_class_storage->template_types) {
|
|
|
|
if (!$lhs_type_part instanceof TGenericObject) {
|
|
|
|
$type_params = [];
|
|
|
|
|
|
|
|
foreach ($declaring_class_storage->template_types as $type_map) {
|
|
|
|
$type_params[] = clone array_values($type_map)[0][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type_part = new TGenericObject($lhs_type_part->value, $type_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = self::localizePropertyType(
|
|
|
|
$codebase,
|
|
|
|
$class_property_type,
|
|
|
|
$lhs_type_part,
|
|
|
|
$class_storage,
|
|
|
|
$declaring_class_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
2020-03-28 22:18:21 +01:00
|
|
|
$class_property_type = self::localizePropertyType(
|
|
|
|
$codebase,
|
|
|
|
$class_property_type,
|
|
|
|
$lhs_type_part,
|
|
|
|
$class_storage,
|
|
|
|
$declaring_class_storage
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:57:24 +02:00
|
|
|
if (!$context->collect_mutations
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !($class_storage->external_mutation_free
|
|
|
|
&& $class_property_type->allow_mutations)
|
|
|
|
) {
|
|
|
|
if ($context->pure) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyFetch(
|
|
|
|
'Cannot access a property on a mutable object from a pure context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2020-08-28 18:42:55 +02:00
|
|
|
} elseif ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
2020-08-23 16:57:24 +02:00
|
|
|
) {
|
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 07:22:51 +02:00
|
|
|
self::processTaints(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$class_property_type,
|
|
|
|
$property_id,
|
2020-06-25 06:24:37 +02:00
|
|
|
$class_storage,
|
|
|
|
$in_assignment
|
2020-06-19 07:22:51 +02:00
|
|
|
);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_type = $statements_analyzer->node_data->getType($stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
Type::combineUnionTypes($class_property_type, $stmt_type)
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $class_property_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = $statements_analyzer->node_data->getType($stmt);
|
|
|
|
|
|
|
|
if ($stmt_var_type->isNullable() && !$context->inside_isset && $stmt_type) {
|
|
|
|
$stmt_type->addType(new TNull);
|
2019-08-23 19:27:38 +02:00
|
|
|
|
|
|
|
if ($stmt_var_type->ignore_nullable_issues) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->ignore_nullable_issues = true;
|
2019-08-23 19:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_type = $statements_analyzer->node_data->getType($stmt))
|
2018-10-26 22:17:15 +02:00
|
|
|
) {
|
|
|
|
$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
|
|
|
$stmt_type->getId()
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($invalid_fetch_types) {
|
|
|
|
$lhs_type_part = $invalid_fetch_types[0];
|
|
|
|
|
|
|
|
if ($has_valid_fetch_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidPropertyFetch(
|
|
|
|
'Cannot fetch property on possible non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
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
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyFetch(
|
|
|
|
'Cannot fetch property on non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $statements_analyzer->node_data->getType($stmt) ?: Type::getMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
return true;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 22:18:21 +01:00
|
|
|
public static function localizePropertyType(
|
|
|
|
\Psalm\Codebase $codebase,
|
|
|
|
Type\Union $class_property_type,
|
|
|
|
TGenericObject $lhs_type_part,
|
|
|
|
ClassLikeStorage $calling_class_storage,
|
|
|
|
ClassLikeStorage $declaring_class_storage
|
|
|
|
) : Type\Union {
|
|
|
|
$template_types = CallAnalyzer::getTemplateTypesForCall(
|
2020-07-05 18:05:25 +02:00
|
|
|
$codebase,
|
2020-03-28 22:18:21 +01:00
|
|
|
$declaring_class_storage,
|
2020-07-05 18:05:25 +02:00
|
|
|
$declaring_class_storage->name,
|
2020-03-28 22:18:21 +01:00
|
|
|
$calling_class_storage,
|
2020-07-05 21:21:44 +02:00
|
|
|
$calling_class_storage->template_types ?: []
|
2020-03-28 22:18:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-18 22:57:13 +02:00
|
|
|
$extended_types = $calling_class_storage->template_type_extends;
|
|
|
|
|
2020-03-28 22:18:21 +01:00
|
|
|
if ($template_types) {
|
2020-04-18 22:57:13 +02:00
|
|
|
if ($calling_class_storage->template_types) {
|
|
|
|
foreach ($lhs_type_part->type_params as $param_offset => $lhs_param_type) {
|
|
|
|
$i = -1;
|
|
|
|
|
|
|
|
foreach ($calling_class_storage->template_types as $calling_param_name => $_) {
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
if ($i === $param_offset) {
|
|
|
|
$template_types[$calling_param_name][$calling_class_storage->name] = [
|
|
|
|
$lhs_param_type,
|
|
|
|
0
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 22:18:21 +01:00
|
|
|
|
2020-04-18 22:57:13 +02:00
|
|
|
foreach ($template_types as $type_name => $_) {
|
|
|
|
if (isset($extended_types[$declaring_class_storage->name][$type_name])) {
|
|
|
|
$mapped_type = $extended_types[$declaring_class_storage->name][$type_name];
|
2020-03-28 22:18:21 +01:00
|
|
|
|
2020-04-18 22:57:13 +02:00
|
|
|
foreach ($mapped_type->getAtomicTypes() as $mapped_type_atomic) {
|
|
|
|
if (!$mapped_type_atomic instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_name = $mapped_type_atomic->param_name;
|
|
|
|
|
|
|
|
$position = false;
|
|
|
|
|
|
|
|
if (isset($calling_class_storage->template_types[$param_name])) {
|
|
|
|
$position = \array_search(
|
|
|
|
$param_name,
|
|
|
|
array_keys($calling_class_storage->template_types)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($position !== false && isset($lhs_type_part->type_params[$position])) {
|
|
|
|
$template_types[$type_name][$declaring_class_storage->name] = [
|
|
|
|
$lhs_type_part->type_params[$position],
|
|
|
|
0
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 22:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type->replaceTemplateTypesWithArgTypes(
|
2020-04-07 06:13:56 +02:00
|
|
|
new TemplateResult([], $template_types),
|
2020-03-28 22:18:21 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class_property_type;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:54:12 +02:00
|
|
|
private static function processTaints(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
Type\Union $type,
|
2020-06-19 07:22:51 +02:00
|
|
|
string $property_id,
|
2020-06-25 06:24:37 +02:00
|
|
|
\Psalm\Storage\ClassLikeStorage $class_storage,
|
|
|
|
bool $in_assignment
|
2019-08-06 18:54:12 +02:00
|
|
|
) : void {
|
2020-09-21 05:59:52 +02:00
|
|
|
if (!$statements_analyzer->control_flow_graph) {
|
2020-06-19 07:22:51 +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);
|
|
|
|
|
|
|
|
if ($class_storage->specialize_instance) {
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
|
|
|
$stmt->var,
|
|
|
|
null,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
|
|
|
$var_property_id = ExpressionIdentifier::getArrayVarId(
|
|
|
|
$stmt,
|
|
|
|
null,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_id) {
|
2020-07-02 17:53:44 +02:00
|
|
|
$var_type = $statements_analyzer->node_data->getType($stmt->var);
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if ($statements_analyzer->control_flow_graph instanceof TaintFlowGraph
|
|
|
|
&& $var_type
|
|
|
|
&& \in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
|
|
|
) {
|
2020-07-02 17:53:44 +02:00
|
|
|
$var_type->parent_nodes = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$var_node = DataFlowNode::getForAssignment(
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_id,
|
|
|
|
$var_location
|
|
|
|
);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($var_node);
|
2020-06-19 07:22:51 +02:00
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$property_node = DataFlowNode::getForAssignment(
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_property_id ?: $var_id . '->$property',
|
|
|
|
$property_location
|
|
|
|
);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($property_node);
|
2020-06-19 07:22:51 +02:00
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath(
|
2020-06-19 07:22:51 +02:00
|
|
|
$var_node,
|
|
|
|
$property_node,
|
|
|
|
'property-fetch'
|
|
|
|
. ($stmt->name instanceof PhpParser\Node\Identifier ? '-' . $stmt->name : '')
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($var_type && $var_type->parent_nodes) {
|
|
|
|
foreach ($var_type->parent_nodes as $parent_node) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath(
|
2020-06-19 07:22:51 +02:00
|
|
|
$parent_node,
|
|
|
|
$var_node,
|
|
|
|
'='
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$type->parent_nodes = [$property_node->id => $property_node];
|
2020-06-19 07:22:51 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-22 05:43:13 +02:00
|
|
|
$code_location = new CodeLocation($statements_analyzer, $stmt->name);
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$localized_property_node = new DataFlowNode(
|
2020-05-22 05:43:13 +02:00
|
|
|
$property_id . '-' . $code_location->file_name . ':' . $code_location->raw_file_start,
|
|
|
|
$property_id,
|
|
|
|
$code_location,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($localized_property_node);
|
2020-05-22 05:43:13 +02:00
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$property_node = new DataFlowNode(
|
2019-10-19 23:59:10 +02:00
|
|
|
$property_id,
|
2019-08-06 18:54:12 +02:00
|
|
|
$property_id,
|
2020-05-22 05:43:13 +02:00
|
|
|
null,
|
2020-05-22 04:47:58 +02:00
|
|
|
null
|
2019-08-06 18:54:12 +02:00
|
|
|
);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addNode($property_node);
|
2020-05-22 05:43:13 +02:00
|
|
|
|
2020-06-25 06:24:37 +02:00
|
|
|
if ($in_assignment) {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($localized_property_node, $property_node, 'property-assignment');
|
2020-06-25 06:24:37 +02:00
|
|
|
} else {
|
2020-09-21 05:59:52 +02:00
|
|
|
$control_flow_graph->addPath($property_node, $localized_property_node, 'property-fetch');
|
2020-06-25 06:24:37 +02:00
|
|
|
}
|
2019-08-06 18:54:12 +02:00
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$type->parent_nodes[$localized_property_node->id] = $localized_property_node;
|
2019-08-06 18:54:12 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|