Skip to content

Plugins

The analyzer types the plugins created by plugin managers, the definitions returned by them and the definitions read by plugins. The checks report unknown plugin ids, plugin managers that never call alterInfo() or setCacheBackend(), and plugin annotations that still use the context key, which Drupal 9 removed.

Plugin instances

$manager->createInstance('id') returns the plugin class for core's attribute-based managers: blocks, actions, conditions, field types, widgets, formatters, layouts, mail, queue workers, render elements, typed data, filters, image effects, media sources, REST resources and the other managers whose constructor names an attribute class. A contrib manager that extends one of them, or a receiver typed by the manager's interface, is recognized through the class ancestry.

$blockManager->createInstance('page_title_block');       // Drupal\Core\Block\Plugin\Block\PageTitleBlock
$blockManager->createInstance('system_menu_block:main'); // Drupal\system\Plugin\Block\SystemMenuBlock
$blockManager->createInstance('no_such_block');          // Broken, and reported as drupal/unknown-plugin

A manager of a plugin type outside core names its attribute and its plugin interface when it calls DefaultPluginManager::__construct(). The analyzer reads that call from the manager's constructor, from disk:

parent::__construct(
  'Plugin/WorkspacePreviewHandler',
  $namespaces,
  $module_handler,
  PreviewHandlerInterface::class,  // the plugin interface
  WorkspacePreviewHandler::class,  // the attribute
);
$handlerManager->createInstance('acquia');   // Drupal\workspace_preview\Plugin\WorkspacePreviewHandler\AcquiaPreviewHandler
$handlerManager->createInstance($active_id); // Drupal\workspace_preview\Handler\PreviewHandlerInterface
  • A base:derivative id resolves through its base plugin, the longest one declared, since a base can hold a colon itself, as core's entity:save_action does. That is wrong for a deriver that sets a class per derivative.
  • On a fallback manager (blocks, entity reference selection and filters), an unknown plain id is typed as the fallback plugin, since that is what runs. A derivative id with an undeclared base keeps the declared type, since the base may come from YAML or a deriver that the index doesn't read.
  • An id that isn't a literal, or that no plugin declares, is typed as the plugin interface named by the manager's constructor. An id that two plugins declare keeps the declared type, and so does a call on a manager that declares its own createInstance().
  • The constructor counts when it calls DefaultPluginManager's own constructor once, in its own body, with Foo::class literals or strings, by position or by name. A call in a closure, an anonymous class or a branch of an if doesn't count. The attribute has to implement AttributeInterface, since before Drupal 10.2 that position took an annotation class.
  • A core manager is read this way only when its constructor names the attribute with a literal. Some of them, such as the views manager, build the attribute name at run time, so no id resolves to a class, and the interface alone would hide the methods of their plugins.

Manager definitions

getDefinitions() and getDefinition() on a manager return arrays shaped from the manager's discovery attribute, found as for createInstance(). AttributeBase::get() returns every initialized property of the attribute as a key, so each property becomes a key of that type:

#[\Attribute(\Attribute::TARGET_CLASS)]
final class WorkspacePreviewHandler extends Plugin {

  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly int $weight = 0,
  ) {}

}
$manager->getDefinitions();             // array<string, array{id: string, label: TranslatableMarkup, weight: int, class: class-string, provider?: string, ...}>
$manager->getDefinition('acquia');      // array{id: string, label: TranslatableMarkup, ...}
$manager->getDefinition($id, FALSE);    // array{id: string, label: TranslatableMarkup, ...}|null
  • The shape is unsealed, since a deriver, processDefinition() and an alter hook can add keys.
  • deriver, provider and dependencies are optional, since get() drops them when they are null. So is a typed property that isn't promoted and doesn't have a default, which may be left uninitialized.
  • getDefinition() returns null only when its second argument isn't TRUE. Without it, the call throws for an unknown plugin.
  • A manager that declares its own getDefinitions() or getDefinition(), such as the layout manager, keeps what it declares. So does a manager whose attribute overrides get(), such as PluginID.
  • A manager that passes an annotation class to DefaultPluginManager too, as most core managers do, also reads plugins declared with an annotation. Their definitions come from the annotation class, with other defaults and types, so only id, class and provider stay typed, and the other keys are optional and mixed. A manager typed by an interface counts as one, since its constructor isn't known.
  • A categorizing manager fills an empty category with the provider's name, so category is always set, as string|TranslatableMarkup. The field type manager always sets list_class.
  • The keys of getDefinitions() are typed string. A plugin id made of digits becomes an int key in PHP, but plugin ids aren't numbers in practice, and array-key would report every loop that passes the key on as an id.

Plugin constructors

Core leaves $plugin_id and $plugin_definition untyped in plugin constructors, and so do most plugins that copy them. The plugin factory always passes a string id, so passing the parameter on to a typed one isn't reported as mixed-argument:

public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  parent::__construct($configuration, $plugin_id, $plugin_definition); // not mixed-argument
}
  • The report goes in the constructor and create() of a plugin class, for an untyped $plugin_id or $plugin_definition that goes on unchanged to a parameter of the same name in parent::__construct(), new static() or new self(). That covers a formatter's $label and $view_mode too. A named argument counts by its name, not its position.
  • Any other method, and any other untyped parameter, keeps the report.
  • A parameter that might not be the one the factory passed keeps the report. That's a parameter that the method writes to, takes a reference to or unsets before the call, one assigned in a foreach or a destructuring, one in a method that calls extract(), and one passed on under another name. So does a closure or arrow function parameter with the same name.
  • In create() of a widget or a formatter, $configuration['field_definition'], ['settings'] and ['third_party_settings'] aren't reported when they go to a parameter of the type set by the manager, and neither are a formatter's ['label'] and ['view_mode']. A create() that assigns $configuration first keeps the reports. Other plugins keep them too, since their configuration isn't known.
  • A class that isn't a plugin, such as an exception that passes an untyped $message on, keeps the report.

The SDK doesn't have a hook that sets the type of a parameter inside a function body, so the analyzer drops the reports instead. The parameters stay mixed for everything else in the method.

The plugin index

The plugin classes are read from Mago's class metadata: every instantiable class that descends from PluginInspectionInterface, or from the mail, archiver, config action and language negotiation method interfaces, which don't extend it, and that has one of those attributes or a subclass of one. Vendor code counts, and field items reach the index through TypedData. A plugin whose attribute isn't one of core's is kept under that attribute, for the manager outside core that names it. The generic Plugin and PluginID attributes don't count, since plugins of unrelated types share them.

The id is the attribute's literal id, or the string that a class constant it names holds, as in id: LanguageNegotiationUrl::METHOD_ID. Two classes that declare the same id cancel each other out.

Contrib still ships legacy docblock annotations such as @Block(id = "…") or @RenderElement("…"). The analyzer reads them from the src/Plugin/**/*.php and src/Element/*.php files of every extension under the Drupal root, analyzed or in includes, from disk through the cache. An annotation maps to the attribute of the same short name, and @SearchPlugin and @FormElement are mapped by hand. The legacy @ContentEntityType, @ConfigEntityType and @EntityType annotations are read the same way, see The entity type index.

  • An analyzed file that has an annotation on disk is read from the text analyzed by Mago, so an edit in an editor buffer that hasn't been saved counts.
  • An annotation added to a file that didn't have one when the workers started is read once the file is saved.
  • An annotated file outside the Drupal root isn't read.

An annotation doesn't count on an abstract class, in a @code sample, or on a class that also has the attribute. Two annotated classes that claim one id cancel each other out, as attributed ones do.

Not modeled

These keep the declared types:

  • the changes that a hook_*_info_alter() makes to definitions, and a change to the type of a key made by a deriver or processDefinition(), as ConstraintManager turns a string type into a list;
  • a manager whose parent class has a constructor of its own between it and DefaultPluginManager, or whose constructor passes the attribute or the interface in a variable;
  • a plugin of a type outside core whose attribute is a subclass of the attribute named by its manager, or that's declared with a docblock annotation;
  • plugins discovered from YAML, such as menu links.

Plugin definitions

PluginBase documents $pluginDefinition and getPluginDefinition() as array|PluginDefinitionInterface, so Mago reports every $this->pluginDefinition['label'] as invalid-array-access for the object half. Arrays are Drupal's default: annotation, attribute and YAML discovery all produce them, unless the plugin type opts into a definition class.

The analyzer drops that report on a plugin's own definition, read through the property or the getter. It keeps the report:

  • on a layout, a Layout Builder section storage or a CKEditor 5 plugin, which have definition objects in core;
  • on a class with a discovery attribute whose get() returns an object;
  • on the generic bases that every plugin type extends, both PluginBase classes and ConfigurablePluginBase, since a plugin of any type may be $this there.
$this->pluginDefinition['label'];       // no invalid-array-access on a block, local task or filter
$this->getPluginDefinition()['label'];  // the same through the getter
$block->getPluginDefinition();          // array<string, mixed> on a BlockPluginInterface

getPluginDefinition() on another plugin returns array<string, mixed> when the type of the receiver names a plugin type with array definitions: an interface that extends PluginInspectionInterface, other than the context-aware ones that layouts and section storage share, or a discovery attribute. That covers a definition held in a variable too. A receiver typed as PluginInspectionInterface or PluginBase keeps core's union, since any plugin type fits. In a trait, $this->getPluginDefinition() is an array when every class that uses the trait has array definitions, so BlockPluginTrait gets one, and ContextAwarePluginTrait, which layouts use too, keeps the union.

Derivers

A deriver gets its base definition documented the same way, so inside a class that implements DeriverInterface every such report is dropped, whatever the variable is called. A deriver for a plugin type with definition objects works with them through their methods. A plugin manager's $definition in processDefinition() or in an alter keeps the report.


drupal/unknown-plugin

  • Level: warning
  • Source: Drupal's plugin managers

A plugin manager throws a PluginNotFoundException for an unknown plugin id. A fallback manager, such as the block manager, returns its fallback plugin instead, which doesn't do what the code expects either.

Reports:

  • a createInstance() call with a literal plain id that no scanned plugin declares.

The check covers the plugin types that have a core plugin to look for: actions, archivers, blocks, conditions, entity reference selection, field types, formatters, widgets, mail, filters, image effects, media sources and REST resources.

Not reported:

  • a base:derivative id, which comes from a deriver;
  • an id while the core plugin of its type isn't in the index. Without it, core is outside the analyzed code;
  • a layout id, since core's layouts are mostly YAML, which the index doesn't hold;
  • test code (any file under a tests directory) and hook documentation (*.api.php files). Tests mock managers.
Example
modules/example/src/Plugin/Block/ExampleBlock.php
namespace Drupal\example\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;

#[Block(id: 'example_block')]
class ExampleBlock extends BlockBase {

  public function build(): array {
    return [];
  }

}
// Reported.
use Drupal\Core\Block\BlockManagerInterface;

function example_block(BlockManagerInterface $manager): object {
  return $manager->createInstance('example_blok');
}

// Fine.
use Drupal\Core\Block\BlockManagerInterface;

function example_block(BlockManagerInterface $manager): object {
  return $manager->createInstance('example_block');
}
Compared with phpstan-drupal

phpstan-drupal doesn't report an unknown plugin id.


drupal/plugin-manager-alter-info

  • Level: warning
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's PluginManagerInspectionRule, the half for alterInfo()

phpstan-drupal reports a plugin manager that never calls alterInfo(). Without the call, other modules can't alter the manager's plugin definitions.

Reports:

  • a DefaultPluginManager subclass in the analyzed code whose own constructor never calls alterInfo().

The calls are read from the tokens of the constructor, so a call in a comment doesn't count. A parent::__construct() call is followed into the parent's constructor, read from disk, so a manager that leaves the call to its parent isn't reported.

Not reported:

  • a manager whose service calls alterInfo() through calls:, or through the calls: of its parent: service;
  • a class without a constructor of its own;
  • an abstract class;
  • test code (any file under a tests directory);
  • a class whose constructor, or a parent constructor that it calls, can't be read from disk. See Finding the Drupal root.
Example
modules/example/src/ExampleManager.php
// Reported.
namespace Drupal\example;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler);
    $this->setCacheBackend($cache_backend, 'example_plugins');
  }

}

// Fine.
namespace Drupal\example;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler);
    $this->alterInfo('example_info');
    $this->setCacheBackend($cache_backend, 'example_plugins');
  }

}
Compared with phpstan-drupal
  • phpstan-drupal's rule is off by default. It runs with pluginManagerInspectionRule: true under parameters.drupal.rules. The check is on by default.
  • phpstan-drupal checks every class that implements PluginManagerInterface and extends another class, abstract classes included. The check takes DefaultPluginManager subclasses that aren't abstract.
  • phpstan-drupal looks for the call in the constructor alone. The check also follows parent::__construct() and reads the calls: of the manager's services.
  • phpstan-drupal skips a class whose fully qualified name contains "test", in upper or lower case, so a manager in a Drupal\contest namespace is skipped too. The check skips the files under a tests directory.
  • phpstan-drupal also reports a manager that calls parent::__construct() when the first assignment in its getDiscovery() is a new YamlDiscovery(). The check doesn't port that half.

drupal/plugin-manager-cache-backend

  • Level: warning
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's PluginManagerSetsCacheBackendRule

phpstan-drupal reports a plugin manager that never sets a cache backend. Without one, plugin discovery runs on every request.

Reports:

  • a DefaultPluginManager subclass in the analyzed code whose own constructor never calls setCacheBackend().

The calls are read as for drupal/plugin-manager-alter-info, with parent::__construct() followed into the parent's constructor. A setCacheBackend() call without the cache key counts as a call; Mago reports it as too-few-arguments, since core requires the key.

Not reported:

  • a manager whose service calls setCacheBackend() through calls:, or through the calls: of its parent: service;
  • a class without a constructor of its own;
  • an abstract class;
  • test code (any file under a tests directory);
  • a class whose constructor, or a parent constructor that it calls, can't be read from disk.
Example
modules/example/src/ExampleManager.php
// Reported.
namespace Drupal\example;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler);
    $this->alterInfo('example_info');
  }

}

// Fine.
namespace Drupal\example;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler);
    $this->alterInfo('example_info');
    $this->setCacheBackend($cache_backend, 'example_plugins');
  }

}
Compared with phpstan-drupal
  • phpstan-drupal checks every class that implements PluginManagerInterface, abstract classes included. The check takes DefaultPluginManager subclasses that aren't abstract.
  • phpstan-drupal looks for the call among the top-level statements of the constructor, so a call inside an if is missed there. The check finds the call anywhere in the constructor, follows parent::__construct(), and reads the calls: of the manager's services.
  • phpstan-drupal counts a call with fewer than two arguments as missing.
  • phpstan-drupal also reports in test code.

drupal/plugin-manager-attribute-discovery

  • Level: warning
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's PluginManagerAttributeDiscoveryRule

phpstan-drupal reports a plugin manager that passes a class that isn't an attribute as the fifth argument of DefaultPluginManager::__construct(). Before Drupal 10.2 that argument was the annotation class. Core now reads it as the attribute class, and falls back to annotation discovery alone when the class doesn't implement AttributeInterface. That is deprecated in Drupal 11.2 and removed in Drupal 12. Core raises the deprecation at run time, so a @deprecated tag doesn't show it.

Reports:

  • a parent::__construct() call in a plugin manager that extends DefaultPluginManager, whose $plugin_definition_attribute_name argument names a class that doesn't implement AttributeInterface.

The argument counts by its type: Foo::class, a string, and a variable or constant that holds one of them. When the type has several class names, as after a ternary, each one is checked.

Not reported:

  • a call that leaves the argument out or passes NULL. Core doesn't raise the deprecation for it;
  • a class not scanned by Mago, and a value whose type doesn't name a class, such as a string parameter;
  • a manager whose parent class has a constructor of its own, since that constructor decides what the arguments mean;
  • new DefaultPluginManager(...);
  • anything on Drupal before 10.2, where AttributeInterface doesn't exist and the argument is the annotation class.

Compared with phpstan-drupal: its PluginManagerAttributeDiscoveryDefaultRule also reports a static property or a constructor parameter that is passed on and whose default value names such a class. That isn't ported.

Example
modules/example/src/ExampleManager.php
// Reported.
namespace Drupal\example;

use Drupal\Component\Annotation\Plugin as PluginAnnotation;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, object $module_handler, object $cache) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler, NULL, PluginAnnotation::class);
    $this->alterInfo('example_info');
    $this->setCacheBackend($cache, 'example_plugins');
  }

}

// Fine.
namespace Drupal\example;

use Drupal\Component\Annotation\Plugin as PluginAnnotation;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\Plugin\DefaultPluginManager;

class ExampleManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, object $module_handler, object $cache) {
    parent::__construct('Plugin/Example', $namespaces, $module_handler, NULL, Plugin::class, PluginAnnotation::class);
    $this->alterInfo('example_info');
    $this->setCacheBackend($cache, 'example_plugins');
  }

}

drupal/plugin-annotation-context

  • Level: error
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's PluginAnnotationContextDefinitionsRule

phpstan-drupal reports a plugin annotation that declares its contexts under the context key. Drupal 8.7 renamed the key to context_definitions, and Drupal 9 removed the old one.

Reports:

  • a plugin annotation with a context key.

The annotation is read as the plugin index reads it.

Not reported:

  • an annotation that doesn't count for the index, such as one on an abstract class or on a class that also has the attribute.
Example
modules/example/src/Plugin/Block/ExampleBlock.php
// Reported.
namespace Drupal\example\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Shows the title of the node.
 *
 * @Block(
 *   id = "example_title",
 *   admin_label = @Translation("Title"),
 *   context = {
 *     "node" = @ContextDefinition("entity:node"),
 *   },
 * )
 */
class ExampleBlock extends BlockBase {

  public function build(): array {
    return [];
  }

}

// Fine.
namespace Drupal\example\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Shows the title of the node.
 *
 * @Block(
 *   id = "example_title",
 *   admin_label = @Translation("Title"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node"),
 *   },
 * )
 */
class ExampleBlock extends BlockBase {

  public function build(): array {
    return [];
  }

}
Compared with phpstan-drupal
  • phpstan-drupal checks a class that implements ContextAwarePluginInterface. The check takes any class with a plugin annotation that the index reads.
  • phpstan-drupal looks for context = { anywhere in the text of the docblock. The check reads the parsed annotation.

drupal/field-type-category

  • Level: error
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's FieldTypeCategoryRule

phpstan-drupal reports a @FieldType annotation whose category is a translated label. Since Drupal 10.2 the category is the ID of a field type category plugin, and Drupal 11 removed the label.

Reports:

  • a @FieldType annotation with category = @Translation(...).

The annotation is read as the plugin index reads it.

Not reported:

  • an annotation that doesn't count for the index, such as one on an abstract class, on a class that also has the attribute, or one without an id;
  • the FieldType attribute. Its category is declared as a string, so Mago reports a TranslatableMarkup there as invalid-argument;
  • anything on Drupal before 10.2, where the category is a label.

Compared with phpstan-drupal: it searches the class docblock for the text after @FieldType(, so it also reports category = @Translation in the description of the annotation or in another annotation below it. It reports a class that also has the attribute, an annotation without an id and a class outside src/Plugin, and only a class that extends another one and implements FieldItemInterface.

Example
modules/example/src/Plugin/Field/FieldType/ExampleItem.php
// Reported.
namespace Drupal\example\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;

/**
 * Stores an example value.
 *
 * @FieldType(
 *   id = "example",
 *   label = @Translation("Example"),
 *   category = @Translation("Number"),
 * )
 */
class ExampleItem extends FieldItemBase {
}

// Fine.
namespace Drupal\example\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;

/**
 * Stores an example value.
 *
 * @FieldType(
 *   id = "example",
 *   label = @Translation("Example"),
 *   category = "number",
 * )
 */
class ExampleItem extends FieldItemBase {
}