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 (!$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
}

source
File Template Variables

PHP Component - Data Type

By using this component, you are able to use type hints also for basic data types.
This component includes class definitions for php basic data types like:

  • Boolean
  • Floating point
  • Integer
  • String
  • Numeric

Features

  • Enables type hints for basic php types
  • Types shipped with useful methods
  • Are comparable with native php types by using "=="
  • Provides generic type casting by implemented "toString()" methods (and so on)

Usage

Example


/**
 * Class with type hint for string
 *
 * @author stev leibelt 
 * @since 2013-08-04
 */
class MyClass
{
    /**
     * @var array
     * @author stev leibelt 
     * @since 2013-08-04
     */
    private $strings = array();

    /**
     * Super cool method with type hint for string
     *
     * @author stev leibelt 
     * @since 2013-08-04
     */
    public function addString(\Net\Bazzline\Component\DataType\String $string)
    {
        $this->strings[] = $string;

        return $this;
    }
}

$myString = new \Net\Bazzline\Component\DataType\String('super cool test string');

$myClass = new MyClass();
$myClass->addString($myString);

Hints

  • Extend provided types with classes in own namespace.
  • If you add a super cool method to your type, push it and be a part of the development team

Install

Via Git


cd path/to/my/git/respositories
mkdir -p stevleibelt/php_component_data_type
cd stevleibelt/php_component_data_type

git clone git://github.com/stevleibelt/php_component_data_type.git .

Via Composer


require: "net_bazzline/component_data_type": "dev-master"

Why?

I started developing this component because of the many casts i have to do while dealing with php's basic data types. As general, i searched the web for existing and easy to use components but could not find them. If you find one, please tell me. Last but not least SplTypes are still experimental.