carthage-software/mago
Mago is an extremely fast PHP linter, formatter, and static analyzer written in Rust. It brings Rust-inspired speed and reliability to PHP projects with a modern toolchain and great developer experience, plus multiple install options (script, Homebrew, Composer).
Integrate Mago directly into Visual Studio Code for powerful, automatic PHP code formatting.
This guide uses the Custom Local Formatters extension to connect Mago to VS Code's formatting engine.
PATH Configured: The mago executable must be available in your system's PATH. The recommended installation methods configure this for you.First, you need to install the bridge extension that allows VS Code to run Mago as a formatter.
Ctrl+Shift+X).Custom Local Formatters.jkillian.settings.jsonNext, you'll configure the extension to use Mago and tell VS Code to use it for PHP files.
Open your user settings.json file. You can do this by opening the Command Palette (Ctrl+Shift+P) and searching for "Open User Settings (JSON)".
Add the following configuration to your settings.json. If you already have these settings, merge them accordingly.
{
// ... your other settings
// 1. Define the Mago command for the formatter extension.
"customLocalFormatters.formatters": [
{
"command": "mago format --stdin-input",
"languages": ["php"]
}
],
// 2. Configure VS Code to use this extension for PHP files.
"[php]": {
// Set the custom formatter as the default for PHP.
"editor.defaultFormatter": "jkillian.custom-local-formatters",
// Recommended: automatically format files on save.
"editor.formatOnSave": true
}
}
Save the settings.json file. You may need to restart VS Code for all changes to take effect.
Your setup is now complete.
editor.formatOnSave enabled, your PHP files will be automatically formatted by Mago every time you save.Ctrl+Shift+P) and running the Format Document command.If you prefer to run Mago directly on save (instead of through VS Code's formatter API), you can use the Run On Save extension.
This approach can be useful when using a project-local Mago binary because the command runs in your workspace context and applies your repository configuration (including exclude rules in mago.toml).
settings.jsonAdd the following configuration to your workspace or user settings.json:
{
// ... your other settings
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.php$",
"cmd": "${workspaceFolder}/vendor/bin/mago fmt ${relativeFile}"
}
]
}
}
After saving a PHP file, VS Code will execute Mago for that file using your workspace's installed binary.
How can I help you explore Laravel packages today?