我正在尝试解决 PhpStorm 的代码验证在匿名函数方面的一个恼人的问题。它看不到传递的对象的方法。
下面的代码片段依赖于 Predis 和 pipeline 方法。
startCacheClient()
实例化并返回 predis 的实例。
pipeline()
然而,它应该被验证
$pipe-set() and $pipe->expire()
验证失败并返回“找不到方法‘Set’”和“找不到方法‘Expire’”
$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++;
}
});
如何通过 PHPDoc 获取 PhpStorm 以了解这些方法实际上存在且可用。代码按预期运行,但验证通知很烦人。
最佳答案
如果 $pipe
是 \Predis\Pipeline\Pipeline
的实例,那么您可以在匿名函数声明中键入提示,例如:
$this->startCacheClient()->pipeline(function(\Predis\ClientContextInterface $pipe) use($values, $jsonEncode, $keepAlive){
/* DO STUFF HERE */
});
关于PHPDoc 和方法匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261357/