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 @@ -15,6 +15,7 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\Enum\EnumCaseObjectType;
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 @@ -58,6 +59,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 @@ -11,6 +11,7 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\Enum\EnumCaseObjectType;
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 @@ -52,6 +53,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 @@ -297,6 +297,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 @@ -222,6 +223,11 @@ public function getEnumCaseObject(): ?EnumCaseObjectType
return $this;
}

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

Comment on lines +226 to +230
Copy link
Contributor

Choose a reason for hiding this comment

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

from my experience enum-case can generate a separate class of bugs, therefore I think we should have a separate test for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand which test you expect since the bug was about class with hasProperty and an enum-case cannot have a property.

Copy link
Contributor

Choose a reason for hiding this comment

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

but it could have a method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might be wrong but since an enum is final method_exists calls will be either always true or always false on it and the HasMethodType won't be added.

I'm getting

enum MyEnum
{
	case CASE1;
	case CASE2;

	public function someMethod(): bool { return true; }
}

class HelloWorld
{
	public function withEnumCase(): void
	{
		$entity = MyEnum::CASE1;
		assertType('class-string<Bug4890PHP8\MyEnum>', get_class($entity));
		assert(method_exists($entity, 'someMethod'));
		assertType('class-string<Bug4890PHP8\MyEnum>', get_class($entity));
	}
}

Do you have a test in mind ?

Copy link
Contributor

@staabm staabm Feb 11, 2026

Choose a reason for hiding this comment

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

what about

enum MyEnum
{
	case CASE1;
	case CASE2;

	public function someMethod(): bool { return true; }
}

class HelloWorld
{
	public function withEnumCase(UnitEnum $entity): void
	{
		assertType('class-string<UnitEnum>', get_class($entity));
		assert(method_exists($entity, 'someMethod'));
		assertType('class-string<UnitEnum&hasMethod<someMethod>>', get_class($entity));
	}
}

$h = new HelloWorld();
$h->withEnumCase(MyEnum::CASE1);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I forgot bout the base enum classes... UnitEnum.

I added the test !

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);
}

Comment on lines +66 to +70
Copy link
Contributor

Choose a reason for hiding this comment

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

might also be worth testing with templates

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also added

}
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);
}

}
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,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 @@ -392,6 +392,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 @@ -114,6 +114,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 @@ -53,6 +53,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 @@ -22,6 +22,7 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -90,6 +91,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 @@ -919,6 +920,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 @@ -50,6 +50,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 @@
return $traverse($type);
}

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

Check warning on line 59 in src/Type/Php/GetClassDynamicReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $isObject = $type->isObject(); - if ($isObject->no()) { + if (!$isObject->yes()) { return new ConstantBooleanType(false); }

Check warning on line 59 in src/Type/Php/GetClassDynamicReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $isObject = $type->isObject(); - if ($isObject->no()) { + if (!$isObject->yes()) { return new ConstantBooleanType(false); }
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 @@ -198,6 +198,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 @@ -111,6 +111,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 @@ -89,6 +89,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 @@ -22,6 +22,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/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function getObjectClassNames(): array;
/** @return list<ClassReflection> */
public function getObjectClassReflections(): array;

/**
* Return class-string<Foo> for object type Foo.
*/
public function getClassStringType(): Type;
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to document what this is supposed to return similarly to how getClassStringObjectType and getObjectTypeOrClassStringObjectType are documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added Return class-string<Foo> for object type Foo. ; is it enough ?


/**
* Returns the object type for a class-string or literal class name string.
* For non-class-string types, returns ErrorType.
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
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4890-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // lint >= 8.0

namespace Bug4890Php8;

use function PHPStan\Testing\assertType;

enum MyEnum
{
case CASE1;
case CASE2;

public function someMethod(): bool { return true; }
}

class HelloWorld
{
public function withEnumCase(\UnitEnum $entity): void
{
assertType('class-string<UnitEnum>', get_class($entity));
assert(method_exists($entity, 'someMethod'));
assertType('class-string<UnitEnum&hasMethod(someMethod)>', get_class($entity));
}
}
Loading
Loading