脚本宝典收集整理的这篇文章主要介绍了phpstorm生成类型为提示的setter,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
但是,即使PHPstorm知道变量的类型/类,它也不会在参数列表中插入类型提示.
如何使PHPstorm生成带有类型提示的setter,但仅适用于类型hintable类型?
示例类
class Codegenerationtest { /* @VAR \DateTimeinterface */ PRivate $date; /* @var int */ private $num; }
/** * @param DateTimeInterface $date */ public function setDate(DateTimeInterface $date) { $this->date = $date; } /** * @param int $num */ public function setNum($num) { $this->num = $num; }
setNum是正确的,但生成的setDate缺少参数的类型提示:
/** * @param DateTimeInterface $date */ public function setDate($date) { $this->date = $date; }
打开PHPStorm的首选项和“文件和代码模板”菜单,在“代码”选项卡下有一个名为“PHP Setter Method”的选项.修改它看起来像这样:
#set($tyPEHintText = "$TYPE_HINT ") ## First we check against a blacklist of primITive and other common types used in documentation. #set($nonTypeHintableTypes = ["","string","int","mixed","number","void","object","real","double","float","resource","null","bool","boolean"]) #foreach($nonTypeHintableType in $nonTypeHintableTypes) #if ($nonTypeHintableType == $TYPE_HINT) #set($typeHintText = "") #end #end ## Make sure the type hint actually looks like a legal PHP class name(permitting namespaces too) for Future proofing reasons. ## This is important because PSR-5 is coming soon,and will allow documentation of types with Syntax like SplStack<int> #if (!$TYPE_HINT.matches('^((\\)?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)+$')) #set($typeHintText = "") #end ## Next,we check if this is using the array Syntax like "MyClass[]",and type hint it as a plaIN ARRAY #if ($TYPE_HINT.endsWith("[]")) #set($typeHintText = "array ") #end /** * @param ${TYPE_HINT} $${PARAM_NAME} */ public ${statIC} function set${NAME}($typeHintText$${PARAM_NAME}) { #if (${STATIC} == "static") self::$${FIELD_NAME} = $${PARAM_NAME}; #else $this->${FIELD_NAME} = $${PARAM_NAME}; #end }
实际上,由于php primitive list实际上很短,因此可以检测它是否是原始类型.
所以:
class CodeGenerationTest { /** * @var DateTimeInterface */ private $date; /** * @var int */ private $num; }
实际上会生成这个:
/** * @var \DateTimeInterface $date */ public function setDate(\DateTimeInterface $date) { $this->date = $date; } /** * @var int $num */ public function setNum($num) { $this->num = $num; }
您可以在此处找到有关模板变量的帮助:
https://www.jetbrains.com/phpstorm/webhelp/file-template-variables.html
以上是脚本宝典为你收集整理的phpstorm生成类型为提示的setter全部内容,希望文章能够帮你解决phpstorm生成类型为提示的setter所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。