nish/phpstan-namespace-dependency

PHPStan 扩展:命名空间依赖规则

v0.1.4 2024-08-16 02:29 UTC

This package is auto-updated.

Last update: 2024-09-16 05:58:48 UTC


README

此包是 PHPStan 扩展,用于检查命名空间依赖。

安装

composer require --dev nish/phpstan-namespace-dependency

使用方法

添加到 phpstan.neon

includes:
  - vendor/nish/phpstan-namespace-dependency/rules.neon

services:
  -
    factory: Nish\PHPStan\NsDepends\DependencyChecker([
      'callee class name prefix': ['caller class name prefix'],
    ], [
      'caller class name prefix': ['callee class name prefix'],
    ])

示例

分层

includes:
  - vendor/nish/phpstan-namespace-dependency/rules.neon

services:
  -
    factory: Nish\PHPStan\NsDepends\DependencyChecker([
      'PDO': ['App\Dao'],
      'App\Dao': ['App\Model'],
      'App\Model': ['App\Page'],
    ], [])

MVC

includes:
  - vendor/nish/phpstan-namespace-dependency/rules.neon

services:
  -
    factory: Nish\PHPStan\NsDepends\DependencyChecker([
      'App\Model': ['App\Controller'],
      'App\View': ['App\Controller'],
    ], [
      'App\Model': ['App\Util', 'App\ValueObject'],
    ])

领域驱动设计(DDD)

includes:
  - vendor/nish/phpstan-namespace-dependency/rules.neon

services:
  -
    factory: Nish\PHPStan\NsDepends\DependencyChecker([
      'App\DomainService': ['App\ApplicationService'],
      'App\Domain': ['App\DomainService', 'App\ApplicationService', 'App\Infrastructure'],
      'App\ApplicationService': ['App\Presentation', 'App\Tests'],
    ], [
      'App\Domain\DomainException': ['Exception'],
      'App\Domain': ['DateTimeInterface', 'DateTimeImmutable'],
      'App\DomainService': [],
      'App\ApplicationService': [],
    ])

参见 https://github.com/nishimura/phpstan-namespace-dependency-sample

朋友,包私有

includes:
  - vendor/nish/phpstan-namespace-dependency/rules.neon

services:
  -
    factory: Nish\PHPStan\NsDepends\DependencyChecker([
      'App\Model\MyModel\InternalClassImpl': ['App\Model\MyModel\InternalClassFactory'],
      'App\Model': ['App\Controller'],
      'App\View': ['App\Controller'],
    ], [])

设置顺序从上至下搜索,当匹配到键时,搜索停止。

App\Model\MyModel\InternalClassImpl 允许从 App\Model\MyModel\InternalClassFactory 依赖,而不允许从 App\Controller 依赖。