nish/phpstan-namespace-dependency

Extension of PHPStan: Namespace Dependency Rules

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

This package is auto-updated.

Last update: 2024-12-16 06:34:10 UTC


README

This package is a PHPStan extension for checking namespace dependencies.

Install

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

How to use

Add to 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'],
    ])

Examples

Layered

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': [],
    ])

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

Friend, Package Private

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'],
    ], [])

The settings are searched in order from the top, and when the key is hit, the search is stopped there.

App\Model\MyModel\InternalClassImpl allows dependencies from App\Model\MyModel\InternalClassFactory, not from App\Controller.