dongqiuwei8667 2016-09-01 01:23
浏览 33
已采纳

PHPDoc和方法匿名函数

I am trying to solve an annoying problem with PhpStorm's code validation when it comes to anonymous function. It does not see the passed object's methods.

The code snippet below relies on Predis and the pipeline method.

startCacheClient()

Instantiates and returns an instance of predis.

pipeline()

validates as it should, however

$pipe-set() and $pipe->expire()

Fail to validate and returns "Method 'Set' Not Found in" and "Method 'Expire' Not Found in"

    $this->i = 0;
    $this->startCacheClient()->pipeline(function($pipe) use($values, $jsonEncode, $keepAlive){

        foreach($values as $key => $currentValue){

            if($jsonEncode) {
                $currentValue = gzcompress(json_encode($currentValue), -1);
            }

            $pipe->set($key, $currentValue);
            $pipe->expire($key, $keepAlive);

            $this->i++;
        }
    });

How can I get PhpStorm through PHPDoc to understand that these methods are actually there and available. The code functions as expected, but validation notice is annoying.

  • 写回答

1条回答 默认 最新

  • dtiopy6088 2016-09-01 02:15
    关注

    If $pipe is an instance of \Predis\Pipeline\Pipeline, then you could type hint within the anonymous function declaration like:

    $this->startCacheClient()->pipeline(function(\Predis\ClientContextInterface $pipe) use($values, $jsonEncode, $keepAlive){
        /* DO STUFF HERE */
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料