Matt Glaman: phpstan-drupal 2.1.0: stricter defaults

parameters:
drupal:
rules:
cacheableDependencyRule: false

// Before: bails on concrete ModuleHandler, fires on plain object/mixed.
if (!$type->isSuperTypeOf($moduleHandlerInterfaceType)->yes()) {
return [];
}

// After: fires only when the receiver is definitely a ModuleHandlerInterface.
if (!$moduleHandlerInterfaceType->isSuperTypeOf($type)->yes()) {
return [];
}

If you hit a bug on 2.0.x, open an issue and note the version. When you are ready to move to 2.1, the release notes walk through every behavior change with its opt-out.parameters:
drupal:
bleedingEdge:
containerHasAlwaysTrue: true

A three-year-old one-line fix: LoadIncludes

if ($this->container->has('some.service')) {
// "Comparison will always evaluate to true" — so you delete the guard.
$this->container->get('some.service')->doThing();
}

Previously, $container->has('some.service') resolved to always-true for any service the service map knew about. PHPStan then flagged the guard as redundant:Disable any of them individually if they don’t fit your project:These rules shipped as opt-ins over the 2.0 cycle. They have had time to bake, and they catch real bugs, so they no longer require configuration:parameters:
drupal:
classResolverReturnType: false

Supporting 2.0.x

ClassResolverInterface::getInstanceFromDefinition(Foo::class) narrows to Foo. Usually right, but the class resolver checks the container first, and a service definition can substitute an entirely different class. With the narrowed type, this defensive assertion gets flagged as always-true:donquixote for the LoadIncludes fix, Niklan for the detailed report that shaped classResolverReturnType, and Fame Helsinki for sponsoring the project. If phpstan-drupal saves your team time, consider sponsoring.
 $foo = $this->classResolver->getInstanceFromDefinition(Foo::class);
assert($foo instanceof Foo);

This behavior shipped in bleedingEdge.neon first and has now graduated: has() returns bool for known services by default. If you want the old inference back:

Thanks

phpstan-drupal 2.1.0 is out. The theme of this release: rules and behaviors that proved themselves as opt-ins are now the defaults. If you run `composer update` and see new errors, that is the release working as intended — everything below includes the configuration to opt back out.

Similar Posts