定制 drmovi/phpstan-laravel-module-boundaries 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 root
  • phpstan-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:

  1. Same Module: Files within a module can import from the same module ✅
  2. Shared Modules: Any module can import from modules listed in the shared configuration ✅
  3. Shared to Shared: Shared modules can import from other shared modules ✅
  4. 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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-28