Last active
March 11, 2016 10:36
-
-
Save prgTW/8f1ae5cbc3773c99cafd to your computer and use it in GitHub Desktop.
PhpStorm Setter Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#set($typeHintSanitized = $TYPE_HINT) | |
## First check if attribute is nullable | |
#set($nullableOptions = ["null|", "null |", "|null", "| null"]) | |
#set($typeHintSuffix = "") | |
#foreach($nullableOption in $nullableOptions) | |
#if ($typeHintSanitized.contains($nullableOption)) | |
#set($typeHintSanitized = $typeHintSanitized.replace($nullableOption, "")) | |
#set($typeHintSuffix = " = null") | |
#end | |
#end | |
## Set default type hint for generated method | |
#set($typeHintText = "$typeHintSanitized ") | |
## Second 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 == $typeHintSanitized) | |
#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 (!$typeHintSanitized.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 ($typeHintSanitized.endsWith("[]")) | |
#set($typeHintText = "array ") | |
#end | |
/** | |
* @param ${TYPE_HINT} $${PARAM_NAME} | |
#if (${STATIC} != "static") | |
* | |
* @return $this | |
#end | |
*/ | |
public ${STATIC} function set${NAME}($typeHintText$${PARAM_NAME}$typeHintSuffix) | |
{ | |
#if (${STATIC} == "static") | |
self::$${FIELD_NAME} = $${PARAM_NAME}; | |
#else | |
$this->${FIELD_NAME} =#if (${typeHintSanitized} == "boolean") (bool)#end $${PARAM_NAME}; | |
return $this; | |
#end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment