aliemami-coder / laravel-n-plus-one-scanner
Static AST analyzer package for Laravel to scan an entire project and detect N+1 query issues.
Package info
github.com/aliemami-coder/laravel-n-plus-one-scanner
pkg:composer/aliemami-coder/laravel-n-plus-one-scanner
Requires
- php: ^8.2
- illuminate/console: ^10.0|^11.0|^12.0|^13.0
- illuminate/filesystem: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- nikic/php-parser: ^5.0
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A fast, zero-runtime static AST analysis package for Laravel (PHP 8.2+, Laravel 10 / 11 / 12) that scans your entire project—without executing code or requiring a database connection—to detect and report N+1 query issues.
Features
- Model, Trait & Inheritance Discovery: Parses Eloquent models (
app/Models), Traits, andBaseModelinheritance hierarchies to map relationships (including polymorphicmorphTo), FQN targets, and$withdefaults. - Data-Flow Variable Tracking: Tracks variable reassignments, query-builder-to-collection transfers (
$query = User::with('posts'); $users = $query->get();), variable aliases, and conditional branch intersections (if/elseif/else). - PHP Loop Analyzer: Detects lazy-loaded relationship accesses (
$user->posts) insideforeach,for,while, and Collection callbacks (each,map,filter,flatMap,transform), respectingwith(),load(),loadMissing(),$with, andrelationLoaded(). - Relationship Query Chain Analyzer: Detects chained relationship queries inside loops (e.g.
$user->posts()->where('published', true)->count(),->exists(),->sum('amount'),->latest()->first()) and suggests the exact aggregate or eager-loading fix (withCount,withExists,withSum,withAvg,withMax,withMin,with). - Direct Query in Loop Analyzer: Flags
Model::find(),Model::where()->first(),DB::table()->get(), andDB::select()executed inside loops. - API Resource Analyzer: Flags
$this->relation/$this->resource->relationaccesses insideJsonResource/ResourceCollectionwithoutwhenLoaded(), with scopedrelationLoaded()guard analysis acrossif, ternary,&&, and early-return blocks. - Blade View Analyzer: Scans
@foreach/@forelseblocks in.blade.phpfiles and cross-references controllerview()/compact()call sites using safe intersection across multiple controllers. - Shared AST Cache: Parses each PHP file at most once across discovery and all analyzers for fast scanning on large codebases.
- Reports, Suppression & CI/CD: Terminal table, JSON (with exit codes), and interactive searchable HTML reports (
--format=html), plus// @nplusone-ignore,ignore_relations, and--generate-baseline/--baseline.
Installation
composer require aliemami-coder/laravel-n-plus-one-scanner --dev php artisan vendor:publish --tag=nplusone-config
Usage
# Scan the entire project (colored terminal table output) php artisan nplusone:scan # Scan a specific path php artisan nplusone:scan app/Http/Controllers # Filter by severity (all, high, medium) php artisan nplusone:scan --severity=high # JSON output for CI/CD php artisan nplusone:scan --format=json # Interactive HTML report (writes nplusone-report.html by default, or pass --output=...) php artisan nplusone:scan --format=html php artisan nplusone:scan --format=html --output=storage/app/nplusone-report.html # Generate and use a baseline for gradual adoption php artisan nplusone:scan --generate-baseline php artisan nplusone:scan --baseline=nplusone-baseline.json
Inline Suppression
foreach ($users as $user) { // @nplusone-ignore echo $user->profile->bio; }
Contributing
Contributions are warmly welcome! Whether you want to report a bug, suggest a new Eloquent/Laravel pattern to detect, improve static analysis accuracy, or add tests and documentation, your help makes this tool better for the Laravel community.
- Fork the repository
- Create your feature branch (
git checkout -b feat/my-improvement) - Run code formatting and tests:
vendor/bin/pint --test vendor/bin/phpunit
- Commit your changes and open a Pull Request
License
This package is open-source software licensed under the MIT License.