phpstorm 生成带有类型提示的设置器

Posted

技术标签:

【中文标题】phpstorm 生成带有类型提示的设置器【英文标题】:phpstorm generate setter with type hint 【发布时间】:2014-12-20 22:47:45 【问题描述】:

phpstorm 中,您可以通过 alt + insert > setter > 选择要为其创建 setter 方法的变量来为类成员生成 setter 方法。

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

如何让 phpstorm 生成带有类型提示的 setter,但仅限于类型提示类型

示例类

class CodeGenerationTest 
    /* @var \DateTimeInterface */
    private $date;
    /* @var int */
    private $num;

所需生成的setter应该是:

/**
 * @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;

【问题讨论】:

【参考方案1】:

您需要在 PhpStorm 中更改您的 PHP Setter Method 的模板以指定类型提示。

打开 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

【讨论】:

是否有办法绕过一大堆 elseif 语句来排除非类型提示类型,例如 int、string 等...? @goat 我没有找到简单的方法,但是您可以将所有内容都放在 if 语句中。我编辑了我的帖子。我没有找到其他方法。 我对你的回答做了一些改进——希望你不介意。 酷,我什至不知道可以在模板中使用数组! 您在哪里找到您的文档?【参考方案2】:

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

给定:

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 &gt; Editor &gt; File and Code Templates &gt; Code &gt; 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", "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;

【讨论】:

谢谢。谢谢你。谢谢你。这只是节省了一大堆时间,否则这些时间会花在构建这个如果不是噩梦。

以上是关于phpstorm 生成带有类型提示的设置器的主要内容,如果未能解决你的问题,请参考以下文章

PhpStorm - 快速文档中的返回类型提示

PhpStorm 完成,类型提示泛型

派生类返回值的 PhpStorm 类型提示

PHPStorm 类型提示不同类型的数组

PhpStorm - 类型提示差异

Netbeans 和 PHPStorm 中的变量类型提示