pubby
V2EX  ›  PHP

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

  •  
  •   pubby · Oct 14, 2016 · 5384 views
    This topic created in 3522 days ago, the information mentioned may be changed or developed.

    团队用的框架是 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 replies    2016-10-14 22:34:25 +08:00
    jswh
        1
    jswh  
       Oct 14, 2016   ❤️ 1
    ide-helper,只能这样,参考 [laravel 的 ide helper generator]( https://github.com/barryvdh/laravel-ide-helper)
    pubby
        2
    pubby  
    OP
       Oct 14, 2016
    @jswh 谢谢,第二个问题 有更好的解决办法吗?
    jswh
        3
    jswh  
       Oct 14, 2016
    @pubby 可以用注释搞定的就用注释呗。反正动态语言的补全套路就是 doc comments ,否则你只能自己写插件了,我相信还是能处理这个问题了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:15 · PVG 06:15 · LAX 15:15 · JFK 18:15
    ♥ Do have faith in what you're doing.