Search code examples
phpphpstormphpdoc

How to tell PHP Storm that the function return static or another type


class A {
    /**
     * @return static|bool
     */
    public static function build()
    {
         if (/**/) {
             return new static;
         }

         return false;
    }
}

class B extends A {}

$o = B::build();

PHP Storm does not understand that there is the B instance in $o. If I leave just the static without second type in return annotation, all is right.


Solution

  • At the moment PhpStorm does not correctly understand @return static|bool -- only @return static on its own is working right now.

    https://youtrack.jetbrains.com/issue/WI-23435 -- watch this and related ticket (star/vote/comment) to get notified on progress.


    Partial workaround: type hint that variable ($o in your case) via inlline PHPDoc, e.g.

    /** @var B $o */
    $o = B::build();