2014-10-25 18 views
7

In PhpStorm, è possibile generare un metodo setter per i membri della classe da alt + inserto> setter> raccogliendo le variabili per rendere metodi setter per.PhpStorm generare setter con il tipo di suggerimento

Ma, anche quando phpstorm conosce il tipo/classe della variabile, non inserisce un suggerimento tipo nell'elenco dei parametri.

Come rendere phpstorm generare setter con suggerimenti tipo, ma solo per tipi tipi di tipi di prova?

classe Esempio

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

Gli incastonatori generati desiderati dovrebbe essere:

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

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

setNum è corretto, ma setDate è prodotto anche mancante del tipo suggerimento sul parametro:

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

risposta

17

È necessario modificare t modello del tuo metodo Setter PHP in PhpStorm per specificare il suggerimento tipo.

Aprire le Preferenze di PhpStorm e il menu "Modelli di file e codici", sotto la scheda "Codice" c'è un'opzione chiamata "Metodo Setter PHP". Modificarlo per assomigliare a questo:

#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 
} 

In realtà, dal momento che il php primitive list è in realtà breve, è possibile rilevare se si tratta di un tipo primitivo o meno.

Quindi:

class CodeGenerationTest { 

     /** 
     * @var DateTimeInterface 
     */ 
     private $date; 

     /** 
     * @var int 
     */ 
     private $num; 
    } 

sarebbe in realtà genera questo:

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

    /** 
    * @var int $num 
    */ 
    public function setNum($num) 
    { 
     $this->num = $num; 
    } 

È possibile trovare aiuto sulle variabili modelli qui: https://www.jetbrains.com/phpstorm/webhelp/file-template-variables.html

+0

Esiste un modo per aggirare un grande elenco di istruzioni elseif per escludere tipi non tipizzabili tipo, come int, string ecc ...? – goat

+0

@goat Non ho trovato un modo semplice, ma puoi inserire tutto nella dichiarazione if. Ho modificato il mio post .. Non ho trovato un altro modo per farlo. –

+1

Ho apportato alcuni miglioramenti alla risposta - spero che non ti dispiaccia. – goat

2

ho trovato @ soluzione di Pier così utile che ho aggiornato il suo modello per generare setter con entrambi i tipi di suggerimento E optiona l tipo casting. Spero che questo aiuti qualcun altro.

Dato:

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

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

    /** 
    * @var string|null 
    */ 
    private $notes; 
} 

genererà:

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

Ecco il modello di codice da copiare/incollare in PhpStorm sotto: Settings > Editor > File and Code Templates > Code > 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}; 
} 
Problemi correlati