都 2016 年了, PhpStorm 代码提示遇到__call()大家是怎么完美解决的?

2016-10-14 14:58:13 +08:00
 pubby

团队用的框架是 zf3 ,我们自己弄了一个库,做了很多辅助轮子来方便各个项目使用,包括很多 controller 的 plugin 和 view helper

这些都是 zf 框架通过 __call() 来使用的

那么写代码的时候怎么让 PhpStorm 识别这些魔术方法呢,总不能去改框架的代码

目前的方案是在自己的库里放了个 phpstorm.doc.php 来专门标记文档

看起来是这样的:

<?php
namespace Zend\View\Renderer {

    /**
     * Class PhpRenderer
     * @package Zend\View\Renderer
     * @method string foo()
     * @method string bar(string $arg)
     */
    class PhpRenderer {}

}

namespace Zend\Mvc\Controller {

    /**
     * Class AbstractActionController
     * @package Zend\Mvc\Controller
     * @method bool myMethod1()
     * @method bool myMethod2()
     */
    abstract class AbstractActionController {}

}

这样在项目 composer.json 引入自己的库后, PhpStorm 能识别到这些新增方法,缺点就是会有类重复定义的警告

另一个头痛的问题是那些 view helper.

zf 默认模板是 action.phtml 文件,这些模板本质上是被 PhpRenderer->render() include 进来的,如果要让 PhpStorm 识别这些 view helper ,需要在文件头注释一个

<?php /* @var $this \Zend\View\Renderer\PhpRenderer */ ?>

<p>
<?php $this->//此时自动提示会有我们的新增方法 ?>
</p>

这太麻烦了,我也不想弄个自定义模板在每个.phtml 文件头都插这样一坨奇怪的东西

大家有什么好的建议不? PhpStorm 有没有啥插件能做到让 .phtml 这种文件类型 自动预设一些 phpdoc 进去(但是文件里本身不可见)

4698 次点击
所在节点    PHP
3 条回复
jswh
2016-10-14 15:24:39 +08:00
ide-helper,只能这样,参考 [laravel 的 ide helper generator]( https://github.com/barryvdh/laravel-ide-helper)
pubby
2016-10-14 16:01:21 +08:00
@jswh 谢谢,第二个问题 有更好的解决办法吗?
jswh
2016-10-14 22:34:25 +08:00
@pubby 可以用注释搞定的就用注释呗。反正动态语言的补全套路就是 doc comments ,否则你只能自己写插件了,我相信还是能处理这个问题了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/312793

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX