Skip to content

Conversation

@VincentLanglet
Copy link
Contributor

Closes #2350
Closes phpstan/phpstan#4890

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

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

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 ?

@VincentLanglet
Copy link
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
Collaborator

This pull request has been marked as ready for review.

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

3 participants