Skip to content

nishphp/phpstan-namespace-dependency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHPStan Namespace Dependency Rule Extension

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.

About

Extension of PHPStan: Namespace Dependency Rules

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages