Home > other >  PhpStorm - type hinting differences
PhpStorm - type hinting differences

Time:11-15

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

CodePudding user response:

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: enter image description here

  • Or even invoke View | Quick Documentation (Ctrl Q here on Windows keymap) to see the function documentation / PHPDoc comment for that function.

    enter image description here


  • Better variable names for RegEx matches -- look into RegEx Named Captures:

    • Related