自动完成动态声明的方法

Autocomplete dynamic declared method

如何让我的 IDE 注册此方法正在返回参数的实例?

所以我可以这样做:

Class::models()->getModel('newModel')->newModelMethodHere()?

代码有效,但 PHPStorm 中的自动完成功能无效。

    /**
     * Returns the object of the model
     *
     * @var $this->_models[$model] $model
     * @param string object $model
     * @throws Exception
     * @return object
     */
    public function getModel($model)
    {
        if (array_key_exists($model, $this->_models) && class_exists($model) && is_object($this->_models[$model])
            && $this->_models[$model] instanceof $model) {
            if (is_a($this->_models[$model], $model)) {
                /* @var $this->_models[$model] $model */
                return ($this->_models[$model]);
            }
        }
        throw new Exception('Model ' . (string)$model . ' is not registered correctly.');
    }

你应该修复你的评论块。 return 应该阐明什么对象 returns.

 /**
 * Returns the object of the model
 * ...
 * @return ModelNameClass
 */

我不得不稍微更改我的代码,但我设法让它工作:

namespace PHPSTORM_META {
    $STATIC_METHOD_TYPES = array(
       \ClassName::getModel('') => [
           "" == "@",
       ],
   );
}

在我的 PHPStorm 项目根目录中的新 .phpstorm.meta.php 文件中。

方法 getModel() 可能 returns 不同类型的对象,具体取决于提供的参数。虽然这不是好的做法,但您可以使用一个简单的技巧让 PhpStorm 帮助您完成自动完成:将 getModel() 返回的值存储到一个变量中并使用 @var 注释来告诉PhpStorm 关于它的类型:

 /** @var newModel $model */
 $model = Class::models()->getModel('newModel');
 // Here PhpStorm will be able to help you with its auto-complete
 $model->newModelMethodHere()?