|
| 1 | +# PHPStan Namespace Constraints Rule |
| 2 | + |
| 3 | +This repository contains the code for the namespace_constraints rule for PHPStan. |
| 4 | + |
| 5 | +## Why? |
| 6 | +In the most architectures you should not depend on everything. |
| 7 | +With this rule you can ensure that a source namespace (RegEx Pattern) can only depend on code that lives in the target |
| 8 | +namespaces. |
| 9 | + |
| 10 | +## Installation |
| 11 | +```bash |
| 12 | +$ composer require --dev mintware-de/phpstan-namespace-constraints |
| 13 | +``` |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Add this to your `phpstan.neon` |
| 18 | + |
| 19 | +```neon |
| 20 | +# phpstan.neon |
| 21 | +includes: |
| 22 | + - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon |
| 23 | +
|
| 24 | +parameters: |
| 25 | + namespace_constraints: |
| 26 | + constraints: |
| 27 | + - from: App\\SourceNamespace\\.* # Everything inside this namespace has access to |
| 28 | + to: [App\\TargetNamespace\\.*] # this namespace |
| 29 | +``` |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | +### Simple |
| 34 | + |
| 35 | +```mermaid |
| 36 | +graph TD; |
| 37 | + App\Data-->App\Core; |
| 38 | + App\UserInterface-->App\Data; |
| 39 | +``` |
| 40 | + |
| 41 | +```neon |
| 42 | +# phpstan.neon |
| 43 | +includes: |
| 44 | + - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon |
| 45 | +
|
| 46 | +parameters: |
| 47 | + namespace_constraints: |
| 48 | + constraints: |
| 49 | + - from: App\\Core(\\.*)? |
| 50 | + to: [App\\Core(\\.*)?] |
| 51 | + - from: App\\Data(\\.*)? |
| 52 | + to: |
| 53 | + - App\\Core(\\.*)? |
| 54 | + - App\\Data(\\.*)? |
| 55 | + - from: App\\UserInterface(\\.*)? |
| 56 | + to: |
| 57 | + - App\\Core(\\.*)? |
| 58 | + - App\\Data(\\.*)? |
| 59 | + - App\\UserInterface(\\.*)? |
| 60 | +``` |
| 61 | + |
| 62 | +### Advanced |
| 63 | + |
| 64 | +```mermaid |
| 65 | +graph TD; |
| 66 | + App\User\Core-->App\Shared\Core |
| 67 | + App\Blog\Core-->App\Shared\Core |
| 68 | + App\*\Core-->App\Shared\Core |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +```neon |
| 73 | +# phpstan.neon |
| 74 | +includes: |
| 75 | + - vendor/mintware-de/phpstan-namespace-constraints/src/Rules/rules.neon |
| 76 | +
|
| 77 | +parameters: |
| 78 | + namespace_constraints: |
| 79 | + constraints: |
| 80 | + # App\User Constraints |
| 81 | + - from: App\\User\\Core(\\.*)? |
| 82 | + to: [App\\User\\Core(\\.*)?] |
| 83 | + # App\\Blog\ Constraints |
| 84 | + - from: App\\Blog\\Core(\\.*)? |
| 85 | + to: [App\\Blog\\Core(\\.*)?] |
| 86 | + # App\*\Core -> App\Shared\Core |
| 87 | + - from: App\\(\w+)\\Core(\\.*)? |
| 88 | + to: [App\\Shared\\Core(\\.*)?] |
| 89 | +``` |
0 commit comments