这是一个关于 PhpStorm(可能还有其他 IDE)中的自动完成行为与 PHP 文档块结合的问题。
我必须在我的应用程序中分组类。首先,各种产品(CarProduct、FoodProduct 等)都有各自的类,它们都继承自 BaseProduct,而个别合同的对应物(CarContract、FoodContract 等)都继承自 BaseContract。
<?php
class BaseContract
{
/** @var BaseProduct */
private $product;
/**
* @return BaseProduct
*/
public function getProduct()
{
return $this->product;
}
}
Now I have an instance of CarContract, and I wanna get some CarProduct specific information:
<?php
/* PhpStorm thinks, this is BaseProduct */
$product = $carContract->getProduct();
/* hence, getSpeed() is not available for PhpStorm */
$product->getSpeed();
The autocompletion is not working as I like. There are two workarounds for this, but both are not nice:
- Overload getProduct() in the subclass, just with updated
@return
docblocks - Add
/** @var CarProduct $product */
everywhere, where I access the product of a CarContract
Is there a "usual" way to solve something like this, or are my workarounds the only solutions?