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 --fixapplies the safe fixes of the linter rules, and--dry-runshows 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 asstrict: true, which Drupal writes asTRUE.mago formatcorrects both, but it formats the whole file, and itsdrupalpreset also rewrites code that phpcs accepts.mago fixapplies the lint fixes and the analyzer's fixes, unless--no-analyzeis given, and runsmago 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-unsafeadds 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 thedeprecated-tagrewrite of an old deprecation wording.--unsafeadds theweak-hashrewrite, 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.