Skip to content

Mago for Drupal

A Mago extension that adds Drupal's coding standards to the Mago linter and teaches the Mago analyzer about Drupal's services, entities, config and plugins. With it, Mago can take the place of phpcs and PHPStan in a Drupal project.

  • Linter rules check the parts of Drupal's standards that can't be checked by Mago's own rules, such as hook docblocks and translatable strings. Many have a fix, applied by mago lint --fix. See Linter rules and Fixes.
  • Analyzer plugins give mago analyze the types of Drupal's services, entities, config and plugins, and check for mistakes specific to Drupal, such as an unknown service, entity type or config key. See Analyzer.
  • Config presets for a site, module or theme, and for a Drupal core checkout. They set up Mago's formatter, linter and analyzer for Drupal, with two-space indentation for Twig templates formatted by mago twig:fmt. See Configuration.
  • Migration guides for projects that move from phpcs or PHPStan. They list every Coder sniff and every phpstan-drupal rule, with what takes its place. See Coming from Coder and Coming from PHPStan.

Quick start

You need PHP 8.1 or later and Mago 1.62.0 or later.

composer require --dev carthage-software/mago "drupal/mago:^1.0@alpha"

Then create a mago.toml file next to composer.json:

extends = "vendor/drupal/mago/drupal.mago.toml"
# The lowest PHP version that the project supports.
php-version = "8.3"

[source]
# The project's own code.
paths = ["web/modules/custom", "web/themes/custom"]
# Read by the analyzer for their classes and functions, and not checked.
includes = ["vendor", "web/core", "web/modules/contrib"]

A module, theme or profile with its own repository uses paths = ["."] and includes = ["vendor", "web"]. For a module cloned into a site and for a Drupal core checkout, see Install.

Then run the linter, the analyzer and the Twig formatter check:

vendor/bin/mago lint
vendor/bin/mago analyze
vendor/bin/mago twig:fmt --check

The next pages have the rest: what the config files set, the worker's arguments and the fix levels.

Acknowledgements

This extension is based on Coder and phpstan-drupal. The checks of Coder's Drupal and DrupalPractice standards are ported to Mago's linter, and the types and rules of phpstan-drupal to Mago's analyzer.