Reputation: 253
Sometimes I have to trick PhpStorm to show me type hinting functions.
I am using Laravel 8 and using Str::replaceFirst()
function which "Search"es a "Subject" string and "Replace"s the subject. I find that if I use a variable, the type hinting does not work unless I precede it with an empty string.
See the image below. I repeat the Str::replaceFirst()
function, the first time with arguments consisting of an empty string followed by a variable, then the second time, only used the variable. notice in the second example, the type hinting disappears.
Is there any way around this? That is, I would like to not have to precede arguments with empty strings to have the type hinting show up.
Upvotes: 0
Views: 683
Reputation: 165148
I find that if I use a variable, the type hinting does not work unless I precede it with an empty string.
That's a default (and IMO the most optimal) behaviour: show such hints when a raw value is used (a string or a number) and hide them when a variable/field is used.
When you use the variable/field then its name should tell you what that is. If it does not do that then consider having a better (more suitable) name for it. You can even improve your $matches
usage that holds your RegEx matches (see the section at the end for that).
Str::replaceFirst('one', 'two', 'three');
Here it is hard to see what those 'one'
, 'two'
and 'three'
really mean. But in the code below it's all clear:
Str::replaceFirst($text, $replace, $subject);
ANYWAY: In JetBrains IDEs such hints are called "Inlay hints" and for PHP you can force the IDE to display such hints for ALL parameters:
Settings (Preferences on macOS)
Editor | Inlay Hints
PHP | Show parameter hints --> Show name for all arguments
A bit more on Inlay Hints here in PhpStorm docs: https://www.jetbrains.com/help/phpstorm/viewing-reference-information.html#inlay-hints
If you are using PHP 8 -- look at PHP's own Named Arguments:
Basically, you write the same hints yourself right there in the method call. The bonus is that you can even swap the order of the parameters and it will still work.
Str::replaceFirst(search: 'one', replace: 'two', subject: 'three');
// or even
Str::replaceFirst(subject: 'three', search: 'one', replace: 'two');
// vs the original (PHP 7.x and below)
Str::replaceFirst('one', 'two', 'three');
For older PHP -- you can use the following IDE functionality to see what the parameter is:
Invoke View | Parameter Info
(Ctrl + P here on Windows keymap) to see the quick info on the parameter: https://www.jetbrains.com/help/phpstorm/viewing-reference-information.html#view-parameter-info
Or even invoke View | Quick Documentation
(Ctrl + Q here on Windows keymap) to see the function documentation / PHPDoc comment for that function.
Better variable names for RegEx matches -- look into RegEx Named Captures:
Original code (using $matches[1]
to reference the found price):
$text = 'Price: €24';
preg_match('/Price: (?:£|€)(\d+)/u', $text, $matches);
echo $matches[1];
Improved code ($matches['price']
tells more than just $matches[1]
):
$text = 'Price: €24';
preg_match('/Price: (?<currency>£|€)(?<price>\d+)/u', $text, $matches);
echo $matches['price'];
You can read a bit more on this here: https://php.watch/articles/php-regex-readability#named-captures
Upvotes: 0