[英]PhpStorm - type hinting differences
我发现如果我使用一个变量,除非我在它前面加上一个空字符串,否则类型提示不起作用。
这是默认(和 IMO 最佳)行为:在使用原始值(字符串或数字)时显示此类提示,并在使用变量/字段时隐藏它们。
当您使用变量/字段时,它的名称应该告诉您那是什么。 如果它没有这样做,那么请考虑为它取一个更好(更合适)的名称。 您甚至可以改进保存 RegEx 匹配项的$matches
用法(请参阅末尾的部分)。
Str::replaceFirst('one', 'two', 'three');
在这里很难看出那些'one'
、 'two'
和'three'
真正含义。 但在下面的代码中很清楚:
Str::replaceFirst($text, $replace, $subject);
无论如何:在 JetBrains IDE 中,此类提示称为“嵌入提示” ,对于 PHP,您可以强制 IDE 为所有参数显示此类提示:
Settings (Preferences on macOS)
Editor | Inlay Hints
PHP | Show parameter hints --> Show name for all arguments
PhpStorm 文档中有关镶嵌提示的更多信息: https ://www.jetbrains.com/help/phpstorm/viewing-reference-information.html#inlay-hints
如果您使用的是 PHP 8 - 查看PHP 自己的命名参数:
基本上,您在方法调用中自己编写相同的提示。 好处是你甚至可以交换参数的顺序,它仍然可以工作。
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');
对于较旧的 PHP - 您可以使用以下IDE 功能来查看参数是什么:
调用View | Parameter Info
View | Parameter Info
(Windows 键盘映射上的Ctrl + P )以查看参数的快速信息: https : //www.jetbrains.com/help/phpstorm/viewing-reference-information.html#view-parameter-info
甚至调用View | Quick Documentation
View | Quick Documentation
(Windows 键盘映射上的Ctrl + Q )以查看该函数的函数文档/PHPDoc 注释。
RegEx 匹配的更好变量名称——查看RegEx Named Captures :
原始代码(使用$matches[1]
来引用找到的价格):
$text = 'Price: €24';
preg_match('/Price: (?:£|€)(\d+)/u', $text, $matches);
echo $matches[1];
改进的代码( $matches['price']
不仅仅是$matches[1]
):
$text = 'Price: €24';
preg_match('/Price: (?<currency>£|€)(?<price>\d+)/u', $text, $matches);
echo $matches['price'];
您可以在这里阅读更多内容:https ://php.watch/articles/php-regex-readability#named-captures
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.