PHPStorm customize parameter type inspection (scalar types)

Hello,

I'm really new to PHPStorm plugin development so please bare with me if I'm using wrong terms or anything ;)

I use a customized PHP version in my company that has built-in support for scalar type hints such as int, float, string, bool etc. Now I need to make PHPStorm aware of that.

Is it even possible with PHPStorm's Open API to customize PHP's BNF grammar (I guess this would be required) and PHPStorm's parameter type inspection system to get this working? Or is that not open for extension?
If that is theoretically possible can anyone give me a hint where to start here? PHPStorm's Open API documentation is not bringing me any further.

Any help is highly appreciated.

Btw: Thank you for making this great IDE. You're doing a great job around here :)

Cheers,

Steve

6 comments
Comment actions Permalink

Nope grammar changes are NOT possible.
Can you please give us examples of your scalar type hint usage? We'll think of something.

0
Comment actions Permalink

Hi Alexey,

thank you for the quick reply. With our patched PHP version it is possible now to type hint primitive PHP scalar types in method signatures:

function foo(int $int, float $float, string $string, bool $bool) {
    is_int($int); // true
    is_float($float); //true
    is_string($string); // true
    is_bool($bool); // true
}

Additionally we added more generic type hints like:

function bar(scalar $scalar, numeric $numeric) {
    is_scalar($scalar); // true
    is_numeric($numeric); //true
}

The syntax is fully backward compatibly, it just adds additional type safety at compiler and runtime level. The reason for this is to avoid unnecessary boilerplate code for type safety ensurance like:

function baz($string) {
    if (!is_string($string) {
        throw new \InvalidArgumentException('Expected string type value');
    }
}

Scalar type hints have been discussed in several PHP RFCs but until now never got accepted to be included in the core. At some point in the future this might change though and using this kind of syntax would then still be compatible.

The problem I have using it in PHPStorm is that those type hints don't work with some components like undefined class inspection. I would like to make PHPStorm aware of those and also interpret them appropriately all over the place.
It's sad to hear that this is not possible with a custom grammar but I am open to any other solution of course.

Thank you very much!

Cheers,
Steve

0
Comment actions Permalink

have you tried to actually creating classes \int etc. and putting them in a file in your project - just to fix those "class" refs?..

0
Comment actions Permalink

Sure this might fix PHPStorm complaining about "undefined class" in function/method signatures. But there is more to it. Passing variables to those functions which are not of class \int (for example) but in fact plain integer values makes PHPStorm complain about invalid argument types passed to those methods. Furthermore function/method docblocks are invalid because if I define "@param int $value" in the docblock and define "public function fooBar(int $value)" as method signature, PHPStorm can't match it and complains about mismatching method signature. There are even more problems to it so just defining a dummy class \int won't work. :(
Any other possibilities I have here?

0
Comment actions Permalink

Sorry, this is not user-serviceable (or augumentable), it counters current language behavior too much.

0
Comment actions Permalink

PHPStorm already allows a @param typehint for "number", yet there is no such native type.

It would be nice to be able to say "scalar" instead of putting "(bool|number|string)" everywhere.

Many documentation generators and code inspectors allow "scalar", yet PHPStorm does not :(

0

Please sign in to leave a comment.