phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

ContainerInterace::has should never return "always true"

Open mglaman opened this issue 2 years ago • 2 comments

Feature request

Consequence of https://github.com/mglaman/phpstan-drupal/pull/610/

This caused ContainerInterace::has to be "always true", which may lead developers to removing if ($container->has() checks, causing actual bugs. Especially around conditional dependencies.

Slack thread for context: https://drupal.slack.com/archives/C033S2JUMLJ/p1701250878367139

mglaman avatar Nov 29 '23 16:11 mglaman

Actually, this wasn't a consequence of #610; it's been a latent problem for a while

        if ($methodReflection->getName() === 'has') {
            return new ConstantBooleanType($this->serviceMap->getService($serviceId) instanceof DrupalServiceDefinition);
        }

mglaman avatar Mar 22 '24 14:03 mglaman

I think this should be a new bleeding edge toggle to:

  • If the service is provided by core.services.yml, be concrete with the result.
  • If it's from a module, always ensure a null return type
  • If calling get after has, do not consider it to be nullable (maybe if possible?)

mglaman avatar Mar 22 '24 14:03 mglaman