'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?
E.g. in this 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.
Solution 1:[1]
Only manually for the moment, when calling that 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)
- Use "Inject language or reference" option
- Then choose "File Reference" entry there (just start typing to filter the list).
Hopefully they will implement the following tickets for a permanent solution:
- Using
#[Language]
PHP attribute at the function declaration: https://youtrack.jetbrains.com/issue/WI-56996 - Or in-place via PHPDoc-like comment (before the parameter when calling that function): https://youtrack.jetbrains.com/issue/WI-20028
Watch those tickets (star/vote/comment) to get notified on any progress (and hopefully speed it up by bringing dev's attention).
Solution 2:[2]
Like LazyOne stated, there is currently no way to declare a parameter as being a file reference.
However, you can get a more permanent File Reference "injection" by [mis]using __DIR__
.
PhpStorm considers a string mixed with the __DIR__
constant to be a file path:
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.
echo mix(basename(__DIR__ . '/slack_bot.php'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Anony Mous |