Skip to content

Fix get_class return type - #4456

Merged
ondrejmirtes merged 6 commits into
phpstan:2.1.xfrom
VincentLanglet:fix/4890
Feb 23, 2026
Merged

ondrejmirtes merged 6 commits into
phpstan:2.1.xfrom
VincentLanglet:fix/4890

Conversation

@VincentLanglet

Copy link
Copy Markdown
Contributor

Closes #2350
Closes phpstan/phpstan#4890

@ondrejmirtes ondrejmirtes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel like a proper solution would be a new Type method. Basically the opposite of getClassStringObjectType. That one goes from class-string<Foo> to Foo.

We need to go from object Foo to class-string<Foo>.

And personally I don't think there's anything wrong with class-string<object{foo: Bar}> because it'd allow for a precise type when doing new on it again.

@VincentLanglet
VincentLanglet marked this pull request as draft December 6, 2025 12:41
@VincentLanglet
VincentLanglet force-pushed the fix/4890 branch 2 times, most recently from 1ce9e04 to 5702d17 Compare December 6, 2025 13:36
@VincentLanglet

Copy link
Copy Markdown
Contributor Author

I feel like a proper solution would be a new Type method. Basically the opposite of getClassStringObjectType. That one goes from class-string<Foo> to Foo.

We need to go from object Foo to class-string<Foo>.

I introduced getClassStringType(), feel free to challenge the name :)

@VincentLanglet
VincentLanglet marked this pull request as ready for review December 6, 2025 13:58
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

Comment thread src/Type/Type.php
*/
public function getObjectClassReflections(): array;

public function getClassStringType(): Type;

Copy link
Copy Markdown
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
Copy Markdown
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 ?

Comment on lines +226 to +230
public function getClassStringType(): Type
{
return new GenericClassStringType(new ObjectType($this->getClassName()));
}

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 ?

@staabm staabm Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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 !

Comment on lines +66 to +70
public function getClassStringType(): Type
{
return new GenericClassStringType($this);
}

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also added

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR is so much better than my take in #2350

great job!

@VincentLanglet

Copy link
Copy Markdown
Contributor Author

Since you initially request changes, I need your approval @ondrejmirtes

@ondrejmirtes
ondrejmirtes merged commit cb33ec0 into phpstan:2.1.x Feb 23, 2026
631 of 641 checks passed
@ondrejmirtes

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

false positive due to method_exists call beforehand

4 participants