|
| 1 | +# Respect\FluentAnalysis |
| 2 | + |
| 3 | +PHPStan extension for [Respect/Fluent](https://github.com/Respect/Fluent) builders. |
| 4 | +Provides method resolution, parameter validation, and tuple-typed `getNodes()` |
| 5 | +without generated code. |
| 6 | + |
| 7 | +Fluent builders use `__call` to resolve method names to class instances. Since |
| 8 | +those methods don't exist as real declarations, PHPStan reports errors and can't |
| 9 | +validate arguments. This extension teaches PHPStan what each method does: its |
| 10 | +parameters, return type, and the exact tuple of accumulated nodes. |
| 11 | + |
| 12 | +```php |
| 13 | +$stack = Middleware::cors('*')->rateLimit(100); |
| 14 | + |
| 15 | +// PHPStan knows: |
| 16 | +// cors() accepts (string $origin = '*') |
| 17 | +// rateLimit() accepts (int $maxRequests = 60) |
| 18 | +// getNodes() returns array{Cors, RateLimit} |
| 19 | +// $stack is Middleware<array{Cors, RateLimit}> |
| 20 | + |
| 21 | +$stack->getNodes()[0]; // PHPStan infers: Cors |
| 22 | +$stack->typo(); // PHPStan error: method not found |
| 23 | +$stack->cors(42); // PHPStan error: int given, string expected |
| 24 | +``` |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```bash |
| 29 | +composer require --dev respect/fluent-analysis |
| 30 | +``` |
| 31 | + |
| 32 | +Requires PHP 8.5+ and PHPStan 2.1+. |
| 33 | + |
| 34 | +## Setup |
| 35 | + |
| 36 | +### 1. Generate the method cache |
| 37 | + |
| 38 | +```bash |
| 39 | +vendor/bin/fluent-analysis generate |
| 40 | +``` |
| 41 | + |
| 42 | +This scans your project for builder classes with `#[FluentNamespace]`, reads the |
| 43 | +factory configuration from the attribute, and writes a `fluent.neon` file mapping |
| 44 | +method names to target classes. |
| 45 | + |
| 46 | +### 2. Include in your PHPStan config |
| 47 | + |
| 48 | +```neon |
| 49 | +includes: |
| 50 | + - vendor/respect/fluent-analysis/extension.neon |
| 51 | + - fluent.neon |
| 52 | +``` |
| 53 | + |
| 54 | +The extension loads automatically via Composer's PHPStan plugin mechanism. |
| 55 | +The `fluent.neon` file provides the method map for your specific builders. |
| 56 | + |
| 57 | +### 3. Re-generate when classes change |
| 58 | + |
| 59 | +Run `vendor/bin/fluent-analysis generate` again after adding, removing, or |
| 60 | +renaming classes in your fluent namespaces. The command detects unchanged output |
| 61 | +and skips the write if nothing changed. |
| 62 | + |
| 63 | +```bash |
| 64 | +# Custom output path |
| 65 | +vendor/bin/fluent-analysis generate -o phpstan/fluent.neon |
| 66 | +``` |
| 67 | + |
| 68 | +## Features |
| 69 | + |
| 70 | +### Method resolution |
| 71 | + |
| 72 | +Every method on your builder is resolved to its target class. PHPStan reports |
| 73 | +unknown methods as errors: typos are caught at analysis time. |
| 74 | + |
| 75 | +### Constructor parameter forwarding |
| 76 | + |
| 77 | +Method parameters come from the target class constructor. If `Cors` has |
| 78 | +`__construct(string $origin = '*')`, then `->cors()` accepts the same |
| 79 | +signature. Type mismatches are reported. |
| 80 | + |
| 81 | +### Tuple-typed `getNodes()` |
| 82 | + |
| 83 | +The extension tracks which node types are accumulated through the chain. |
| 84 | +`getNodes()` returns a precise tuple instead of `array<int, object>`: |
| 85 | + |
| 86 | +```php |
| 87 | +$builder = new MiddlewareStack(); |
| 88 | +$chain = $builder->cors('*')->rateLimit(100)->auth('bearer'); |
| 89 | + |
| 90 | +// PHPStan knows: array{Cors, RateLimit, Auth} |
| 91 | +$nodes = $chain->getNodes(); |
| 92 | + |
| 93 | +// Individual elements are typed |
| 94 | +$nodes[0]; // Cors |
| 95 | +$nodes[1]; // RateLimit |
| 96 | +$nodes[2]; // Auth |
| 97 | +``` |
| 98 | + |
| 99 | +Tuple tracking works through variable assignments and static calls: |
| 100 | + |
| 101 | +```php |
| 102 | +$a = MiddlewareStack::cors('*'); |
| 103 | +$b = $a->rateLimit(100); |
| 104 | +$b->getNodes(); // array{Cors, RateLimit} |
| 105 | +``` |
| 106 | + |
| 107 | +### Deprecation forwarding |
| 108 | + |
| 109 | +If a target class is marked `@deprecated`, the fluent method inherits the |
| 110 | +deprecation. PHPStan reports it wherever the method is called. |
| 111 | + |
| 112 | +### Composable prefix support |
| 113 | + |
| 114 | +For builders using Respect/Fluent's composable prefixes (like Validation's |
| 115 | +`notEmail()`, `nullOrStringType()`), the extension resolves composed methods |
| 116 | +with correct parameter signatures. |
| 117 | + |
| 118 | +## How it works |
| 119 | + |
| 120 | +The extension registers two PHPStan hooks: |
| 121 | + |
| 122 | +1. **`FluentMethodsExtension`** (`MethodsClassReflectionExtension`) — tells |
| 123 | + PHPStan which methods exist on each builder, with parameters extracted from |
| 124 | + the target class constructor. |
| 125 | + |
| 126 | +2. **`FluentDynamicReturnTypeExtension`** (`DynamicMethodReturnTypeExtension` + |
| 127 | + `DynamicStaticMethodReturnTypeExtension`) — intercepts each method call to |
| 128 | + track accumulated node types as a `GenericObjectType` wrapping a |
| 129 | + `ConstantArrayType` tuple. When `getNodes()` is called, the tuple is |
| 130 | + returned directly. |
| 131 | + |
| 132 | +Both extensions share a `MethodMap` that resolves method names to target |
| 133 | +class FQCNs, with parent-class fallback for builder inheritance. |
| 134 | + |
| 135 | +The `generate` command reads the `#[FluentNamespace]` attribute from each |
| 136 | +builder, extracts the factory's resolver and namespaces, discovers classes, |
| 137 | +and uses `FluentResolver::unresolve()` to derive method names from class names. |
| 138 | + |
| 139 | +## vs. `@mixin`-style interfaces |
| 140 | + |
| 141 | +| | FluentAnalysis | `@mixin` | |
| 142 | +|---------------------|-------------------------------------|--------------------------------------| |
| 143 | +| Generated files | None (one small neon cache) | Interface files per builder + prefix | |
| 144 | +| Return type | `Builder<array{A, B, C}>` | `Builder` (via `@mixin`) | |
| 145 | +| `getNodes()` type | `array{A, B, C}` (exact tuple) | `array<int, Node>` (generic) | |
| 146 | +| Element access | `$nodes[0]` typed as `A` | `mixed` | |
| 147 | +| Deprecation | Forwarded automatically | Must regenerate | |
| 148 | +| Composable prefixes | Resolved from cache | Full method signatures | |
| 149 | +| IDE support | PHPStan-powered (PhpStorm, VS Code) | Direct IDE autocomplete | |
| 150 | +| Maintenance | Re-run `generate` on class changes | Manual/generated | |
| 151 | + |
| 152 | +Both approaches work. Use FluentAnalysis for precise type tracking. Use `@mixin`s |
| 153 | +for broader IDE autocomplete without PHPStan. |
0 commit comments