Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Type/Accessory/HasMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\CompoundType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -57,6 +58,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

private function getCanonicalMethodName(): string
{
return strtolower($this->methodName);
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Accessory/HasPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\CompoundType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -51,6 +52,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ public function isObject(): TrinaryLogic
return $this->objectType->isObject();
}

public function getClassStringType(): Type
{
return $this->objectType->getClassStringType();
}

public function isEnum(): TrinaryLogic
{
return $this->objectType->isEnum();
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Enum/EnumCaseObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -217,6 +218,11 @@ public function getEnumCases(): array
return [$this];
}

public function getClassStringType(): Type
{
return new GenericClassStringType(new ObjectType($this->getClassName()));
}

public function toPhpDocNode(): TypeNode
{
return new ConstTypeNode(
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Generic/TemplateMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public function toStrictMixedType(): TemplateStrictMixedType
);
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

}
5 changes: 5 additions & 0 deletions src/Type/Generic/TemplateObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function __construct(
$this->default = $default;
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if $this should be intersected with ObjectWithoutClassType here.

With the current implementation, generics.php assertions are not changed.

But we might prefer having

assertType(
		'class-string<object&T (function PHPStan\Generics\FunctionsAssertType\getClassOnTemplateType(), argument)>|false',
		get_class($a)
	);

over

assertType(
		'class-string<T (function PHPStan\Generics\FunctionsAssertType\getClassOnTemplateType(), argument)>|false',
		get_class($a)
	);

If later we're doing new on it. WDYT ?

}

}
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ public function isObject(): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isObject());
}

public function getClassStringType(): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->getClassStringType());
}

public function isEnum(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isEnum());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function getClassStringType(): Type
{
return new ClassStringType();
}

public function isEnum(): TrinaryLogic
{
if ($this->subtractedType !== null) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function getClassStringType(): Type
{
return new NeverType();
}

public function isEnum(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NonexistentParentClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function getClassStringType(): Type
{
return new ClassStringType();
}

public function isEnum(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
6 changes: 6 additions & 0 deletions src/Type/ObjectShapeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -89,6 +90,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

public function hasProperty(string $propertyName): TrinaryLogic
{
return $this->hasInstanceProperty($propertyName);
Expand Down
6 changes: 6 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Traits\MaybeIterableTypeTrait;
Expand Down Expand Up @@ -916,6 +917,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

public function isEnum(): TrinaryLogic
{
$classReflection = $this->getClassReflection();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getClassStringType(): Type
{
return new ClassStringType();
}

public function accepts(Type $type, bool $strictTypes): AcceptsResult
{
if ($type instanceof CompoundType) {
Expand Down
41 changes: 10 additions & 31 deletions src/Type/Php/GetClassDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -62,34 +55,20 @@ static function (Type $type, callable $traverse): Type {
return $traverse($type);
}

if ($type instanceof EnumCaseObjectType) {
return new GenericClassStringType(new ObjectType($type->getClassName()));
$isObject = $type->isObject();
if ($isObject->no()) {
return new ConstantBooleanType(false);
}

$objectClassNames = $type->getObjectClassNames();
if ($type instanceof TemplateType && $objectClassNames === []) {
if ($type instanceof ObjectWithoutClassType) {
return new GenericClassStringType($type);
}

return new UnionType([
new GenericClassStringType($type),
new ConstantBooleanType(false),
]);
} elseif ($type instanceof MixedType) {
return new UnionType([
new ClassStringType(),
new ConstantBooleanType(false),
]);
} elseif ($type instanceof StaticType) {
return new GenericClassStringType($type->getStaticObjectType());
} elseif ($objectClassNames !== []) {
return new GenericClassStringType($type);
} elseif ($type instanceof ObjectWithoutClassType) {
return new ClassStringType();
$classStringType = $type->getClassStringType();
if ($isObject->yes()) {
return $classStringType;
}

return new ConstantBooleanType(false);
return new UnionType([
$classStringType,
new ConstantBooleanType(false),
]);
},
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public function isObject(): TrinaryLogic
return $this->getStaticObjectType()->isObject();
}

public function getClassStringType(): Type
{
return $this->getStaticObjectType()->getClassStringType();
}

public function isEnum(): TrinaryLogic
{
return $this->getStaticObjectType()->isEnum();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StrictMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function getClassStringType(): Type
{
return new ErrorType();
}

public function isEnum(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function isObject(): TrinaryLogic
return $this->resolve()->isObject();
}

public function getClassStringType(): Type
{
return $this->resolve()->getClassStringType();
}

public function isEnum(): TrinaryLogic
{
return $this->resolve()->isEnum();
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Traits/MaybeObjectTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

Expand All @@ -30,6 +31,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function getClassStringType(): Type
{
return new ClassStringType();
}

public function isEnum(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/NonObjectTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function isObject(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function getClassStringType(): Type
{
return new ErrorType();
}

public function isEnum(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function getObjectClassNames(): array;
*/
public function getObjectClassReflections(): array;

public function getClassStringType(): Type;

/**
* Returns object type Foo for class-string<Foo> and 'Foo' (if Foo is a valid class).
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ public function isObject(): TrinaryLogic
return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isObject());
}

public function getClassStringType(): Type
{
return $this->unionTypes(static fn (Type $type): Type => $type->getClassStringType());
}

public function isEnum(): TrinaryLogic
{
return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isEnum());
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4890.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Bug4890;

use function PHPStan\Testing\assertType;

interface Proxy {}

class HelloWorld
{
public function update(object $entity): void
{
assertType('class-string', get_class($entity));
assert(method_exists($entity, 'getId'));
assertType('class-string<hasMethod(getId)>', get_class($entity));

if ($entity instanceof Proxy) {
assertType('class-string<Bug4890\Proxy&hasMethod(getId)>', get_class($entity));
}

$class = $entity instanceof Proxy
? get_parent_class($entity)
: get_class($entity);
assert(is_string($class));

}

public function updateProp(object $entity): void
{
assertType('class-string', get_class($entity));
assert(property_exists($entity, 'myProp'));
assertType('class-string<hasProperty(myProp)>', get_class($entity));

if ($entity instanceof Proxy) {
assertType('class-string<Bug4890\Proxy&hasProperty(myProp)>', get_class($entity));
}

$class = $entity instanceof Proxy
? get_parent_class($entity)
: get_class($entity);
assert(is_string($class));
}

/**
* @param object{foo: self, bar: int, baz?: string} $entity
*/
public function updateObjectShape($entity): void
{
assertType('class-string<object{foo: Bug4890\HelloWorld, bar: int, baz?: string}>', get_class($entity));
assert(property_exists($entity, 'foo'));
assertType('class-string<object{foo: Bug4890\HelloWorld, bar: int, baz?: string}>', get_class($entity));
}
}
Loading
Loading