覆盖PHP文档块/方法注释,而不进行重载(PhpStorm自动完成) [英] Override PHP docblock/annotation of method without overloading (PhpStorm autocompletion)

查看:187
本文介绍了覆盖PHP文档块/方法注释,而不进行重载(PhpStorm自动完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关PhpStorm(以及可能的其他IDE)与PHP docblocks一起使用时自动完成行为的问题.

我必须在我的应用程序中对类进行分组.首先,有各种产品(汽车产品,食品产品等)的单独类,它们都是从BaseProduct继承的,而各个合同(汽车合同,FoodContract等)的对应类都是从BaseContract继承.

I have to groups of classes in my application. First there are individual classes for various products (CarProduct, FoodProduct etc.), all inheriting from BaseProduct, and the counterpart for individual contracts (CarContract, FoodContract etc.), all inheriting from BaseContract.

<?php

class BaseContract
{
    /** @var BaseProduct */
    private $product;

    /**
     * @return BaseProduct
     */
    public function getProduct()
    {
        return $this->product;
    }
}

现在我有一个 CarContract 的实例,我想获得一些 CarProduct 特定的信息:

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:

  1. 在子类中重载 getProduct(),仅使用更新的@return文档块
  2. 在可访问CarContract产品的任何地方添加/** @var CarProduct $product */
  1. Overload getProduct() in the subclass, just with updated @return docblocks
  2. 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?

推荐答案

PhpStorm确实不允许/不支持执行以下操作:在其他地方定义相同的命名类,并将其用作重写真实类定义的参考.您可以这样做..但是IDE会使用同一类的多个定义"发出警告,并且它可能会引入一些奇怪的行为/意外警告...

PhpStorm does not really allow/does not support doing something like: have the same named class defined elsewhere and just use it as a reference for overriding definitions of real class. You can do that .. but IDE will warn with "multiple definitions of the same class" and it may introduce some weird behaviour/unexpected warnings...

以下是要求此类功能的票证: https://youtrack.jetbrains.com /issue/WI-851 -观看(星号/投票/评论)以获取有关任何进度的通知.

Here is a ticket that ask for such feature: https://youtrack.jetbrains.com/issue/WI-851 -- watch it (star/vote/comment) to get notified on any progress.

您的选择是:您可以使用@var在本地(向局部变量)提供正确的类型提示-您已经知道了,这是您首先想到的:

Your options are: you can provide correct type hint locally (to local variable) using @var -- you already know it and that's first that you would think of:

<?php

/** @var \CarProduct $product */
$product = $carContract->getProduct();

$product->getSpeed();


另一种可能的方法:您可以尝试使用相同的方法,而不是覆盖实际的方法.. PHPDoc @method可以与您的代码一起使用:


Another possible way: instead of overriding actual method .. you can try doing the same but with @method PHPDoc -- will work with your code:

<?php
/**
 * My Car Product class
 * 
 * @method \CarProduct getProduct() Bla-bla optional description
 */
class CarContract extends BaseContract ...

这篇关于覆盖PHP文档块/方法注释,而不进行重载(PhpStorm自动完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆