nunomaduro/phpinsights
PHP Insights analyzes PHP code quality, style, architecture, and complexity from your terminal. Works out of the box with Laravel (artisan insights), Symfony, Yii, Magento, and more, with built-in checks for reliability and loose coupling.
Installation:
composer require nunomaduro/phpinsights --dev
For Laravel projects, publish the config:
php artisan vendor:publish --provider="NunoMaduro\PhpInsights\Application\Adapters\Laravel\InsightsServiceProvider"
First Run:
./vendor/bin/phpinsights
For Laravel:
php artisan insights
Key Files:
phpinsights.php (or config/insights.php in Laravel) – Main configuration.phpinsights.json – Optional custom ruleset file.Run a pre-commit hook or CI check to enforce code quality before merging:
./vendor/bin/phpinsights --no-interaction --format=json > insights.json
Use the JSON output to fail builds if thresholds aren’t met (e.g., via GitHub Actions).
Local Development: Run insights after major refactors or PRs:
php artisan insights --fix
The --fix flag auto-corrects fixable issues (e.g., PSR-12 formatting).
Team Enforcement:
.git/hooks/pre-commit:
#!/bin/sh
./vendor/bin/phpinsights --no-interaction --format=json | jq '.errors | length' -e | grep -q '0' || exit 1
- name: Run PHP Insights
run: ./vendor/bin/phpinsights --format=github
Custom Rulesets:
Extend phpinsights.json to disable/enable checks:
{
"exclude": ["tests/*", "vendor/*"],
"presets": ["slevomat"],
"insights": {
"NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenTraits": {
"enabled": false
}
}
}
php artisan insights:report to generate HTML reports for stakeholders.--format=slack flag.Cache Results:
php artisan insights --cache
Re-run with --cache-clear to refresh.
Parallel Execution:
Enable in phpinsights.php:
'parallel' => true,
False Positives:
ForbiddenTraits may flag Laravel’s built-in traits (e.g., MustVerifyEmail).phpinsights.json:
"exclude": ["app/Models/*"]
Configuration Overrides:
maxComplexity values in phpinsights.php may conflict with team standards."presets": ["symfony"]) to inherit defaults.Slow Analysis:
phpinsights --directory=app/Http
Verbose Output:
phpinsights --verbose
Reveals skipped files and insight execution order.
Dry Run:
phpinsights --dry-run
Simulates checks without modifying files.
Custom Insights:
Create a new insight by extending NunoMaduro\PhpInsights\Domain\InsightInterface and register it in phpinsights.php:
'insights' => [
\App\Insights\CustomInsight::class,
],
Hooks:
Use the insights.run event in Laravel’s EventServiceProvider:
protected $listen = [
'NunoMaduro\PhpInsights\Events\InsightsRun' => [
\App\Listeners\LogInsights::class,
],
];
Preset Management: Share custom presets via Packagist or GitHub:
'presets' => [
'custom-preset' => __DIR__.'/config/insights-preset.php',
],
InsightsServiceProvider is registered in config/app.php under providers.app/Console/Kernel.php:
protected $commands = [
\NunoMaduro\PhpInsights\Application\Adapters\Laravel\Console\InsightsCommand::class,
];
How can I help you explore Laravel packages today?