Skip to content

Using class-string<Foo>&literal-string with new does not produce Foo #7374

@jlherren

Description

@jlherren

The intersection type class-string<SomeClass>&literal-string does not create an instance of SomeClass when used with new. Code:

<?php declare(strict_types = 1);

class HelloWorld
{
	/** @return class-string<self>&literal-string */
	public static function getClass(): string {
		return self::class;
	}
	
	public function build(): self {
		$class = self::getClass();
		return new $class();
	}
}

https://phpstan.org/r/3da883ab-d288-4ca1-9bea-163046e130e4

12	Method HelloWorld::build() should return HelloWorld but returns object.

I wouldn't normally use such a type, but it seems I will have to, to make Doctrine\ORM\QueryBuilder::from() happy, since it now insists on a literal-string.

Did PHPStan help you today? Did it make you happy in any way?

Every day! ❤ I hope that I can find some time to contribute again when things are less busy for me :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions