phpstan-drupal
phpstan-drupal copied to clipboard
ContainerInterace::has should never return "always true"
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
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);
}
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
nullreturn type - If calling
getafterhas, do not consider it to be nullable (maybe if possible?)