|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace ScriptDevelopment\PhpstanWarroomRules\Rules; |
| 6 | + |
| 7 | +use Illuminate\Foundation\Http\FormRequest; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Node\InClassNode; |
| 12 | +use PHPStan\Reflection\ClassReflection; |
| 13 | +use PHPStan\Rules\Rule; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | + |
| 16 | +use function sprintf; |
| 17 | + |
| 18 | +/** |
| 19 | + * Enforces ADR-0012 §FormRequest → DTO Flow: every concrete `FormRequest` |
| 20 | + * subclass must define (or inherit) a `toDto()` method so validated input |
| 21 | + * crosses the HTTP boundary as a typed DTO, never as a raw validated array. |
| 22 | + * Without the method, controllers hand `$request->validated()` arrays to |
| 23 | + * Actions — untyped, key-renameable, and invisible to static analysis. |
| 24 | + * |
| 25 | + * Doctrine source: ADR-0012 (FormRequest → DTO Flow). Promoted from |
| 26 | + * entreezuil's reflection-based Pest arch test |
| 27 | + * (`tests/Arch/FormRequestsTest.php`, "form requests with mutation actions |
| 28 | + * define toDto method") — the second instance of the "arch test detects |
| 29 | + * misuse but not omission" enforcement shape under war-room enforcement |
| 30 | + * queue #55, dispositioned for Phase-2 promotion by the Commander on |
| 31 | + * 2026-05-07. Sister of `EnforceResourceDataValidatorOptInRule` (instance 3, |
| 32 | + * PR #20). |
| 33 | + * |
| 34 | + * Scope: classes whose ancestor chain includes the configured base FQCN |
| 35 | + * (default: `Illuminate\Foundation\Http\FormRequest`). Inheritance is |
| 36 | + * matched via PHPStan reflection — short-name collisions in unrelated |
| 37 | + * namespaces do not fire. Abstract classes are skipped (the per-territory |
| 38 | + * `BaseFormRequest` shape is an intermediate layer, not a mutation request). |
| 39 | + * |
| 40 | + * Detection (all three must hold): |
| 41 | + * 1. Class transitively extends the configured base class. |
| 42 | + * 2. Class is concrete (abstract intermediates are exempt). |
| 43 | + * 3. Class neither declares nor inherits a `toDto()` method — own |
| 44 | + * declarations, parent-class declarations, and trait-provided methods |
| 45 | + * all satisfy the contract (mirroring the source-of-truth Pest test's |
| 46 | + * `method_exists()` matcher). |
| 47 | + * |
| 48 | + * Legitimately DTO-less requests (entreezuil precedent: `LoginRequest`, |
| 49 | + * whose auth flow calls `AuthManager::attempt()` directly) are suppressed |
| 50 | + * per consumer `phpstan.neon` `ignoreErrors` keyed on the identifier — |
| 51 | + * never by name inside the rule, per the package convention. |
| 52 | + * |
| 53 | + * Implementation note: the constructor default uses `FormRequest::class` |
| 54 | + * (compile-time constant, never autoloads) instead of an FQCN string |
| 55 | + * literal. Pint's class_keyword fixer calls class_exists() on string |
| 56 | + * literals that look like class names, and the Pint phar bundles a real |
| 57 | + * `Illuminate\Foundation\Http\FormRequest` whose ValidatesWhenResolvedTrait |
| 58 | + * dependency is NOT bundled — a bare FQCN string literal anywhere in this |
| 59 | + * package's PHP source makes `composer format` fatal with "Trait not |
| 60 | + * found". The `use` import is alias-only; consumers analysing non-Laravel |
| 61 | + * trees are unaffected because `::class` resolution requires no autoload. |
| 62 | + * |
| 63 | + * @implements Rule<InClassNode> |
| 64 | + */ |
| 65 | +final class EnforceFormRequestToDtoRule implements Rule |
| 66 | +{ |
| 67 | + private const string DTO_METHOD_NAME = 'toDto'; |
| 68 | + |
| 69 | + public function __construct( |
| 70 | + private string $formRequestBaseClass = FormRequest::class, |
| 71 | + ) {} |
| 72 | + |
| 73 | + public function getNodeType(): string |
| 74 | + { |
| 75 | + return InClassNode::class; |
| 76 | + } |
| 77 | + |
| 78 | + public function processNode(Node $node, Scope $scope): array |
| 79 | + { |
| 80 | + $classNode = $node->getOriginalNode(); |
| 81 | + |
| 82 | + if (!$classNode instanceof Class_) { |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + if ($classNode->isAbstract()) { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + $classReflection = $node->getClassReflection(); |
| 91 | + |
| 92 | + if (!$this->extendsFormRequestBase($classReflection)) { |
| 93 | + return []; |
| 94 | + } |
| 95 | + |
| 96 | + if ($classReflection->hasNativeMethod(self::DTO_METHOD_NAME)) { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + return [ |
| 101 | + RuleErrorBuilder::message(sprintf( |
| 102 | + '%s extends FormRequest but does not define a toDto() method — raw validated-array handoff risk (ADR-0012 / war-room queue #55 / entreezuil FormRequestsTest opt-in invariant).', |
| 103 | + $classReflection->getName(), |
| 104 | + )) |
| 105 | + ->identifier('enforceFormRequestToDto.missingToDtoMethod') |
| 106 | + ->line($classNode->getStartLine()) |
| 107 | + ->build(), |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Inheritance gate: the class must be a (transitive) subclass of the |
| 113 | + * configured base FQCN. Uses PHPStan reflection — handles intermediate |
| 114 | + * abstract layers and namespace-relative `extends` clauses. Short-name |
| 115 | + * collisions in unrelated namespaces do not match, and the base class |
| 116 | + * itself is not a subclass of itself. |
| 117 | + */ |
| 118 | + private function extendsFormRequestBase(ClassReflection $classReflection): bool |
| 119 | + { |
| 120 | + return $classReflection->isSubclassOf($this->formRequestBaseClass); |
| 121 | + } |
| 122 | +} |
0 commit comments