Skip to content

Instantly share code, notes, and snippets.

@Nicofuma
Created August 25, 2016 12:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Nicofuma/21a9e22738ce0524ceca323f2d41bcaa to your computer and use it in GitHub Desktop.
Save Nicofuma/21a9e22738ce0524ceca323f2d41bcaa to your computer and use it in GitHub Desktop.
PHPStrom templates (PHP 5 & 7, type hint arrays, string[] support, ...)
#set($typeHintText = "$TYPE_HINT")
#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

#if ($typeHintText.matches('^((\\)?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)+$'))
    #set($SCALAR_TYPE_HINT = $TYPE_HINT)
#end

#if ($TYPE_HINT.endsWith("[]"))
    #set($SCALAR_TYPE_HINT = "array")
#end

#if ($TYPE_HINT.endsWith("[]"))
    #set($typeHintText = $TYPE_HINT.substring(1, $TYPE_HINT.indexOf("[")))

    #foreach($nonTypeHintableType in $nonTypeHintableTypes)
        #if ($nonTypeHintableType == $typeHintText)
            #set($TYPE_HINT = "${typeHintText}[]")
        #end
    #end
#end

/**
 * @param ${TYPE_HINT} $${PARAM_NAME}
 *
 * @return $this
 */
public function set${NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})#if(${RETURN_TYPE}): ${CLASS_NAME}#else#end
{
    $this->${FIELD_NAME} = $${PARAM_NAME};
    
    return $this;
}
#set($nonTypeHintableTypes = ["", "string", "int", "mixed", "number", "void", "object", "real", "double", "float", "resource", "null", "bool", "boolean"])

#if ($TYPE_HINT.endsWith("[]"))
    #set($typeHintText = $TYPE_HINT.substring(1, $TYPE_HINT.indexOf("[")))

    #foreach($nonTypeHintableType in $nonTypeHintableTypes)
        #if ($nonTypeHintableType == $typeHintText)
            #set($TYPE_HINT = "${typeHintText}[]")
        #end
    #end
#end

/**
 * @return ${TYPE_HINT}
 */
public ${STATIC} function ${GET_OR_IS}${NAME}()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end
{
#if (${STATIC} == "static")
    return static::$${FIELD_NAME};
#else
    return $this->${FIELD_NAME};
#end
}
#set($typeHintText = "$TYPE_HINT")
#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

#if ($typeHintText.matches('^((\\)?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)+$'))
    #set($SCALAR_TYPE_HINT = $TYPE_HINT)
#end

#if ($TYPE_HINT.endsWith("[]"))
    #set($SCALAR_TYPE_HINT = "array")
#end

#if ($TYPE_HINT.endsWith("[]"))
    #set($typeHintText = $TYPE_HINT.substring(1, $TYPE_HINT.indexOf("[")))

    #foreach($nonTypeHintableType in $nonTypeHintableTypes)
        #if ($nonTypeHintableType == $typeHintText)
            #set($TYPE_HINT = "${typeHintText}[]")
        #end
    #end
#end

/**
 * @param ${TYPE_HINT} $${PARAM_NAME}
 */
public function set${NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})#if(${RETURN_TYPE}): ${CLASS_NAME}#else#end
{
#if (${STATIC} == "static")
    static::$${FIELD_NAME} = $${PARAM_NAME};
#else
    $this->${FIELD_NAME} = $${PARAM_NAME};
#end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment