Robert Bryan Davis
Robert Bryan Davis

Reputation: 253

PhpStorm - type hinting differences

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.

enter image description here

Upvotes: 0

Views: 683

Answers (2)

LazyOne
LazyOne

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

enter image description here

A bit more on Inlay Hints here in PhpStorm docs: https://www.jetbrains.com/help/phpstorm/viewing-reference-information.html#inlay-hints


Other than that

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:


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

Related Questions