Skip to content

Fixes

Many rules have a fix, the way phpcbf fixes Coder's sniffs. Three commands apply fixes, and they change different amounts of code:

  • mago lint --fix applies the safe fixes of the linter rules, and --dry-run shows their diff first. It only changes the code reported by a rule, so it's the command to use on a project that's checked with phpcs too. Its fixes don't change the indentation, and some write code that isn't in Drupal's style, such as strict: true, which Drupal writes as TRUE.
  • mago format corrects both, but it formats the whole file, and its drupal preset also rewrites code that phpcs accepts.
  • mago fix applies the lint fixes and the analyzer's fixes, unless --no-analyze is given, and runs mago format, in turn until the code stops changing. Use it on a project that's formatted by Mago.

Two flags add riskier fixes to mago lint --fix and mago fix:

  • --potentially-unsafe adds the fixes that turn a comment into a docblock, which is then read by the analyzers. It also adds the fixes that drop or move comment or docblock text, and the deprecated-tag rewrite of an old deprecation wording.
  • --unsafe adds the weak-hash rewrite, which changes stored digests.

Both flags also apply Mago's own fixes of that level, such as strict: true from strict-behavior. Each rule's entry under Linter rules says what its fix does.

The strict-behavior fix

With --potentially-unsafe, Mago's strict-behavior adds strict: true to a call such as in_array() or array_search(), which changes what a loose comparison returns. On core, it would break code that relies on the loose comparison, such as a float compared with a list of ints, or a value from explode() that keeps its spaces. So review each change. Mago doesn't have a flag that skips one rule's fix. To apply the other potentially unsafe fixes without it, use a config file that extends the project's mago.toml and turns the rule off:

# mago-fix.toml, next to mago.toml.
extends = "mago.toml"

[linter.rules]
strict-behavior = { enabled = false }
vendor/bin/mago --config mago-fix.toml lint --fix --potentially-unsafe

mago --config mago-fix.toml fix --potentially-unsafe works the same way.