PhpStorm警告说;预期为1级,获得2级“;当class2扩展class1时
我使用的是Phalcon框架,它有一个模型类:PhpStorm警告说;预期为1级,获得2级“;当class2扩展class1时,php,phpstorm,phpdoc,type-hinting,Php,Phpstorm,Phpdoc,Type Hinting,我使用的是Phalcon框架,它有一个模型类:Phalcon\Mvc\Model(从现在起只作为p\m\Model)。我已经定义了基本域,它扩展了该类,然后每个其他域都扩展了我的基本域,因此Phalcons模型类: Domain.php: class Domain extends \Phalcon\Mvc\Model { ... } DomainA.php: class DomainA extends Domain { ... } 然后我使用repository manage
Phalcon\Mvc\Model
(从现在起只作为p\m\Model
)。我已经定义了基本域,它扩展了该类,然后每个其他域都扩展了我的基本域,因此Phalcons模型类:
Domain.php:
class Domain extends \Phalcon\Mvc\Model
{
...
}
DomainA.php:
class DomainA extends Domain
{
...
}
然后我使用repository manager获取DomainA
模型的存储库。所有存储库都有相同的父级,类似于定义了方法find()的Domain
Repository.php:
class Repository
{
/**
* Will always return object of classes
* which extends Phalcon\Mvc\Model
*
* @return Phalcon\Mvc\Model
*/
public function find()
{
...
$domain::find();
}
}
RepositoryA.php:
class RepositoryA extends Repository
{
...
}
因此,RepositoryA
现在从其父级有方法find(),因为父级不确切知道他将返回什么,但知道所有返回的父级,所以它通过@return
进行类型提示
然后我有另一个类,它的方法只需要DomainA
对象,它也是p\M\Model
的父对象,我尝试将该类型的对象推到那里,因为从存储库返回的对象实际上是DomainA
对象,但存储库将其注释为P\M\Model
,因此PhpStorm用消息突出显示它,“预期的域,得到了Phalcon\Mvc\Model…”
我应该如何注释这类内容?在@return
中暗示所有域,如@return DomainA | DomainB | DomainC…
都不好,因为我们有数百个域,在函数中也期望父P\M\Model
是不好的,因为我们想确定它是唯一的DomainA
谢谢。尝试使用界面而不是基本模型。根据我的经验,有时PHPStorm会与这种复杂的类层次结构混淆。在我的程序中,我定义了一个接口并键入提示。如果您在类
RepositoryA
中重新定义find()
,则PHPStorm可以正确地检测类,然后仅使用@return DomainA
注释其实现。否则,在类RepositoryA
的docblock中将find()
声明为@method
,返回类型为DomainA
两种方法如下所示:
/**
* @method DomainA find() <-- use this when the method is inherited
* but not redefined in this class
*/
class RepositoryA extends Repository
{
/**
* @return DomainA <-- preferably use this
*/
public function find()
{
}
}
/**
*@method DomainA find()定义了一个@method
标记
语法
例子
Re:从我的照片上取的 但是当我需要基本模型具有非抽象方法时?然后我应该添加接口,但仍然扩展基础模型吗?您可以。您可以使用接口来创建一种开发应用程序的最佳实践,这样不仅可以使PHPStorm满意,还可以使类系统的结构形式化。我应该注意到,我还看到其他开发人员创建了空白接口来修复这个问题@GerardRoche的可能重复项这一个比它早一天,那么另一个不应该被认为是可能的重复项吗?
/**
* @method DomainA find() <-- use this when the method is inherited
* but not redefined in this class
*/
class RepositoryA extends Repository
{
/**
* @return DomainA <-- preferably use this
*/
public function find()
{
}
}
/**
* @property DomainA $member <-- it was declared as @var Domain $member
* in the base class
*/
@method [return type] [name]([type] [parameter], [...]) [description]
/**
* @method string getString()
* @method void setInteger(int $integer)
* @method setString(int $integer)
*/
class Child extends Parent
{
<...>
}
abstract class EntitySerializer {
/**
* @return Entity
*/
abstract public function getEntity();
}
/**
* @method RealEntity getEntity()
*/
abstract class RealEntitySerializer extends EntitySerializer {}
/**
* @method PseudoEntity getEntity()
*/
abstract class PseudoEntitySerializer extends EntitySerializer {}