简体   繁体   English

PhpStorm - 类型提示差异

[英]PhpStorm - type hinting differences

Sometimes I have to trick PhpStorm to show me type hinting functions.有时我不得不欺骗 PhpStorm 向我展示类型提示功能。

I am using Laravel 8 and using Str::replaceFirst() function which "Search"es a "Subject" string and "Replace"s the subject.我正在使用 Laravel 8 并使用Str::replaceFirst() function ,其中“搜索”是“主题”字符串,而“替换”是主题。 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.我重复Str::replaceFirst() function,第一次用 arguments 由一个空字符串和一个变量组成,然后第二次,只使用变量。 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.也就是说,我不想在 arguments 之前使用空字符串来显示类型提示。

在此处输入图像描述

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.这是默认(和 IMO 最佳)行为:在使用原始值(字符串或数字)时显示此类提示,并在使用变量/字段时隐藏它们。

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).您甚至可以改进保存 RegEx 匹配项的$matches用法(请参阅末尾的部分)。

Str::replaceFirst('one', 'two', 'three');

Here it is hard to see what those 'one' , 'two' and 'three' really mean.在这里很难看出那些'one''two''three'真正含义。 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:无论如何:在 JetBrains IDE 中,此类提示称为“嵌入提示” ,对于 PHP,您可以强制 IDE 为所有参数显示此类提示:

  • 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 PhpStorm 文档中有关镶嵌提示的更多信息: 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 :如果您使用的是 PHP 8 - 查看PHP 自己的命名参数

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:对于较旧的 PHP - 您可以使用以下IDE 功能来查看参数是什么:


Better variable names for RegEx matches -- look into RegEx Named Captures : RegEx 匹配的更好变量名称——查看RegEx Named Captures

Original code (using $matches[1] to reference the found price):原始代码(使用$matches[1]来引用找到的价格):

$text = 'Price: €24';
preg_match('/Price: (?:£|€)(\d+)/u', $text, $matches);
echo $matches[1];

Improved code ( $matches['price'] tells more than just $matches[1] ):改进的代码( $matches['price']不仅仅是$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您可以在这里阅读更多内容:https ://php.watch/articles/php-regex-readability#named-captures

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

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