简体   繁体   English

如何告诉 PhpStorm function 参数是文件路径

[英]How to tell PhpStorm that function argument is file path

In PhpStorm (and other JetBrains IDE), is it possible to make function attributes as file / resource path?在 PhpStorm(和其他 JetBrains IDE)中,是否可以将 function 属性作为文件/资源路径?

Eg in this function:例如在这个 function 中:

function mix($file): string
{
  // check mix maninfest for $file and return path from mix manifest
  return $fire_path_with_cachebuster;
}

I'd like PHP to suggest files in the project as I define $file attribute when calling mix function.我希望 PHP 在项目中建议文件,因为我在调用 mix function 时定义$file属性。

Only manually for the moment, when calling that function.暂时只能手动调用 function。 And it's a temp injection (for a session) so it's not convenient:这是一个临时注入(用于会话),所以不方便:

mix('')
  • Place caret inside the string parameter将插入符号放在字符串参数内
  • Use Alt + Enter (or via light bulb icon) to bring the Quick Fix / Intentions menu (on some Keymaps it might be different one)使用Alt + Enter (或通过灯泡图标)打开 Quick Fix / Intentions 菜单(在某些键盘映射上可能不同)
  • Use "Inject language or reference" option使用“注入语言或参考”选项
    在此处输入图像描述
  • Then choose "File Reference" entry there (just start typing to filter the list).然后在那里选择“文件参考”条目(只需开始输入以过滤列表)。 在此处输入图像描述

The result:结果:
在此处输入图像描述


Hopefully they will implement the following tickets for a permanent solution:希望他们将实施以下票证以获得永久解决方案:

Watch those tickets (star/vote/comment) to get notified on any progress (and hopefully speed it up by bringing dev's attention).观看这些票(星/投票/评论)以获得任何进展的通知(并希望通过引起开发人员的注意来加速它)。

Like LazyOne stated, there is currently no way to declare a parameter as being a file reference.正如LazyOne所说,目前无法将参数声明为文件引用。

However, you can get a more permanent File Reference "injection" by [mis]using __DIR__ .但是,您可以通过[错误]使用__DIR__获得更永久的文件引用“注入”。

PhpStorm considers a string mixed with the __DIR__ constant to be a file path: PhpStorm 将与__DIR__常量混合的字符串视为文件路径:

在此处输入图像描述

It isn't perfect as it depends on what directory you are currently located in. If you only want the filename passed to your method, you can wrap the string in basename , or handle that from within your method.它并不完美,因为它取决于您当前所在的目录。如果您只想将文件名传递给您的方法,您可以将字符串包装在basename中,或者从您的方法中处理它。

echo mix(basename(__DIR__ . '/slack_bot.php'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM