How to add PHP extension syntax to Inspector?

I'm using the phpredis extension to interface with redis backends, and I was wondering how I can educate PHPStorm about allowed comments?

For example I always get highlighting errors on $r->hexists() when $r = new Redis();

How can I teach phpStorm the Redis() class?

Thanks!
Matt

5 comments
Comment actions Permalink

Hi Matt,

You can do this by creating a stub file (kind of PHP equivalent of the extension -- all constants/fields/methods are declared but method/functions bodies are empty) -- all classes that are already supported by PhpStorm are done in this way.
* create a file (or use existing one) where you going to have your class defined (for example: redis.php)
* declare all required classes/constants/fields/methods/functions with appropriate PHPDoc comments
* include this file somehow into your project (via External libraries, for example, or together with your main code)

Here is an example (you may skip documentation if you know what each method does, but I still would recommend to provide correct parameter type hints):

<?php


class Redis
{


    /**
     * Creates a Redis client
     */
    public function __construct() { }


    /**
     * Connects to a Redis instance
     *
     * @param string $host Can be a host, or the path to a unix domain socket
     * @param int $port [optional]
     * @param float $timeout [optional] Value in seconds (default is 0 meaning unlimited)
     * @return boolean TRUE on success, FALSE on error
     */
    public function connect($host, $port = 6379, $timeout = 0.0) { }
    
    /**
     * Connects to a Redis instance or reuse a connection already established with pconnect/popen<br /><br />
     * <p>The connection will not be closed on close or end of request until the php process ends.
     * So be patient on to many open FD's (specially on redis server side) when
     * using persistent connections on many servers connecting to one redis server.</p><br />
     * <p>Also more than one persistent connection can be made identified by
     * either host + port + timeout or unix socket + timeout.</p><br />
     * <p>This feature is not available in threaded versions. pconnect and
     * popen then working like their non persistent equivalents.</p>
     *
     * @param string $host Can be a host, or the path to a unix domain socket
     * @param int $port [optional]
     * @param float $timeout [optional] Value in seconds (default is 0 meaning unlimited)
     * @return boolean TRUE on success, FALSE on error
     */
    public function pconnect($host, $port = 6379, $timeout = 0.0) { }


}
0
Comment actions Permalink

Woah did you type up that stub yourself just now or did you generate it somehow? That was fast :)

Thanks though, I'll do it that way.

0
Comment actions Permalink

Typed + CopyPaste + PhpStorm helped a bit with PHPDoc generation

1) Type the method declaration:

public function connect($host, $port = 6379, $timeout = 0.0) { }


2) Place cursor just above declaration and type /**<ENTER>

/**<ENTER>
public function connect($host, $port = 6379, $timeout = 0.0) { }


3) Type some bits & Copy+Paste the rest of documentation (nice formatting needs to be done manually). But you can skip the full documentation part if you want -- the recommended minimum will be:

    /**
     * @param string $host
     * @param int $port [optional]
     * @param float $timeout [optional]
     * @return boolean
     */
    public function connect($host, $port = 6379, $timeout = 0.0) { }
0
Comment actions Permalink

Thank you! Perfect!

0

Please sign in to leave a comment.