carthage-software/mago
Mago is an ultra-fast PHP linter, formatter, and static analyzer written in Rust. It helps enforce code quality and consistency with a modern toolchain inspired by Rust, built for reliable checks, formatting, and analysis in PHP projects.
Installation:
composer require carthage-software/mago --dev
Or via the official shell script for global CLI access.
First Run:
mago lint app/ --fix
--fix auto-corrects lint issues (e.g., formatting, redundant code).mago analyze for static analysis (e.g., type safety, Laravel-specific rules).Configuration:
Create mago.php in your project root:
return [
'integrations' => ['laravel'],
'rules' => [
'laravel/assertions' => true,
'laravel/queries' => true,
],
];
laravel/assertions for assertEquals best practices).CI Integration:
Add to .github/workflows/ci.yml:
- name: Lint with Mago
run: mago lint --fail-level=warning
Pre-Commit Hooks:
Use mago via php-cs-fixer or husky:
mago lint --fix --fail-level=error
--baseline to skip unchanged files.Static Analysis in Development:
mago analyze --no-progress --json > mago-results.json
mago-language-server).Laravel-Specific Patterns:
laravel/assertions to catch assertEquals mismatches.laravel/queries to enforce DB::table() over query builder.fake()-> calls with --fix.Custom Rules:
Extend rules via mago.php:
'rules' => [
'no-redundant-variable' => ['severity' => 'warning'],
'custom/rule' => true, // Enable community rules
],
IDE Integration:
mago-language-server for real-time diagnostics:
mago language-server --stdio
php-intelephense to use Mago’s AST.Composer Scripts:
{
"scripts": {
"mago": "mago lint --fix",
"mago:analyze": "mago analyze --no-progress"
}
}
Run with composer mago.
GitHub Actions:
- name: Mago Static Analysis
run: mago analyze --fail-level=error --json
Upload mago-results.json as an artifact for PR reviews.
Symlinked Vendors:
Mago 1.29+ supports symlinked vendor/ via --follow-symlinks.
False Positives:
sound-generic-checking in mago.php if needed:
'analyzer' => ['sound-generic-checking' => false],
// mago:ignore redundant-null-coalesce
$value ??= $default;
Performance:
node_modules/ or vendor/ from analysis:
mago analyze --ignore="node_modules|vendor"
--parallel for large codebases (Rust-backed, so fast by default).Configuration Quirks:
mago.dist support. Place shared rules here.pattern = "..." in mago.php to ignore issues by text:
'analyzer' => ['ignore' => ['pattern' => '.*redundant-logical-operation.*']],
Laravel-Specific:
fake()-> is used in strings. Use --no-fix for these cases.'integrations' => ['wordpress'],
Verbose Output:
mago analyze --verbose
Shows Rust-backed parsing steps.
AST Visualization:
mago ast app/Http/Controllers/Controller.php
Inspect the Abstract Syntax Tree for complex issues.
Baseline Management:
mago lint --remove-outdated-baseline-entries
'cli' => ['version-drift-fail-level' => 'minor'],
Custom Rules: Write Rust-based rules using Mago’s rule SDK. Example:
// src/rules/no_fully_qualified.rs
#[derive(Debug, Default)]
pub struct NoFullyQualified;
impl Rule for NoFullyQualified {
fn check(&self, node: &Node) -> Vec<Diagnostic> {
// Implement logic here
}
}
Language Server:
Extend diagnostics via mago-language-server’s onDiagnostic hook.
Formatter:
Override defaults in mago.php:
'formatter' => [
'echo_tags' => 'align',
'constructor_parens' => 'preserve',
],
{closure:src/foo.php:12:5}).wordpress integration for rules like nonce-verification.--follow-symlinks for monorepos or custom setups.How can I help you explore Laravel packages today?