Skip to content

howto - add type hinting to phpstorms generated setter method

A colleague of me forwarded me this super cool solution/template to remove one of the little annoying corners of the great PHPStorm IDE (beside the sad fact, that it is closed source software of course).
The template is fixing the problem with the generated setter methods and the missing type hinting.

Open PhpStorm's Preferences and "File and Code Templates" menu, under the "Code" tab there's an option called "PHP Setter Method". Modify it to look like this:
#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

if (!$TYPEHINT.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 ${TYPEHINT} $${PARAMNAME} */ public ${STATIC} function set${NAME}($typeHintText$${PARAM_NAME}) {

if (${STATIC} == "static")

self::$${FIELD_NAME} = $${PARAM_NAME};

else

$this->${FIELD_NAME} = $${PARAM_NAME};

end

}


source
File Template Variables

Translate to de es fr it pt ja

web - Snake game using C++ template metaprogramming

Conway's game of Life may be mathematically interesting, but it is not what most people would consider a real game. So let's take a look at another product of the 70's, the arcade game Snake. This post walks through a complete, compile time implementation of a Snake game using C++ template metaprogramming. We'll start by implementing a basic list and grid, before moving on to encode the game rules. You can find the complete source here.
[...]

Source

Nice way of learning template metaprogramming in C++!

Translate to de es fr it pt ja