Learn to enforce code quality with static analysis and automated linting. Master PHPStan and Laravel Pint to keep your Laravel project board maintainable.
Previously in this course, we explored advanced testing: integration tests in Laravel, ensuring our business logic holds up under pressure. While testing validates behavior, it doesn't always catch structural inconsistencies or potential type-related bugs. This lesson focuses on code quality—specifically, how to use static analysis and linting to automate the detection of errors and enforce a consistent style before a single test runs.
As your Laravel application grows, maintaining a consistent style manually becomes impossible. Code quality tools act as a "first line of defense." They don't just check for missing semicolons; they analyze your code's structure to ensure you're using types correctly and adhering to architectural constraints.
By integrating these tools into your workflow, you move from "hoping" your code is clean to "knowing" it meets the project's standards.
Laravel Pint is a zero-configuration opinionated PHP code style fixer built on top of PHP-CS-Fixer. It ensures your code adheres to a standard (usually the PSR-12 standard).
Installation: Install Pint as a dev-dependency:
Bashcomposer require laravel/pint --dev
Configuration:
Create a pint.json file in your root directory to define your rules. For our project board, we want strict typing:
JSON{ "preset": "laravel", "rules": { "strict_param": true, "ordered_imports": {"sort_algorithm": "alpha"} } }
Running the Linter:
Run ./vendor/bin/pint to automatically fix style issues across your entire codebase.
While linting fixes style, static analysis checks for "semantic" bugs—like passing a string to a method that expects an integer, or calling a method on a null object. We use PHPStan to perform this deep inspection.
For a deeper dive into how this helps enforce structural boundaries in larger apps, see PHPStan static analysis for enforcing Laravel architecture constraints.
Setup: Install PHPStan:
Bashcomposer require --dev phpstan/phpstan
Configuration:
Create phpstan.neon in your root. We'll set the level to 5 to start, which balances thoroughness with manageable noise:
NEONincludes: - ./vendor/nunomaduro/larastan/extension.neon parameters: paths: - app level: 5
Note: Using Larastan (the Laravel extension for PHPStan) is essential because it teaches the analyzer about Eloquent's "magic" methods.
Execution: Run the analysis:
Bash./vendor/bin/phpstan analyse
Tools are only useful if they are used. You should never rely on memory to run these checks. Add them to your composer.json scripts section to ensure they are part of your development lifecycle:
JSON"scripts": { "lint": "pint", "analyze": "phpstan analyse", "test:all": ["@lint", "@analyze", "php artisan test"] }
Now, running composer test:all ensures your code is formatted, type-safe, and functionally correct before you commit.
In our project board application, open your TaskService.php from our earlier service-oriented task management lesson.
array)../vendor/bin/phpstan analyse.composer lint to ensure your formatting remains consistent.--generate-baseline feature to ignore current errors and focus on preventing new ones.Code quality is not a luxury; it's a structural requirement for long-term maintainability. By utilizing linting to enforce style and static analysis to catch type-related bugs, you reduce the surface area for production errors. These tools turn your editor into a pair-programmer that never sleeps, ensuring your project board remains robust as you add new features.
Up next: We will begin modularizing our application structure in Project Structure for Large Applications.
Master memory management for Laravel queue workers and CLI tasks. Learn to identify leaks, set memory thresholds, and keep your production processes stable.
Read moreStop guessing why your application is slow. Learn to use Xdebug and Blackfire to profile PHP execution, identify memory bottlenecks, and analyze call traces.
Code Quality and Static Analysis