Skip to content

Instantly share code, notes, and snippets.

@codeaid
Last active August 3, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeaid/c986fd8301c15b7d1498e34453d39874 to your computer and use it in GitHub Desktop.
Save codeaid/c986fd8301c15b7d1498e34453d39874 to your computer and use it in GitHub Desktop.
PhpStorm Code Templates
#set ($ARGUMENTS = [])
#set ($ARGUMENTS_STR = "")
#set ($PARAM_DOC_DESC = "")
#set ($NEWLINE = "
")
## only process arguments if any are selected
#if ($PARAM_DOC != "")
## split PhpDoc lines generated by PhpStorm into multiple lines
#set ($DOC_LINES = $PARAM_DOC.split("\n"))
## iterate through all lines and append each argument to the parameter string
#foreach ($DOC_LINE in $DOC_LINES)
## trim the line
#set ($DOC_LINE = $DOC_LINE.trim())
## explode the line into "*", "@param", type and variable name
#set ($LINE_PARTS = $DOC_LINE.split(" "))
#set ($PART_COUNT = 0)
## add "type $variable" to the list of arguments
#if ($LINE_PARTS.size() == 4)
## argument has a type defined
#set ($PARAM_VAR = $LINE_PARTS[3])
#set ($PARAM_TYPE = "$LINE_PARTS[2] ")
#else
## argument has no type defined
#set ($PARAM_VAR = $LINE_PARTS[2])
#set ($PARAM_TYPE = "")
#end
## append argument
$ARGUMENTS.add("${PARAM_TYPE}${PARAM_VAR}")
## convert parameter name to a lowercase sentence
#set ($PARAM_NAME = $PARAM_VAR.substring(1))
#set ($METHOD_DESC = $PARAM_NAME.replaceAll("([A-Z])", " $1"))
#set ($METHOD_DESC = $METHOD_DESC.toLowerCase())
## generate parameter descriptor from the method description
#set ($FIRST_CHAR = $METHOD_DESC.substring(0,1).toUpperCase())
#set ($REMAINING = $METHOD_DESC.substring(1))
#set ($PARAM_DESC = "${FIRST_CHAR}${REMAINING}")
## append parameter description to the list
#set ($PARAM_DOC_DESC = "${PARAM_DOC_DESC}@param ${PARAM_TYPE}${PARAM_VAR} ${PARAM_DESC}${NEWLINE}")
#end
## calculate the last index of arguments
#set ($N = $ARGUMENTS.size() - 1)
## iterate through all arguments and concatenate them into one method hint string
#foreach ($INDEX in [0..$N])
## extract argument at the specified index
#set ($ARGUMENT_STR = $ARGUMENTS.get($INDEX))
## append argument with an optional trailing comma
#if ($INDEX < $N)
#set ($ARGUMENTS_STR = "${ARGUMENTS_STR}${ARGUMENT_STR}, ")
#else
#set ($ARGUMENTS_STR = "${ARGUMENTS_STR}${ARGUMENT_STR}")
#end
#end
#end
/**
* Class constructor
${PARAM_DOC_DESC}
${THROWS_DOC}
*/
public function __construct(${ARGUMENTS_STR})
{
${BODY}
}
#parse ("PHP Setter Method.php")
## convert parameter name to a lowercase sentence
#set ($METHOD_DESC = $PARAM_NAME.replaceAll("([A-Z])", " $1"))
#set ($METHOD_DESC = $METHOD_DESC.toLowerCase())
/**
* Get ${METHOD_DESC}
*
* @return ${TYPE_HINT}
*/
public ${STATIC} function get${NAME}()#if (${SCALAR_TYPE_HINT}): ${SCALAR_TYPE_HINT}#else#end
{
#if (${STATIC} == "static")
return self::$${FIELD_NAME};
#else
return $this->${FIELD_NAME};
#end
}
## convert parameter name to a lowercase sentence
#set ($METHOD_DESC = $PARAM_NAME.replaceAll("([A-Z])", " $1"))
#set ($METHOD_DESC = $METHOD_DESC.toLowerCase())
## generate parameter descriptor from the method description
#set ($FIRST_CHAR = $METHOD_DESC.substring(0,1).toUpperCase())
#set ($REMAINING = $METHOD_DESC.substring(1))
#set ($PARAM_DESC = "${FIRST_CHAR}${REMAINING}")
/**
* Set ${METHOD_DESC}
*
* @param ${TYPE_HINT} $${PARAM_NAME} ${PARAM_DESC}
* @return self
*/
public function set${NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME}): self
{
#if (${STATIC} == "static")
self::$${FIELD_NAME} = $${PARAM_NAME};
return self;
#else
$this->${FIELD_NAME} = $${PARAM_NAME};
return $this;
#end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment