Best Practices Analyzers
15 analyzers ensuring you follow Laravel ecosystem best practices and framework conventions.
Overview
Best Practices analyzers focus on Laravel-specific patterns, framework conventions, and architectural best practices. These analyzers help ensure your code follows Laravel's intended usage patterns and prevents common mistakes that can lead to maintainability issues or performance problems.
Key Analyzers
Laravel Conventions
- Logic in Routes Analyzer - Detects business logic in route files
- Logic in Blade Analyzer - Detects complex logic in Blade templates
- Hardcoded Configuration Analyzer - Detects configuration values outside config files
Eloquent & Database
- Eloquent N+1 Query Analyzer - Detects N+1 query problems
- Missing Chunk Analyzer - Detects missing chunk() calls for large datasets
- Missing Database Transactions Analyzer - Detects operations that should use database transactions
- Mixed Query Builder and Eloquent Analyzer - Detects inconsistent query building patterns
Code Organization
- Fat Model Analyzer - Detects models with too much business logic that violate Single Responsibility Principle
- Service Container Resolution Analyzer - Detects manual service container resolution and recommends constructor dependency injection
Error Handling & Logging
- Silent Failure Analyzer - Detects empty catch blocks and error suppression that hide failures
- Missing Error Tracking Analyzer - Ensures error tracking is configured
Anti-Patterns
- Helper Function Abuse Analyzer - Detects overuse of global helper functions
- Hardcoded Storage Paths Analyzer - Detects hardcoded file paths
- PHP-Side Data Filtering Analyzer - Detects filtering that should be done in database
- Framework Override Analyzer - Detects dangerous extensions of Laravel core classes that break during framework upgrades
How They Work
Best Practices analyzers use:
- Pattern Matching: Identifies Laravel-specific patterns and conventions
- AST Analysis: Analyzes code structure to detect architectural violations
- Query Analysis: Examines database queries for optimization opportunities
- Convention Validation: Checks code against Laravel best practices
Severity Levels
| Severity | Description | Examples |
|---|---|---|
| High | Issues that violate core Laravel principles | Logic in routes, N+1 queries, missing transactions |
| Medium | Issues that reduce maintainability | Fat models, hardcoded paths |
| Low | Best practice violations | Helper function abuse |
Running Best Practices Analyzers
Run All Best Practices Analyzers
bash
php artisan shield:analyze --category=best-practicesRun Specific Analyzer
bash
php artisan shield:analyze --analyzer=eloquent-n-plus-one
php artisan shield:analyze --analyzer=missing-database-transactions
php artisan shield:analyze --analyzer=fat-modelRun Multiple Analyzers
bash
php artisan shield:analyze --analyzer=eloquent-n-plus-one,missing-database-transactions,chunk-missingBest Practices
Development
- Run best practices analyzers during code reviews
- Fix High severity issues before merging
- Use analyzers to learn Laravel conventions
Code Reviews
- Review best practices violations in pull requests
- Use violations as teaching opportunities
- Ensure new code follows Laravel patterns
Team Standards
- Agree on which best practices are mandatory
- Document team-specific conventions
- Use analyzers to maintain consistency across codebase
Related Categories
- Security Analyzers - Prevent security vulnerabilities
- Code Quality Analyzers - Maintain code quality standards
- Reliability Analyzers - Prevent runtime errors
- Performance Analyzers - Optimize application performance