|
| 1 | +# Forbidden Dependencies Rule |
| 2 | + |
| 3 | +Enforces dependency constraints between namespaces by checking `use` statements and optionally fully qualified class names (FQCNs). This rule prevents classes in one namespace from depending on classes in another, helping enforce architectural boundaries like layer separation. |
| 4 | + |
| 5 | +> **Note:** This rule replaces the deprecated `DependencyConstraintsRule`. See [Dependency Constraints Rule](Dependency-Constraints-Rule.md) for migration details. |
| 6 | +
|
| 7 | +## Configuration Example |
| 8 | + |
| 9 | +### Basic Usage (Use Statements Only) |
| 10 | + |
| 11 | +```neon |
| 12 | + - |
| 13 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 14 | + arguments: |
| 15 | + forbiddenDependencies: [ |
| 16 | + '/^App\\Domain(?:\\\w+)*$/': ['/^App\\Controller\\/'] |
| 17 | + ] |
| 18 | + tags: |
| 19 | + - phpstan.rules.rule |
| 20 | +``` |
| 21 | + |
| 22 | +### With FQCN Checking Enabled |
| 23 | + |
| 24 | +```neon |
| 25 | + - |
| 26 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 27 | + arguments: |
| 28 | + forbiddenDependencies: [ |
| 29 | + '/^App\\Capability(?:\\\w+)*$/': [ |
| 30 | + '/^DateTime$/', |
| 31 | + '/^DateTimeImmutable$/' |
| 32 | + ] |
| 33 | + ] |
| 34 | + checkFqcn: true |
| 35 | + tags: |
| 36 | + - phpstan.rules.rule |
| 37 | +``` |
| 38 | + |
| 39 | +### With Selective Reference Types |
| 40 | + |
| 41 | +```neon |
| 42 | + - |
| 43 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 44 | + arguments: |
| 45 | + forbiddenDependencies: [ |
| 46 | + '/^App\\Capability(?:\\\w+)*$/': [ |
| 47 | + '/^DateTime$/', |
| 48 | + '/^DateTimeImmutable$/' |
| 49 | + ] |
| 50 | + ] |
| 51 | + checkFqcn: true |
| 52 | + fqcnReferenceTypes: ['new', 'param', 'return', 'property'] |
| 53 | + tags: |
| 54 | + - phpstan.rules.rule |
| 55 | +``` |
| 56 | + |
| 57 | +## Parameters |
| 58 | + |
| 59 | +- `forbiddenDependencies`: Array where keys are regex patterns for source namespaces and values are arrays of regex patterns for disallowed dependency namespaces. |
| 60 | +- `checkFqcn` (optional, default: `false`): Enable checking of fully qualified class names in addition to use statements. |
| 61 | +- `fqcnReferenceTypes` (optional, default: all types): Array of reference types to check when `checkFqcn` is enabled. |
| 62 | +- `allowedDependencies` (optional, default: `[]`): Whitelist that overrides forbidden dependencies. If a dependency matches both a forbidden pattern and an allowed pattern, it will be allowed. |
| 63 | + |
| 64 | +## FQCN Reference Types |
| 65 | + |
| 66 | +When `checkFqcn` is enabled, the following reference types can be checked: |
| 67 | + |
| 68 | +- `new` - Class instantiations (e.g., `new \DateTime()`) |
| 69 | +- `param` - Parameter type hints (e.g., `function foo(\DateTime $date)`) |
| 70 | +- `return` - Return type hints (e.g., `function foo(): \DateTime`) |
| 71 | +- `property` - Property type hints (e.g., `private \DateTime $date`) |
| 72 | +- `static_call` - Static method calls (e.g., `\DateTime::createFromFormat()`) |
| 73 | +- `static_property` - Static property access (e.g., `\DateTime::ATOM`) |
| 74 | +- `class_const` - Class constant (e.g., `\DateTime::class`) |
| 75 | +- `instanceof` - instanceof checks (e.g., `$x instanceof \DateTime`) |
| 76 | +- `catch` - catch blocks (e.g., `catch (\Exception $e)`) |
| 77 | +- `extends` - class inheritance (e.g., `class Foo extends \DateTime`) |
| 78 | +- `implements` - interface implementation (e.g., `class Foo implements \DateTimeInterface`) |
| 79 | + |
| 80 | +## Use Cases |
| 81 | + |
| 82 | +### Enforcing Layer Boundaries |
| 83 | + |
| 84 | +Prevent domain classes from depending on infrastructure or presentation layers: |
| 85 | + |
| 86 | +```neon |
| 87 | + - |
| 88 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 89 | + arguments: |
| 90 | + forbiddenDependencies: |
| 91 | + '/^App\\Capability\\.*\\Domain/': |
| 92 | + - '/^App\\Capability\\.*\\Application/' |
| 93 | + - '/^App\\Capability\\.*\\Infrastructure/' |
| 94 | + - '/^App\\Capability\\.*\\Presentation/' |
| 95 | + '/^App\\Capability\\.*\\Application/': |
| 96 | + - '/^App\\Capability\\.*\\Infrastructure/' |
| 97 | + - '/^App\\Capability\\.*\\Presentation/' |
| 98 | + tags: |
| 99 | + - phpstan.rules.rule |
| 100 | +``` |
| 101 | + |
| 102 | +### Preventing DateTime Usage in Domain Layer |
| 103 | + |
| 104 | +Encourage the use of domain-specific date/time objects instead of PHP's built-in classes: |
| 105 | + |
| 106 | +```neon |
| 107 | + - |
| 108 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 109 | + arguments: |
| 110 | + forbiddenDependencies: [ |
| 111 | + '/^App\\Capability(?:\\\w+)*$/': [ |
| 112 | + '/^DateTime$/', |
| 113 | + '/^DateTimeImmutable$/' |
| 114 | + ] |
| 115 | + ] |
| 116 | + checkFqcn: true |
| 117 | + tags: |
| 118 | + - phpstan.rules.rule |
| 119 | +``` |
| 120 | + |
| 121 | +This will catch: |
| 122 | + |
| 123 | +- `use DateTime;` (use statement) |
| 124 | +- `new \DateTime()` (instantiation) |
| 125 | +- `function foo(\DateTime $date)` (parameter type) |
| 126 | +- `function bar(): \DateTime` (return type) |
| 127 | +- `private \DateTime $date` (property type) |
| 128 | +- And all other reference types listed above |
| 129 | + |
| 130 | +### Whitelist with allowedDependencies |
| 131 | + |
| 132 | +The `allowedDependencies` parameter lets you create a "forbid everything except X" pattern. Dependencies matching both forbidden and allowed patterns will be allowed. |
| 133 | + |
| 134 | +```neon |
| 135 | + - |
| 136 | + class: Phauthentic\PHPStanRules\Architecture\ForbiddenDependenciesRule |
| 137 | + arguments: |
| 138 | + forbiddenDependencies: [ |
| 139 | + '/^App\\Capability\\.*\\Domain$/': [ |
| 140 | + '/.*\\\\.*/' |
| 141 | + ] |
| 142 | + ] |
| 143 | + checkFqcn: true |
| 144 | + allowedDependencies: [ |
| 145 | + '/^App\\Capability\\.*\\Domain$/': [ |
| 146 | + '/^App\\Shared\\/', |
| 147 | + '/^App\\Capability\\/', |
| 148 | + '/^Psr\\/' |
| 149 | + ] |
| 150 | + ] |
| 151 | + tags: |
| 152 | + - phpstan.rules.rule |
| 153 | +``` |
| 154 | + |
| 155 | +This will: |
| 156 | + |
| 157 | +- **Allow**: `App\Shared\ValueObject\Money`, `App\Capability\Billing\Invoice`, `Psr\Log\LoggerInterface` |
| 158 | +- **Forbid**: `Doctrine\ORM\EntityManager`, `Symfony\Component\HttpFoundation\Request` |
| 159 | + |
| 160 | +## Diagram |
| 161 | + |
| 162 | +```mermaid |
| 163 | +flowchart TD |
| 164 | + A[Check Dependency] --> B{Matches forbidden pattern?} |
| 165 | + B -->|No| C[Allow] |
| 166 | + B -->|Yes| D{Matches allowed pattern?} |
| 167 | + D -->|Yes| E[Allow - Override] |
| 168 | + D -->|No| F[Report Error] |
| 169 | +``` |
| 170 | + |
| 171 | +## Backward Compatibility |
| 172 | + |
| 173 | +By default, `checkFqcn` is `false` and `allowedDependencies` is empty, so existing configurations will continue to work exactly as before, checking only `use` statements. The new FQCN checking and allowedDependencies features must be explicitly enabled. |
0 commit comments