Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PhpStorm警告说;预期为1级,获得2级“;当class2扩展class1时_Php_Phpstorm_Phpdoc_Type Hinting - Fatal编程技术网

PhpStorm警告说;预期为1级,获得2级“;当class2扩展class1时

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框架,它有一个模型类:
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 {}