Skip to content

Resolve static type in the typesystem, not in scope #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,6 @@ parameters:
count: 1
path: src/Testing/TestCase.php

-
message: "#^Cannot call method getActiveTemplateTypeMap\\(\\) on PHPStan\\\\Reflection\\\\ClassReflection\\|null\\.$#"
count: 2
path: src/Type/Generic/GenericObjectType.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
Expand Down
279 changes: 63 additions & 216 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ private function resolveType(Expr $node): Type
$uncertainty = false;

if ($node->class instanceof Node\Name) {
$classType = $this->resolveTypeByName($node->class);
$className = $this->resolveName($node->class);
$classType = new ObjectType($className);
} else {
$classType = $this->getType($node->class);
$classType = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use (&$uncertainty): Type {
Expand Down Expand Up @@ -1877,40 +1878,11 @@ private function resolveType(Expr $node): Type

if ($node instanceof MethodCall && $node->name instanceof Node\Identifier) {
$typeCallback = function () use ($node): Type {
$methodCalledOnType = $this->getType($node->var);
$methodName = $node->name->name;
$map = function (Type $type, callable $traverse) use ($methodName, $node): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
if ($type instanceof IntersectionType) {
$returnTypes = [];
foreach ($type->getTypes() as $innerType) {
$returnType = $this->methodCallReturnType(
$type,
$innerType,
$methodName,
$node
);
if ($returnType === null) {
continue;
}

$returnTypes[] = $returnType;
}
if (count($returnTypes) === 0) {
return new NeverType();
}
return TypeCombinator::intersect(...$returnTypes);
}
return $this->methodCallReturnType(
$type,
$type,
$methodName,
$node
) ?? new NeverType();
};
$returnType = TypeTraverser::map($methodCalledOnType, $map);
$returnType = $this->methodCallReturnType(
$this->getType($node->var),
$node->name->name,
$node
) ?? new NeverType();
if ($returnType instanceof NeverType && !$returnType->isExplicit()) {
return new ErrorType();
}
Expand Down Expand Up @@ -1939,39 +1911,11 @@ private function resolveType(Expr $node): Type
}
}

$methodName = $node->name->toString();
$map = function (Type $type, callable $traverse) use ($methodName, $node): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
if ($type instanceof IntersectionType) {
$returnTypes = [];
foreach ($type->getTypes() as $innerType) {
$returnType = $this->methodCallReturnType(
$type,
$innerType,
$methodName,
$node
);
if ($returnType === null) {
continue;
}

$returnTypes[] = $returnType;
}
if (count($returnTypes) === 0) {
return new NeverType();
}
return TypeCombinator::intersect(...$returnTypes);
}
return $this->methodCallReturnType(
$type,
$type,
$methodName,
$node
) ?? new NeverType();
};
$returnType = TypeTraverser::map($staticMethodCalledOnType, $map);
$returnType = $this->methodCallReturnType(
$staticMethodCalledOnType,
$node->name->toString(),
$node
) ?? new NeverType();
if ($returnType instanceof NeverType && !$returnType->isExplicit()) {
return new ErrorType();
}
Expand All @@ -1988,39 +1932,11 @@ private function resolveType(Expr $node): Type

if ($node instanceof PropertyFetch && $node->name instanceof Node\Identifier) {
$typeCallback = function () use ($node): Type {
$propertyFetchedOnType = $this->getType($node->var);
$propertyName = $node->name->name;
$map = function (Type $type, callable $traverse) use ($propertyName, $node): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
if ($type instanceof IntersectionType) {
$returnTypes = [];
foreach ($type->getTypes() as $innerType) {
$returnType = $this->propertyFetchType(
$innerType,
$propertyName,
$node
);
if ($returnType === null) {
continue;
}

$returnTypes[] = $returnType;
}
if (count($returnTypes) === 0) {
return new NeverType();
}
return TypeCombinator::intersect(...$returnTypes);
}
return $this->propertyFetchType(
$type,
$propertyName,
$node
) ?? new NeverType();
};

$returnType = TypeTraverser::map($propertyFetchedOnType, $map);
$returnType = $this->propertyFetchType(
$this->getType($node->var),
$node->name->name,
$node
) ?? new NeverType();
if ($returnType instanceof NeverType) {
return new ErrorType();
}
Expand Down Expand Up @@ -2052,38 +1968,11 @@ private function resolveType(Expr $node): Type
}
}

$staticPropertyName = $node->name->toString();
$map = function (Type $type, callable $traverse) use ($staticPropertyName, $node): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
if ($type instanceof IntersectionType) {
$returnTypes = [];
foreach ($type->getTypes() as $innerType) {
$returnType = $this->propertyFetchType(
$innerType,
$staticPropertyName,
$node
);
if ($returnType === null) {
continue;
}

$returnTypes[] = $returnType;
}
if (count($returnTypes) === 0) {
return new NeverType();
}
return TypeCombinator::intersect(...$returnTypes);
}
return $this->propertyFetchType(
$type,
$staticPropertyName,
$node
) ?? new NeverType();
};

$returnType = TypeTraverser::map($staticPropertyFetchedOnType, $map);
$returnType = $this->propertyFetchType(
$staticPropertyFetchedOnType,
$node->name->toString(),
$node
) ?? new NeverType();
if ($returnType instanceof NeverType) {
return new ErrorType();
}
Expand Down Expand Up @@ -4713,32 +4602,46 @@ private function getTypeToInstantiateForNew(Type $type): Type
}

/**
* @param \PHPStan\Type\Type $calledOnType
* @param \PHPStan\Type\Type $typeWithMethod
* @param string $methodName
* @param MethodCall|\PhpParser\Node\Expr\StaticCall $methodCall
* @return \PHPStan\Type\Type|null
*/
private function methodCallReturnType(Type $calledOnType, Type $typeWithMethod, string $methodName, Expr $methodCall): ?Type
private function methodCallReturnType(Type $typeWithMethod, string $methodName, Expr $methodCall): ?Type
{
if ($typeWithMethod instanceof UnionType) {
$newTypes = [];
foreach ($typeWithMethod->getTypes() as $innerType) {
if (!$innerType->hasMethod($methodName)->yes()) {
continue;
}

$newTypes[] = $innerType;
}
if (count($newTypes) === 0) {
return null;
}
$typeWithMethod = TypeCombinator::union(...$newTypes);
}

if (!$typeWithMethod->hasMethod($methodName)->yes()) {
return null;
}

$methodReflection = $typeWithMethod->getMethod($methodName, $this);

$resolvedTypes = [];
if ($typeWithMethod instanceof TypeWithClassName) {
foreach (TypeUtils::getDirectClassNames($typeWithMethod) as $className) {
if ($methodCall instanceof MethodCall) {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicMethodReturnTypeExtensionsForClass($typeWithMethod->getClassName()) as $dynamicMethodReturnTypeExtension) {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicMethodReturnTypeExtensionsForClass($className) as $dynamicMethodReturnTypeExtension) {
if (!$dynamicMethodReturnTypeExtension->isMethodSupported($methodReflection)) {
continue;
}

$resolvedTypes[] = $dynamicMethodReturnTypeExtension->getTypeFromMethodCall($methodReflection, $methodCall, $this);
}
} else {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicStaticMethodReturnTypeExtensionsForClass($typeWithMethod->getClassName()) as $dynamicStaticMethodReturnTypeExtension) {
foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicStaticMethodReturnTypeExtensionsForClass($className) as $dynamicStaticMethodReturnTypeExtension) {
if (!$dynamicStaticMethodReturnTypeExtension->isStaticMethodSupported($methodReflection)) {
continue;
}
Expand All @@ -4749,49 +4652,14 @@ private function methodCallReturnType(Type $calledOnType, Type $typeWithMethod,
}

if (count($resolvedTypes) > 0) {
$methodReturnType = TypeCombinator::union(...$resolvedTypes);
} else {
$methodReturnType = ParametersAcceptorSelector::selectFromArgs(
$this,
$methodCall->args,
$methodReflection->getVariants()
)->getReturnType();
}

if ($methodCall instanceof MethodCall) {
$calledOnThis = $calledOnType instanceof StaticType && $this->isInClass();
} else {
if (!$methodCall->class instanceof Name) {
$calledOnThis = false;
} else {
$calledOnThis = in_array(strtolower($methodCall->class->toString()), ['self', 'static', 'parent'], true) && $this->isInClass();
}
return TypeCombinator::union(...$resolvedTypes);
}

$transformedCalledOnType = TypeTraverser::map($calledOnType, function (Type $type, callable $traverse) use ($calledOnThis): Type {
if ($type instanceof StaticType) {
if ($calledOnThis && $this->isInClass()) {
return $traverse($type->changeBaseClass($this->getClassReflection()));
}
if ($this->isInClass()) {
return $traverse($type->changeBaseClass($this->getClassReflection())->getStaticObjectType());
}
}

return $traverse($type);
});

return TypeTraverser::map($methodReturnType, function (Type $returnType, callable $traverse) use ($transformedCalledOnType, $calledOnThis): Type {
if ($returnType instanceof StaticType) {
if ($calledOnThis && $this->isInClass()) {
return $traverse($returnType->changeBaseClass($this->getClassReflection()));
}

return $traverse($transformedCalledOnType);
}

return $traverse($returnType);
});
return ParametersAcceptorSelector::selectFromArgs(
$this,
$methodCall->args,
$methodReflection->getVariants()
)->getReturnType();
}

/**
Expand All @@ -4802,52 +4670,31 @@ private function methodCallReturnType(Type $calledOnType, Type $typeWithMethod,
*/
private function propertyFetchType(Type $fetchedOnType, string $propertyName, Expr $propertyFetch): ?Type
{
if ($fetchedOnType instanceof UnionType) {
$newTypes = [];
foreach ($fetchedOnType->getTypes() as $innerType) {
if (!$innerType->hasProperty($propertyName)->yes()) {
continue;
}

$newTypes[] = $innerType;
}
if (count($newTypes) === 0) {
return null;
}
$fetchedOnType = TypeCombinator::union(...$newTypes);
}
if (!$fetchedOnType->hasProperty($propertyName)->yes()) {
return null;
}

$propertyReflection = $fetchedOnType->getProperty($propertyName, $this);

if ($this->isInExpressionAssign($propertyFetch)) {
$propertyType = $propertyReflection->getWritableType();
} else {
$propertyType = $propertyReflection->getReadableType();
return $propertyReflection->getWritableType();
}

if ($propertyFetch instanceof PropertyFetch) {
$fetchedOnThis = $fetchedOnType instanceof StaticType && $this->isInClass();
} else {
if (!$propertyFetch->class instanceof Name) {
$fetchedOnThis = false;
} else {
$fetchedOnThis = in_array(strtolower($propertyFetch->class->toString()), ['self', 'static', 'parent'], true) && $this->isInClass();
}
}

$transformedFetchedOnType = TypeTraverser::map($fetchedOnType, function (Type $type, callable $traverse) use ($fetchedOnThis): Type {
if ($type instanceof StaticType) {
if ($fetchedOnThis && $this->isInClass()) {
return $traverse($type->changeBaseClass($this->getClassReflection()));
}
if ($this->isInClass()) {
return $traverse($type->changeBaseClass($this->getClassReflection())->getStaticObjectType());
}
}

return $traverse($type);
});

return TypeTraverser::map($propertyType, function (Type $propertyType, callable $traverse) use ($transformedFetchedOnType, $fetchedOnThis): Type {
if ($propertyType instanceof StaticType) {
if ($fetchedOnThis && $this->isInClass()) {
return $traverse($propertyType->changeBaseClass($this->getClassReflection()));
}

return $traverse($transformedFetchedOnType);
}

return $traverse($propertyType);
});
return $propertyReflection->getReadableType();
}

}
6 changes: 5 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ private function processStmtNode(
$classReflection = $scope->getClassReflection();
$phpDocReturnType = TypeTraverser::map($phpDocReturnType, static function (Type $type, callable $traverse) use ($classReflection): Type {
if ($type instanceof StaticType) {
return $traverse($type->changeBaseClass($classReflection));
$changedType = $type->changeBaseClass($classReflection);
if ($classReflection->isFinal()) {
$changedType = $changedType->getStaticObjectType();
}
return $traverse($changedType);
}

return $traverse($type);
Expand Down
Loading