Hi,

I don't know if it is possible to fix that but:

       /**
         * @param \Swoole\Http\Request $request
         * @param \Swoole\Http\Response $response
         * @param array $uri
         * @return false|null|mixed
         */
        protected function _dispatch(Swoole\Http\Request $request, Swoole\Http\Response $response, array $uri)
        {
            $handlers = [
                '*' => Handler\Api::class
            ];

            $currentUri = reset($uri);

            foreach($handlers as $matchUri => $handler)
            {
                if($currentUri === $matchUri || $matchUri === '*')
                {
                    /**
                     * @var class-string<\PhpCliShell\Application\Firewall\Gui\Handler\HandlerInterface> $handler
                     */
                    $handler = new $handler($this->_manager->logger);
                    return $handler->handle($request, $uri);
                }
            }

            return false;
        }

When we use a variable to instanciate a class, we can use the class-string type to help the resolver to retrieve the right type.

Here $handler has the type string

The type class-string can be inline like the example above or about a method argument.

The workaround is to declare the instance type with /** @var ... */ instead of the variable type used to instanciate the new object.

Thank you.

Jean-FrancoisHIVERT thank you for the test case.

We do support class-string<T> for function parameters, I did not though of class-string<T> within @var.

I'll take a look if it can be easily implemented.

    It seems, class-string can't be used in this way. It denotes the variable to be of type string which value is a name of class; let e dig through phpstan documentation how to achieve that.

    ok; it gets confused

    /** @var Type $handler */
    $handler = new $handler;

    @var gets associated with the left-side of the assignment. I don't think there is a way to tell them apart (better to use two variables for this).

      preparing fixed pre-release

      • fixed new $names
      • @var class-string<T> will work as well, you should just use two variables for that, i.e. $handler and $handler_instance, or $name and $obj:
      Write a Reply...