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;
|
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;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2019-06-04 06:32:19 +02:00
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
|
|
|
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\ParentNotFound;
|
|
|
|
use Psalm\Issue\PossiblyInvalidPropertyFetch;
|
|
|
|
use Psalm\Issue\PossiblyNullPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedClass;
|
2019-05-16 21:06:56 +02:00
|
|
|
use Psalm\Issue\UndefinedDocblockClass;
|
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;
|
|
|
|
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_reverse;
|
|
|
|
use function array_keys;
|
|
|
|
use function count;
|
|
|
|
use function explode;
|
2019-08-04 16:37:36 +02:00
|
|
|
use Psalm\Internal\Taint\TypeSource;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class PropertyFetchAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\PropertyFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeInstance(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
$prop_name = $stmt->name->name;
|
|
|
|
} elseif (isset($stmt->name->inferredType)
|
|
|
|
&& $stmt->name->inferredType->isSingleStringLiteral()
|
|
|
|
) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$prop_name = $stmt->name->inferredType->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
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$stmt_var_id = ExpressionAnalyzer::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
|
|
|
);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::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
|
|
|
);
|
|
|
|
|
|
|
|
$stmt_var_type = null;
|
2018-06-09 05:54:07 +02:00
|
|
|
$stmt->inferredType = null;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if ($var_id && $context->hasVariable($var_id, $statements_analyzer)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
|
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,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:32:19 +01:00
|
|
|
if ($stmt_var_id === '$this'
|
|
|
|
&& !$stmt->inferredType->initialized
|
|
|
|
&& $context->collect_initializations
|
|
|
|
&& isset($stmt->var->inferredType)
|
|
|
|
&& $stmt->var->inferredType->hasObjectType()
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
) {
|
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
|
|
|
|
$property_id = null;
|
|
|
|
|
|
|
|
foreach ($stmt->var->inferredType->getTypes() as $lhs_type_part) {
|
|
|
|
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
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UninitializedProperty(
|
2019-03-25 02:16:36 +01:00
|
|
|
'Cannot use uninitialized property ' . $var_id,
|
2019-02-13 19:32:19 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$var_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType->addType(new Type\Atomic\TNull);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:49:57 +02:00
|
|
|
if (isset($stmt->var->inferredType)
|
2018-01-14 18:09:40 +01:00
|
|
|
&& $stmt->var->inferredType->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
|
|
|
|
foreach ($stmt->var->inferredType->getTypes() as $lhs_type_part) {
|
|
|
|
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
|
|
|
|
2019-08-06 18:54:12 +02:00
|
|
|
self::processTaints($statements_analyzer, $stmt, $stmt->inferredType, $property_id);
|
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,
|
2019-03-01 14:57:10 +01:00
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2018-10-09 19:49:57 +02:00
|
|
|
$context->collect_references
|
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
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if ($stmt_var_id && $context->hasVariable($stmt_var_id, $statements_analyzer)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt_var_type = $context->vars_in_scope[$stmt_var_id];
|
|
|
|
} elseif (isset($stmt->var->inferredType)) {
|
|
|
|
$stmt_var_type = $stmt->var->inferredType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmt_var_type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
|
|
|
);
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($stmt_var_type->hasMixed()) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
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-12-08 19:18:55 +01:00
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
return null;
|
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
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt_var_type->isNullable() && !$stmt_var_type->ignore_nullable_issues && !$context->inside_isset) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullPropertyFetch(
|
|
|
|
'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-18 20:47:58 +02:00
|
|
|
foreach ($stmt_var_type->getTypes() as $type) {
|
|
|
|
if ($type instanceof Type\Atomic\TNamedObject) {
|
2019-04-27 23:38:24 +02:00
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
strtolower($type->value) . '::$',
|
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
|
|
|
);
|
2019-04-18 20:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_fetch_types = [];
|
|
|
|
$has_valid_fetch_type = false;
|
|
|
|
|
|
|
|
foreach ($stmt_var_type->getTypes() as $lhs_type_part) {
|
|
|
|
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(
|
|
|
|
$lhs_type_part->as->getTypes()
|
|
|
|
)[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) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
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])
|
|
|
|
) {
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes(
|
|
|
|
$lhs_type_part->properties[$prop_name],
|
|
|
|
$stmt->inferredType
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $lhs_type_part->properties[$prop_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|| in_array(strtolower($lhs_type_part->value), ['stdclass', 'simplexmlelement'], true)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
|
|
|
$stmt->inferredType = 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)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
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
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
$class_exists = false;
|
|
|
|
$interface_exists = false;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$codebase->classExists($lhs_type_part->value)) {
|
2018-02-01 06:50:01 +01:00
|
|
|
if ($codebase->interfaceExists($lhs_type_part->value)) {
|
2019-01-23 22:58:50 +01:00
|
|
|
$interface_exists = true;
|
2018-11-25 00:31:00 +01:00
|
|
|
$interface_storage = $codebase->classlike_storage_provider->get($lhs_type_part->value);
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
$override_property_visibility = $interface_storage->override_property_visibility;
|
2018-11-25 00:31:00 +01:00
|
|
|
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
if ($intersection_type instanceof TNamedObject
|
|
|
|
&& $codebase->classExists($intersection_type->value)
|
|
|
|
) {
|
|
|
|
$fq_class_name = $intersection_type->value;
|
|
|
|
$class_exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
2019-01-23 22:58:50 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
2018-11-25 00:31:00 +01:00
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
2019-01-23 22:58:50 +01:00
|
|
|
return null;
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$codebase->methodExists($fq_class_name . '::__set')) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-25 00:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
if (!$class_exists && !$interface_exists) {
|
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
|
|
|
}
|
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
return null;
|
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
|
|
|
}
|
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-11-25 00:31:00 +01:00
|
|
|
if ($codebase->methodExists($fq_class_name . '::__get')
|
2019-03-01 14:57:10 +01:00
|
|
|
&& (!$codebase->properties->propertyExists($property_id, false, $statements_analyzer, $context)
|
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
|
|
|
)
|
|
|
|
) {
|
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
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if (isset($class_storage->pseudo_property_get_types['$' . $prop_name])) {
|
|
|
|
$stmt->inferredType = clone $class_storage->pseudo_property_get_types['$' . $prop_name];
|
2019-08-06 18:54:12 +02:00
|
|
|
self::processTaints($statements_analyzer, $stmt, $stmt->inferredType, $property_id);
|
2018-05-09 02:25:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
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-05-23 18:59:09 +02:00
|
|
|
$stmt->inferredType = $fake_method_call->inferredType ?? Type::getMixed();
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_exists) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
if (!$codebase->properties->propertyExists(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2018-11-11 18:01:14 +01:00
|
|
|
$context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
|
2018-01-14 18:09:40 +01:00
|
|
|
)
|
|
|
|
) {
|
2019-01-31 23:48:48 +01:00
|
|
|
if ($fq_class_name !== $context->self
|
|
|
|
&& $context->self
|
|
|
|
&& $codebase->properties->propertyExists(
|
|
|
|
$context->self . '::$' . $prop_name,
|
2019-03-01 14:57:10 +01:00
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
2019-01-31 23:48:48 +01:00
|
|
|
$context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$property_id = $context->self . '::$' . $prop_name;
|
|
|
|
} else {
|
2019-02-13 20:04:10 +01:00
|
|
|
if ($context->inside_isset || $context->collect_initializations) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2018-05-09 18:54:31 +02:00
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $stmt->inferredType;
|
|
|
|
}
|
2018-05-09 18:54:31 +02:00
|
|
|
|
2019-01-31 23:48:48 +01:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
$declaring_property_class = (string) $codebase->properties->getDeclaringClassForProperty(
|
|
|
|
$property_id,
|
|
|
|
true
|
|
|
|
);
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-05-11 21:39:53 +02:00
|
|
|
if ($property_storage->psalm_internal && $context->self) {
|
2019-05-14 23:30:16 +02:00
|
|
|
if (! NamespaceAnalyzer::isWithin($context->self, $property_storage->psalm_internal)) {
|
2019-05-11 16:45:25 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalProperty(
|
2019-05-11 21:39:53 +02:00
|
|
|
$property_id . ' is marked internal to ' . $property_storage->psalm_internal,
|
2019-05-11 16:45:25 +02:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($property_storage->internal && $context->self) {
|
2019-05-14 23:50:21 +02:00
|
|
|
if (! NamespaceAnalyzer::nameSpaceRootsMatch($context->self, $declaring_property_class)) {
|
2019-03-01 14:57:10 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalProperty(
|
|
|
|
$property_id . ' is marked internal',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
2018-11-25 00:31:00 +01:00
|
|
|
'Property ' . $fq_class_name . '::$' . $prop_name
|
2018-04-17 18:16:25 +02:00
|
|
|
. ' does not have a declared type',
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$class_property_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
clone $class_property_type,
|
2018-01-26 19:51:00 +01:00
|
|
|
$declaring_property_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$declaring_property_class,
|
|
|
|
$declaring_class_storage->parent_class
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
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 ($class_storage->template_types) {
|
|
|
|
$class_template_params = [];
|
|
|
|
|
|
|
|
$reversed_class_template_types = array_reverse(array_keys($class_storage->template_types));
|
|
|
|
|
|
|
|
$provided_type_param_count = count($lhs_type_part->type_params);
|
|
|
|
|
|
|
|
foreach ($reversed_class_template_types as $i => $type_name) {
|
|
|
|
if (isset($lhs_type_part->type_params[$provided_type_param_count - 1 - $i])) {
|
|
|
|
$class_template_params[$type_name] =
|
|
|
|
(string)$lhs_type_part->type_params[$provided_type_param_count - 1 - $i];
|
|
|
|
} else {
|
|
|
|
$class_template_params[$type_name] = 'mixed';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$type_tokens = Type::tokenize((string)$class_property_type);
|
|
|
|
|
2019-06-28 18:29:39 +02:00
|
|
|
$new_type_tokens = [];
|
|
|
|
|
|
|
|
foreach ($type_tokens as $type_token_map) {
|
|
|
|
if (isset($class_template_params[$type_token_map[0]])) {
|
|
|
|
$tokened = Type::tokenize($class_template_params[$type_token_map[0]]);
|
|
|
|
foreach ($tokened as $new_t) {
|
|
|
|
$new_type_tokens[] = [$new_t[0], $type_token_map[1]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$new_type_tokens[] = $type_token_map;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 18:29:39 +02:00
|
|
|
$class_property_type = Type::parseTokens($new_type_tokens);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:54:12 +02:00
|
|
|
self::processTaints($statements_analyzer, $stmt, $class_property_type, $property_id);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($class_property_type, $stmt->inferredType);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $class_property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFilePath(),
|
2018-10-26 22:17:15 +02:00
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$context->vars_in_scope[$var_id] = isset($stmt->inferredType) ? $stmt->inferredType : Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:54:12 +02:00
|
|
|
private static function processTaints(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
Type\Union $type,
|
|
|
|
string $property_id
|
|
|
|
) : void {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
if ($codebase->taint) {
|
|
|
|
$method_source = new TypeSource(
|
|
|
|
$property_id,
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->name)
|
|
|
|
);
|
|
|
|
|
|
|
|
$type->sources = [$method_source];
|
|
|
|
|
2019-08-13 05:16:05 +02:00
|
|
|
if ($tainted_source = $codebase->taint->hasPreviousSource($method_source)) {
|
|
|
|
$type->tainted = $tainted_source->taint;
|
2019-08-06 18:54:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public static function analyzeStatic(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\StaticPropertyFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable ||
|
|
|
|
$stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
) {
|
|
|
|
// @todo check this
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = null;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if (count($stmt->class->parts) === 1
|
|
|
|
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
|
|
|
|
) {
|
|
|
|
if ($stmt->class->parts[0] === 'parent') {
|
2018-11-11 18:01:14 +01:00
|
|
|
$fq_class_name = $statements_analyzer->getParentFQCLN();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($fq_class_name === null) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ParentNotFound(
|
|
|
|
'Cannot check property fetch on parent as this class does not extend another',
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$fq_class_name = (string)$context->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
$aliases = $statements_analyzer->getAliases();
|
2018-10-27 05:04:38 +02:00
|
|
|
|
|
|
|
if ($context->calling_method_id
|
|
|
|
&& !$stmt->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
2019-04-16 22:07:48 +02:00
|
|
|
$codebase->file_reference_provider->addMethodReferenceToClassMember(
|
2018-10-27 05:04:38 +02:00
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
'use:' . $stmt->class->parts[0] . ':' . \md5($statements_analyzer->getFilePath())
|
2018-10-27 05:04:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->class,
|
2018-10-27 05:04:38 +02:00
|
|
|
$aliases
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->check_classes) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-01-14 18:09:40 +01:00
|
|
|
false
|
|
|
|
) !== true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 06:32:19 +02:00
|
|
|
if ($fq_class_name
|
|
|
|
&& $codebase->methods_to_move
|
|
|
|
&& $context->calling_method_id
|
|
|
|
&& isset($codebase->methods_to_move[strtolower($context->calling_method_id)])
|
|
|
|
) {
|
|
|
|
$destination_method_id = $codebase->methods_to_move[strtolower($context->calling_method_id)];
|
|
|
|
|
|
|
|
$codebase->classlikes->airliftClassLikeReference(
|
|
|
|
$fq_class_name,
|
|
|
|
explode('::', $destination_method_id)[0],
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
(int) $stmt->class->getAttribute('startFilePos'),
|
|
|
|
(int) $stmt->class->getAttribute('endFilePos') + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->class->inferredType = $fq_class_name ? new Type\Union([new TNamedObject($fq_class_name)]) : null;
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\VarLikeIdentifier) {
|
|
|
|
$prop_name = $stmt->name->name;
|
|
|
|
} elseif (isset($stmt->name->inferredType)
|
|
|
|
&& $stmt->name->inferredType->isSingleStringLiteral()
|
|
|
|
) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$prop_name = $stmt->name->inferredType->getSingleStringLiteral()->value;
|
2018-05-09 03:21:22 +02:00
|
|
|
} else {
|
|
|
|
$prop_name = null;
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if (!$prop_name) {
|
|
|
|
if ($fq_class_name) {
|
2019-04-27 23:38:24 +02:00
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
strtolower($fq_class_name) . '::$',
|
|
|
|
$context->calling_method_id ?: $statements_analyzer->getFileName()
|
|
|
|
);
|
2019-04-18 20:47:58 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if (!$fq_class_name
|
|
|
|
|| !$context->check_classes
|
|
|
|
|| !$context->check_variables
|
|
|
|
|| ExpressionAnalyzer::isMock($fq_class_name)
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
|
|
|
$stmt,
|
|
|
|
$context->self ?: $statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
2019-04-18 20:47:58 +02:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if ($var_id && $context->hasVariable($var_id, $statements_analyzer)) {
|
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
// log the appearance
|
|
|
|
$codebase->properties->propertyExists(
|
|
|
|
$property_id,
|
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2019-04-18 20:47:58 +02:00
|
|
|
&& isset($stmt->inferredType)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2019-04-18 20:47:58 +02:00
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$codebase->properties->propertyExists(
|
|
|
|
$property_id,
|
|
|
|
false,
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
$context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyFetch(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$property_id
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2019-04-18 20:47:58 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if (ClassLikeAnalyzer::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context,
|
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
|
|
|
$fq_class_name . '::$' . $prop_name,
|
|
|
|
true
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-06-04 06:32:19 +02:00
|
|
|
$declaring_property_id = strtolower((string) $declaring_property_class) . '::$' . $prop_name;
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($codebase->alter_code && $stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
$moved_class = $codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->class,
|
|
|
|
$fq_class_name,
|
|
|
|
$context->calling_method_id
|
|
|
|
);
|
2019-06-04 06:32:19 +02:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if (!$moved_class) {
|
|
|
|
foreach ($codebase->property_transforms as $original_pattern => $transformation) {
|
|
|
|
if ($declaring_property_id === $original_pattern) {
|
|
|
|
list($old_declaring_fq_class_name) = explode('::$', $declaring_property_id);
|
|
|
|
list($new_fq_class_name, $new_property_name) = explode('::$', $transformation);
|
|
|
|
|
|
|
|
$file_manipulations = [];
|
|
|
|
|
|
|
|
if (strtolower($new_fq_class_name) !== strtolower($old_declaring_fq_class_name)) {
|
|
|
|
$file_manipulations[] = new \Psalm\FileManipulation(
|
|
|
|
(int) $stmt->class->getAttribute('startFilePos'),
|
|
|
|
(int) $stmt->class->getAttribute('endFilePos') + 1,
|
|
|
|
Type::getStringFromFQCLN(
|
|
|
|
$new_fq_class_name,
|
|
|
|
$statements_analyzer->getNamespace(),
|
|
|
|
$statements_analyzer->getAliasedClassesFlipped(),
|
|
|
|
null
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_manipulations[] = new \Psalm\FileManipulation(
|
|
|
|
(int) $stmt->name->getAttribute('startFilePos'),
|
|
|
|
(int) $stmt->name->getAttribute('endFilePos') + 1,
|
|
|
|
'$' . $new_property_name
|
|
|
|
);
|
2019-06-04 06:32:19 +02:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 06:32:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get((string)$declaring_property_class);
|
|
|
|
$property = $class_storage->properties[$prop_name];
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-04-18 20:47:58 +02:00
|
|
|
if ($var_id) {
|
|
|
|
if ($property->type) {
|
|
|
|
$context->vars_in_scope[$var_id] = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
clone $property->type,
|
|
|
|
$declaring_property_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$declaring_property_class,
|
|
|
|
$class_storage->parent_class
|
2019-04-18 20:47:58 +02:00
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
2019-04-18 20:47:58 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$var_id];
|
|
|
|
|
|
|
|
if ($codebase->store_node_types
|
2019-07-01 15:55:39 +02:00
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
2019-04-18 20:47:58 +02:00
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-04-18 20:47:58 +02:00
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|