Код: Выделить всё
class Car
{
}
/**
* @template S of object
*/
interface BuilderInterface
{
/**
* @return S
*/
public function build(): object;
}
/**
* @implements BuilderInterface
*/
class CarBuilder implements BuilderInterface
{
public function build(): object
{
return new Car();
}
public function notAnInterfaceFunc(): void
{
}
}
/**
* @template T of BuilderInterface
* @param class-string $builderClass
* @return T
*/
function createBuilder(string $builderClass): BuilderInterface
{
return new $builderClass();
}
/**
* @template T of BuilderInterface
* @param class-string $builderClass
* @return BuilderInterface
*/
function createBuilder1(string $builderClass): BuilderInterface
{
return new $builderClass();
}
$builder = createBuilder(CarBuilder::class);
\PHPStan\dumpType($builder->build()); // proper return object is of type Car
$builder->notAnInterfaceFunc(); // the function is callable
$builder1 = createBuilder1(CarBuilder::class);
\PHPStan\dumpType($builder1->build()); // return object is 'object'
$builder1->notAnInterfaceFunc(); // is not callable
Чего я хочу:
- возвращает объект типа Автомобиль
Код: Выделить всё
$builder->build() - не вызывается
Код: Выделить всё
notAnInterfaceFunc()
Подробнее здесь: https://stackoverflow.com/questions/783 ... hp-phpstan
Мобильная версия