php:S1488 false positive for return local variable

PHP 8.2.8, SonarQube Community Edition 9.9.1

/**
 * @template TService of object
 *
 * @param class-string<TService> $identifier
 *
 * @return TService
 */
protected function getService(string $identifier): object
{
    /** @var TService $service */
    $service = $this->container->get($identifier);

    return $service;
}

Hi @kordum,

can you provide a bit more context of your code example and and explanation of why you think it is a false positive?

Best,
Nils

Hi, sure!

$this->container->get($identifier)

This code returns object (of any class). I use generic method in example, see screenshot. Psalm and phpstan provide it as additional syntax and typing extension. Psalm and phpstan correctly understands this syntax construction, this construction cannot be written otherwise due to dynamic typing. But SonarQube marks as php:S1488.

Thank you for the clarification. I still wonder why you did not set the return type to TService instead of object. And do I get it correctly that the phpDoc is only for Pslam and PhpStan?

Best,

I still wonder why you did not set the return type to TService instead of object

I use service-container (as service-locator in this place of my project) from vendor package, it returns object and not generic unfortunately. I create a generic manually through phpDoc.

And do I get it correctly that the phpDoc is only for Pslam and PhpStan?

Yes. And for PhpStorm too.

It seems that sonar doesn’t understand that the variable has a phpDoc typing specification (for generic from @template).

Should I provide some more information for this problem?