2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements\Expression\Fetch;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
2018-06-08 17:08:57 +02:00
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\DeprecatedProperty;
|
|
|
|
use Psalm\Issue\InvalidPropertyFetch;
|
|
|
|
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;
|
|
|
|
use Psalm\Issue\UndefinedPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyFetch;
|
|
|
|
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;
|
|
|
|
|
|
|
|
class PropertyFetchChecker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\PropertyFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeInstance(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\PropertyFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ExpressionChecker::analyze($statements_checker, $stmt->var, $context) === false) {
|
|
|
|
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-01-14 18:09:40 +01:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-04-06 22:23:10 +02:00
|
|
|
$stmt_var_id = ExpressionChecker::getArrayVarId(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->var,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
2018-04-06 22:23:10 +02:00
|
|
|
$var_id = ExpressionChecker::getArrayVarId(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
$stmt_var_type = null;
|
2018-06-09 05:54:07 +02:00
|
|
|
$stmt->inferredType = null;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
if ($var_id && $context->hasVariable($var_id, $statements_checker)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
|
2018-05-30 22:19:18 +02:00
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_checker->getFilePath());
|
2018-04-20 16:52:23 +02:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$codebase->properties->propertyExists(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
2018-09-26 00:37:24 +02:00
|
|
|
$context->calling_method_id,
|
2018-10-09 19:49:57 +02:00
|
|
|
$context->collect_references
|
|
|
|
? new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
: null
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
if ($codebase->server_mode) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
if ($stmt_var_id && $context->hasVariable($stmt_var_id, $statements_checker)) {
|
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,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isEmpty()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyFetch(
|
|
|
|
'Cannot fetch property on empty var ' . $stmt_var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_type->isMixed()) {
|
2018-05-30 22:19:18 +02:00
|
|
|
$codebase->analyzer->incrementMixedCount($statements_checker->getFilePath());
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyFetch(
|
|
|
|
'Cannot fetch property on mixed var ' . $stmt_var_id,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:19:18 +02:00
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_checker->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,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if (!$prop_name) {
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ExpressionChecker::isMock($lhs_type_part->value)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
if (!$codebase->classExists($lhs_type_part->value)) {
|
|
|
|
if ($codebase->interfaceExists($lhs_type_part->value)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot get properties of undefined class ' . $lhs_type_part->value,
|
2018-03-21 15:17:57 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$lhs_type_part->value
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
$property_id = $lhs_type_part->value . '::$' . $prop_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-08 17:08:57 +02:00
|
|
|
$statements_checker_source = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if ($codebase->methodExists($lhs_type_part->value . '::__get')
|
|
|
|
&& (!$statements_checker_source instanceof FunctionLikeChecker
|
|
|
|
|| $statements_checker_source->getMethodId() !== $lhs_type_part->value . '::__get')
|
2018-05-09 02:25:39 +02:00
|
|
|
&& (!$context->self || !$codebase->classExtends($context->self, $lhs_type_part->value))
|
|
|
|
&& (!$codebase->properties->propertyExists($property_id)
|
2018-06-08 17:08:57 +02:00
|
|
|
|| ($stmt_var_id !== '$this'
|
|
|
|
&& $lhs_type_part->value !== $context->self
|
|
|
|
&& ClassLikeChecker::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker_source,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
false
|
|
|
|
) !== true)
|
2018-05-09 02:25:39 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$lhs_type_part);
|
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];
|
2018-05-09 02:25:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-05-09 02:25:39 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
/*
|
|
|
|
* If we have an explicit list of all allowed magic properties on the class, and we're
|
|
|
|
* not in that list, fall through
|
|
|
|
*/
|
|
|
|
if (!$class_storage->sealed_properties) {
|
|
|
|
continue;
|
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,
|
2018-09-26 00:37:24 +02:00
|
|
|
$context->calling_method_id,
|
2018-01-14 18:09:40 +01:00
|
|
|
$context->collect_references ? new CodeLocation($statements_checker->getSource(), $stmt) : null
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if ($context->inside_isset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt_var_id === '$this') {
|
|
|
|
if ($context->collect_mutations) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyFetch(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2018-05-11 06:07:41 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2018-05-09 18:54:31 +02:00
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyFetch(
|
|
|
|
'Instance property ' . $property_id . ' is not defined',
|
2018-05-11 06:07:41 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
2018-05-09 18:54:31 +02:00
|
|
|
// fall through
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:54:31 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $stmt->inferredType;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassLikeChecker::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty($property_id);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$declaring_class_storage = $project_checker->classlike_storage_provider->get(
|
|
|
|
(string)$declaring_property_class
|
|
|
|
);
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
$property_storage = $declaring_class_storage->properties[$prop_name];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($property_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedProperty(
|
|
|
|
$property_id . ' is marked deprecated',
|
2018-05-11 06:07:41 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = $property_storage->type;
|
|
|
|
|
|
|
|
if ($class_property_type === false) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
2018-05-09 03:21:22 +02:00
|
|
|
'Property ' . $lhs_type_part->value . '::$' . $prop_name
|
2018-04-17 18:16:25 +02:00
|
|
|
. ' does not have a declared type',
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::getMixed();
|
|
|
|
} else {
|
|
|
|
$class_property_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
clone $class_property_type,
|
2018-01-26 19:51:00 +01:00
|
|
|
$declaring_property_class,
|
2018-01-14 18:09:40 +01:00
|
|
|
$declaring_property_class
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($lhs_type_part->value);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
foreach ($type_tokens as &$type_token) {
|
|
|
|
if (isset($class_template_params[$type_token])) {
|
|
|
|
$type_token = $class_template_params[$type_token];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_property_type = Type::parseString(implode('', $type_tokens));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($class_property_type, $stmt->inferredType);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = $class_property_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$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,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyFetch(
|
|
|
|
'Cannot fetch property on non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = isset($stmt->inferredType) ? $stmt->inferredType : Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public static function analyzeStatic(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\StaticPropertyFetch $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable ||
|
|
|
|
$stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
) {
|
|
|
|
// @todo check this
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = null;
|
|
|
|
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
2018-02-09 00:14:28 +01:00
|
|
|
$codebase = $project_checker->codebase;
|
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') {
|
|
|
|
$fq_class_name = $statements_checker->getParentFQCLN();
|
|
|
|
|
|
|
|
if ($fq_class_name === null) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ParentNotFound(
|
|
|
|
'Cannot check property fetch on parent as this class does not extend another',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$fq_class_name = (string)$context->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->isPhantomClass($fq_class_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-27 05:04:38 +02:00
|
|
|
$aliases = $statements_checker->getAliases();
|
|
|
|
|
|
|
|
if ($context->calling_method_id
|
|
|
|
&& !$stmt->class instanceof PhpParser\Node\Name\FullyQualified
|
|
|
|
) {
|
|
|
|
$codebase->file_reference_provider->addReferenceToClassMethod(
|
|
|
|
$context->calling_method_id,
|
|
|
|
'use:' . $stmt->class->parts[0] . ':' . \md5($statements_checker->getFilePath())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
|
|
|
$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) {
|
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
|
|
|
$statements_checker,
|
|
|
|
$fq_class_name,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
false
|
|
|
|
) !== true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($fq_class_name &&
|
|
|
|
$context->check_classes &&
|
|
|
|
$context->check_variables &&
|
2018-05-09 03:21:22 +02:00
|
|
|
$prop_name &&
|
2018-01-14 18:09:40 +01:00
|
|
|
!ExpressionChecker::isMock($fq_class_name)
|
|
|
|
) {
|
|
|
|
$var_id = ExpressionChecker::getVarId(
|
|
|
|
$stmt,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
$property_id = $fq_class_name . '::$' . $prop_name;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode) {
|
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
$property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
if ($var_id && $context->hasVariable($var_id, $statements_checker)) {
|
2018-01-14 18:09:40 +01:00
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
// log the appearance
|
2018-02-09 00:14:28 +01:00
|
|
|
$codebase->properties->propertyExists(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
2018-09-26 00:37:24 +02:00
|
|
|
$context->calling_method_id,
|
2018-01-14 18:09:40 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
&& isset($stmt->inferredType)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
if (!$codebase->properties->propertyExists(
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_id,
|
2018-09-26 00:37:24 +02:00
|
|
|
$context->calling_method_id,
|
2018-01-14 18:09:40 +01:00
|
|
|
$context->collect_references ? new CodeLocation($statements_checker->getSource(), $stmt) : null
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyFetch(
|
|
|
|
'Static property ' . $property_id . ' is not defined',
|
2018-05-11 06:07:41 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$property_id
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassLikeChecker::checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$context->self,
|
|
|
|
$statements_checker->getSource(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
2018-05-09 03:21:22 +02:00
|
|
|
$fq_class_name . '::$' . $prop_name
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get((string)$declaring_property_class);
|
2018-05-09 03:21:22 +02:00
|
|
|
$property = $class_storage->properties[$prop_name];
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = $property->type
|
|
|
|
? clone $property->type
|
|
|
|
: Type::getMixed();
|
|
|
|
|
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$var_id];
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
if ($codebase->server_mode
|
|
|
|
&& (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->addNodeType(
|
|
|
|
$statements_checker->getFilePath(),
|
|
|
|
$stmt->name,
|
|
|
|
(string) $stmt->inferredType
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|