mintware-de / phpstan-namespace-constraints
A PHPStan rule for restricting namespace usings to control dependency inheritance.
Installs: 4 454
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.1 || ^8.0
- nikic/php-parser: ^4.13 || ^5.0
- phpstan/phpstan: ^0.12.99 || ^1.4.8
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- friendsofphp/php-cs-fixer: ^3.7
- phpunit/phpunit: ^9.5
README
This repository contains the code for the namespace_constraints rule for PHPStan.
Why?
In the most architectures you should not depend on everything. With this rule you can ensure that a source namespace (RegEx Pattern) can only depend on code that lives in the target namespaces.
Installation
$ composer require --dev mintware-de/phpstan-namespace-constraints
Configuration
Add this to your phpstan.neon
# phpstan.neon includes: - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon parameters: namespace_constraints: constraints: - from: 'App\\SourceNamespace(\\.*)?' # Everything inside this namespace has access to to: ['App\\SourceNamespace(\\.*)?'] # this namespace
Examples
Simple
# phpstan.neon includes: - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon parameters: namespace_constraints: constraints: - from: 'App\\Core(\\.*)?' to: ['App\\Core(\\.*)?'] - from: 'App\\Data(\\.*)?' to: - 'App\\Core(\\.*)?' - 'App\\Data(\\.*)?' - from: 'App\\UserInterface(\\.*)?' to: - 'App\\Core(\\.*)?' - 'App\\Data(\\.*)?' - 'App\\UserInterface(\\.*)?'
Advanced
# phpstan.neon includes: - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon parameters: namespace_constraints: constraints: # App\User Constraints - from: 'App\\User\\Core(\\.*)?' to: ['App\\User\\Core(\\.*)?'] # App\\Blog\ Constraints - from: 'App\\Blog\\Core(\\.*)?' to: ['App\\Blog\\Core(\\.*)?'] # App\*\Core -> App\Shared\Core - from: 'App\\(\w+)\\Core(\\.*)?' to: ['App\\Shared\\Core(\\.*)?']