marcokoepfli/laravel-patrol
Composer 安装命令:
composer require marcokoepfli/laravel-patrol
包简介
An opinionated linter that patrols your Laravel app for convention violations
README 文档
README
An opinionated linter that patrols your Laravel app for convention violations. It checks if your code follows "the Laravel way" based on official Laravel documentation and provides actionable improvement suggestions with links to the relevant docs.
Why Patrol?
Tools like Larastan catch type errors. Pint fixes code style. Patrol checks if you're actually using Laravel the way it was designed — Form Requests instead of inline validation, config() instead of env(), Eloquent instead of raw queries, and more.
Every violation includes:
- A clear message explaining what's wrong
- A suggestion for how to fix it
- A link to the relevant Laravel docs section
Installation
composer require --dev marcokoepfli/laravel-patrol
Optionally publish the config:
php artisan vendor:publish --tag="patrol-config"
Usage
php artisan patrol
Example output:
██████╗ █████╗ ████████╗██████╗ ██████╗ ██╗
██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗██╔═══██╗██║
██████╔╝███████║ ██║ ██████╔╝██║ ██║██║
██╔═══╝ ██╔══██║ ██║ ██╔══██╗██║ ██║██║
██║ ██║ ██║ ██║ ██║ ██║╚██████╔╝███████╗
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝
v1.0 · Laravel 12 · 4 rules · the Laravel way
app/Http/Controllers/UserController.php ....................................
[WARNING] env() called outside of config files:11
Move this env() call to a config file and use config() to retrieve the value.
Docs: https://laravel.com/docs/12.x/configuration#accessing-configuration-values
[WARNING] Inline validation: $request->validate():12
Extract validation to a Form Request class using: php artisan make:request
Docs: https://laravel.com/docs/12.x/validation#form-request-validation
app/Services/ReportService.php .............................................
[WARNING] env() called outside of config files:11
Move this env() call to a config file and use config() to retrieve the value.
Docs: https://laravel.com/docs/12.x/configuration#accessing-configuration-values
[INFO] Raw database query: DB::select():12
Consider using Eloquent models and relationships instead of raw queries.
Docs: https://laravel.com/docs/12.x/eloquent
Found 4 violation(s): 3 warning(s), 1 info
Options
# Show everything including info-level hints php artisan patrol --severity=info # Run a specific rule only php artisan patrol --rule=no-env-outside-config # JSON output for CI pipelines php artisan patrol --format=json
Configuration
// config/patrol.php return [ 'version' => 12, // Your Laravel version (11, 12) 'preset' => 'recommended', // 'strict', 'recommended', 'relaxed' 'rules' => [], // Override individual rules (FQCN => bool) 'paths' => ['app', 'routes', 'config', 'resources'], 'exclude' => ['vendor', 'node_modules', 'storage'], 'custom_rules' => [], // Your own rule classes ];
Presets
| Preset | Rules | Use case |
|---|---|---|
relaxed |
1 rule | Just the essentials |
recommended |
4 rules | Sensible defaults for most projects |
strict |
6 rules | Full enforcement |
Disabling a rule
'rules' => [ \MarcoKoepfli\LaravelPatrol\Rules\NoRawQueries::class => false, ],
Built-in Rules
| Rule | ID | Severity | Description |
|---|---|---|---|
| NoEnvOutsideConfig | no-env-outside-config |
Warning | env() should only be used in config files |
| UseFormRequests | use-form-requests |
Warning | Use Form Request classes instead of inline validation |
| NoRawQueries | no-raw-queries |
Info | Prefer Eloquent over raw DB queries |
| UseResourceControllers | use-resource-controllers |
Warning | Use Route::resource() for CRUD routes |
| UseBladeComponents | use-blade-components |
Info | Prefer Blade components over @include |
| NoBusinessLogicInControllers | no-business-logic-in-controllers |
Warning | Keep controllers thin (max 10 statements per method) |
All PHP rules use AST parsing via nikic/php-parser — no regex matching. This means:
env()in strings or comments is not flagged$service->validate()is not confused with$request->validate()DB::table()andDB::transaction()are correctly ignored by the raw query rule
Suppressing violations
Add @patrol-ignore on the line above or the same line:
// @patrol-ignore $key = env('APP_KEY');
{{-- @patrol-ignore --}} @include('partials.legacy')
Custom Rules
Create a class extending AbstractRule:
use MarcoKoepfli\LaravelPatrol\Enums\LaravelVersion; use MarcoKoepfli\LaravelPatrol\ProjectContext; use MarcoKoepfli\LaravelPatrol\Rules\AbstractRule; class NoTodoComments extends AbstractRule { public function id(): string { return 'no-todo-comments'; } public function description(): string { return 'TODO comments should be resolved before merging.'; } public function docsUrl(LaravelVersion $version): string { return 'https://your-team-wiki.com/conventions'; } public function check(ProjectContext $context): array { $violations = []; foreach ($context->phpFiles() as $file) { // Use $context->analyzer() for AST parsing // Use $this->violation() to create results // Use $this->shouldIgnore() for @patrol-ignore support } return $violations; } }
Register in config/patrol.php:
'custom_rules' => [ \App\Patrol\NoTodoComments::class, ],
Testing
composer test # Run tests composer analyse # Run PHPStan composer format # Fix code style
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
marcokoepfli/laravel-patrol 适用场景与选型建议
marcokoepfli/laravel-patrol 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「linter」 「laravel」 「conventions」 「Best-Practices」 「patrol」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marcokoepfli/laravel-patrol 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marcokoepfli/laravel-patrol 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marcokoepfli/laravel-patrol 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Craft Conventions
PHP Naming Conventions
Vanio Coding Standard Ruleset for PHP_CodeSniffer.
Lot's of tools for git, file and static source code analysis.
Validate Blade templates for syntax errors, security issues, and best practices
An less opinionated version of Laravel Pint.
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15