V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pubby
V2EX  ›  PHP

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

  •  
  •   pubby · 2016-10-14 14:58:13 +08:00 · 4683 次点击
    这是一个创建于 2744 天前的主题,其中的信息可能已经有所发展或是发生改变。

    团队用的框架是 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 进去(但是文件里本身不可见)

    3 条回复    2016-10-14 22:34:25 +08:00
    jswh
        1
    jswh  
       2016-10-14 15:24:39 +08:00   ❤️ 1
    ide-helper,只能这样,参考 [laravel 的 ide helper generator]( https://github.com/barryvdh/laravel-ide-helper)
    pubby
        2
    pubby  
    OP
       2016-10-14 16:01:21 +08:00
    @jswh 谢谢,第二个问题 有更好的解决办法吗?
    jswh
        3
    jswh  
       2016-10-14 22:34:25 +08:00
    @pubby 可以用注释搞定的就用注释呗。反正动态语言的补全套路就是 doc comments ,否则你只能自己写插件了,我相信还是能处理这个问题了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1010 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:00 · PVG 03:00 · LAX 12:00 · JFK 15:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.