phpstorm生成带有类型提示的setter

phpstorm生成带有类型提示的setter,php,code-generation,phpstorm,Php,Code Generation,Phpstorm,在phpstorm中,可以通过alt+insert>setters>picking变量为类成员生成setter方法 但是,即使phpstorm知道变量的类型/类,它也不会在参数列表中插入类型提示 如何使phpstorm生成带有类型提示的setter,但仅针对可链接的类型? 示例类 class CodeGenerationTest { /* @var \DateTimeInterface */ private $date; /* @var int */ privat

在phpstorm中,可以通过alt+insert>setters>picking变量为类成员生成setter方法

但是,即使phpstorm知道变量的类型/类,它也不会在参数列表中插入类型提示

如何使phpstorm生成带有类型提示的setter,但仅针对可链接的类型?

示例类

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方法的模板,以指定类型提示

打开PhpStorm的首选项和“文件和代码模板”菜单,在“代码”选项卡下有一个名为“PHPSetter方法”的选项。将其修改为如下所示:

#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
}
实际上会产生这样的结果:

     /**
     * @var \DateTimeInterface $date
     */
    public function setDate(\DateTimeInterface $date)
    {
        $this->date = $date;
    }

    /**
     * @var int $num
     */
    public function setNum($num)
    {
        $this->num = $num;
    }
您可以在此处找到有关模板变量的帮助:

我发现@Pier的解决方案非常有用,因此我更新了他的模板,以生成带有类型暗示和可选类型转换的setter。希望这对其他人有帮助

鉴于:

class CodeGenerationTest
{
    /**
     * @var \DateTime
     */
    private $date;

    /**
     * @var int
     */
    private $id;

    /**
     * @var string|null
     */
    private $notes;
}
将产生:

/**
 * @param \DateTime $date
 */
public function setDate(\DateTime $date)
{
    $this->date = $date;
}

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = (int)$id;
}

/**
 * @param null|string $notes
 */
public function setNotes($notes)
{
    $this->notes = is_null($notes) ? null : (string)$notes;
}
下面是要复制/粘贴到PHPStorm的代码模板:
Settings>Editor>File and code Templates>code>PHP Setter方法

#set($typeHintText = "$TYPE_HINT ")
## First we check against a blacklist of primitive and other common types used in documentation.
#set($nonTypeHintableTypes = ["", "string", "int", "integer", "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

## Set this or self
#set($thisOrSelf = "$this->")
#if (${STATIC} == "static")
    #set($thisOrSelf = "self::$")
#end

## Type cast incoming variable that can also be null, using the ternary operator
#set($ternaryCast = "")
#if ($TYPE_HINT.contains('null|') || $TYPE_HINT.contains('|null'))
    #set($ternaryCast = "is_null($${PARAM_NAME}) ? null : ")
#end

## Type cast incoming variable
#set($cast = " ")
#if ($TYPE_HINT.contains('string')) 
    #set($cast = "(string) ")
#elseif ($TYPE_HINT.contains('object')) 
    #set($cast = "(object) ")
#elseif ($TYPE_HINT.contains('int')) 
    #set($cast = "(int) ")
#elseif ($TYPE_HINT.contains('bool')) 
    #set($cast = "(bool) ")
#elseif ($TYPE_HINT.contains('float') || $TYPE_HINT.contains('double') || $TYPE_HINT.contains('real')) 
    #set($cast = "(float) ")
#end

/**
 * @param ${TYPE_HINT} $${PARAM_NAME}
 */
public ${STATIC} function set${NAME}($typeHintText$${PARAM_NAME})
{
    $thisOrSelf${FIELD_NAME} = $ternaryCast$cast$${PARAM_NAME};
}
#set($typeHintText=“$TYPE\u HINT”)
##首先,我们检查文档中使用的原语和其他常见类型的黑名单。
#set($nonTypeHintableTypes=[“”、“string”、“int”、“integer”、“mixed”、“number”、“void”、“object”、“real”、“double”、“float”、“resource”、“null”、“bool”、“boolean”]))
#foreach($nonTypeHintableTypes中的nonTypeHintableType)
#if($nonTypeHintableType==$TYPE\u HINT)
#套装($typeHintText=”“)
#结束
#结束
##确保类型提示实际上看起来像合法的php类名(也允许使用名称空间),以备将来使用。
##这一点很重要,因为PSR-5即将推出,并且将允许使用诸如SplStack之类的语法编写类型文档
#if(!$TYPE\u HINT.matches(“^(\\)?[a-zA-Z\x7f-\xff][a-zA-Z0-9\UX7F-\xff]+)+$”)
#套装($typeHintText=”“)
#结束
##接下来,我们检查这是否使用了类似“MyClass[]”的数组语法,并将其作为普通数组键入hint
#if($TYPE_HINT.endsWith(“[]”)型)
#设置($typeHintText=“数组”)
#结束
##设置这个或self
#设置($thisOrSelf=“$this->”)
#如果(${STATIC}==“STATIC”)
#设置($thisOrSelf=“self::$”)
#结束
##使用三元运算符键入也可以为null的cast传入变量
#set($ternaryCast=”“)
#if($TYPE|u HINT.contains('null |'))| |$TYPE_HINT.contains('null |'))
#set($ternaryCast=“is_null($${PARAM_NAME})?null:)
#结束
##类型转换传入变量
#套装($cast=”“)
#if($TYPE_HINT.contains('string'))
#设置($cast=“(字符串)”)
#elseif($TYPE\u HINT.contains('object'))
#set($cast=“(对象)”)
#elseif($TYPE_HINT.contains('int'))
#设置($cast=“(int)”)
#elseif($TYPE_HINT.contains('bool'))
#设置($cast=“(bool)”)
#elseif($TYPE_HINT.contains('float')| |$TYPE_HINT.contains('double')| |$TYPE_HINT.contains('real'))
#设置($cast=“(float)”)
#结束
/**
*@param${TYPE\u HINT}$${param\u NAME}
*/
公共${STATIC}函数集${NAME}($typeHintText$${PARAM_NAME})
{
$thisOrSelf${FIELD_NAME}=$ternaryCast$cast$${PARAM_NAME};
}

是否有办法绕过一大串elseif语句来排除非类型hintable类型,如int、string等?@goat我没有找到一个简单的方法,但您可以将所有内容都放在if语句中。我编辑了我的帖子。。我没有找到其他方法。我对你的答案做了一些改进-希望你不介意。酷,我甚至不知道在模板中使用数组是可能的!您在哪里找到您的文档?谢谢。非常感谢。非常感谢。这就节省了大量的时间,如果不是这样的话,这些时间本可以花在构建这个噩梦上。
#set($typeHintText = "$TYPE_HINT ")
## First we check against a blacklist of primitive and other common types used in documentation.
#set($nonTypeHintableTypes = ["", "string", "int", "integer", "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

## Set this or self
#set($thisOrSelf = "$this->")
#if (${STATIC} == "static")
    #set($thisOrSelf = "self::$")
#end

## Type cast incoming variable that can also be null, using the ternary operator
#set($ternaryCast = "")
#if ($TYPE_HINT.contains('null|') || $TYPE_HINT.contains('|null'))
    #set($ternaryCast = "is_null($${PARAM_NAME}) ? null : ")
#end

## Type cast incoming variable
#set($cast = " ")
#if ($TYPE_HINT.contains('string')) 
    #set($cast = "(string) ")
#elseif ($TYPE_HINT.contains('object')) 
    #set($cast = "(object) ")
#elseif ($TYPE_HINT.contains('int')) 
    #set($cast = "(int) ")
#elseif ($TYPE_HINT.contains('bool')) 
    #set($cast = "(bool) ")
#elseif ($TYPE_HINT.contains('float') || $TYPE_HINT.contains('double') || $TYPE_HINT.contains('real')) 
    #set($cast = "(float) ")
#end

/**
 * @param ${TYPE_HINT} $${PARAM_NAME}
 */
public ${STATIC} function set${NAME}($typeHintText$${PARAM_NAME})
{
    $thisOrSelf${FIELD_NAME} = $ternaryCast$cast$${PARAM_NAME};
}