drmovi/phpstan-laravel-module-boundaries
最新稳定版本:1.0.0
Composer 安装命令:
composer require drmovi/phpstan-laravel-module-boundaries
包简介
PHPStan extension to enforce Laravel module boundaries and prevent cross-module imports
README 文档
README
A PHPStan extension that enforces module boundaries in Laravel applications to maintain bounded contexts and prevent unwanted cross-module dependencies.
Features
- Detects and prevents cross-module imports between Laravel modules
- Supports shared modules that can be imported by any module
- Configurable via Laravel project's
composer.json - Works with PHPStan 2.0+
Installation
Install the extension via Composer:
composer require --dev drmovi/phpstan-laravel-module-boundaries
Configuration
1. Laravel Project Configuration
In your Laravel project's composer.json, add the following configuration:
{
"extra": {
"modules": {
"path": "app/Modules"
},
"phpstan-laravel-module-boundaries": {
"shared": ["shared", "auth"]
}
}
}
laravel-module.path: The path to your modules directory relative to the project rootphpstan-laravel-module-boundaries.shared: Array of module names that are considered "shared" and can be imported by any module
2. PHPStan Configuration
The extension is automatically loaded when installed. No additional PHPStan configuration is required.
Rules
Module Boundary Rule
This rule enforces the following boundaries:
- Same Module: Files within a module can import from the same module ✅
- Shared Modules: Any module can import from modules listed in the
sharedconfiguration ✅ - Shared to Shared: Shared modules can import from other shared modules ✅
- Cross-Module: Non-shared modules cannot import from other non-shared modules ❌
Example
Given the following module structure:
app/Modules/
├── User/
│ └── UserService.php
├── Order/
│ └── OrderService.php
├── shared/
│ └── SharedHelper.php
└── auth/
└── AuthService.php
With configuration:
{
"extra": {
"modules": {
"path": "app/Modules"
},
"phpstan-laravel-module-boundaries": {
"shared": ["shared", "auth"]
}
}
}
Allowed Imports ✅
// In User/UserService.php use App\Modules\User\UserRepository; // Same module use App\Modules\shared\SharedHelper; // From shared module use App\Modules\auth\AuthService; // From shared module
// In shared/SharedHelper.php use App\Modules\auth\AuthService; // Shared to shared
Forbidden Imports ❌
// In User/UserService.php use App\Modules\Order\OrderService; // Cross-module import
// In Order/OrderService.php use App\Modules\User\UserService; // Cross-module import
Error Messages
When a boundary violation is detected, PHPStan will report an error like:
Module "User" cannot import "App\Modules\Order\OrderService" from module "Order".
Cross-module imports are only allowed from shared modules (shared, auth).
Requirements
- PHP 8.4+
- PHPStan 2.0+
- Laravel project with modular structure
License
MIT License. See LICENSE file for details.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-28