|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace ScriptDevelopment\PhpstanWarroomRules\Rules; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Array_; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PhpParser\Node\Stmt\ClassConst; |
| 14 | +use PhpParser\Node\Stmt\ClassMethod; |
| 15 | +use PHPStan\Analyser\Scope; |
| 16 | +use PHPStan\Node\InClassNode; |
| 17 | +use PHPStan\Reflection\ClassReflection; |
| 18 | +use PHPStan\Rules\Rule; |
| 19 | +use PHPStan\Rules\RuleErrorBuilder; |
| 20 | + |
| 21 | +use function array_filter; |
| 22 | +use function implode; |
| 23 | +use function in_array; |
| 24 | +use function is_array; |
| 25 | +use function sprintf; |
| 26 | + |
| 27 | +/** |
| 28 | + * Enforces ADR-0009 §EAGER_LOAD validator opt-in: a `ResourceData` subclass |
| 29 | + * declaring a non-empty `EAGER_LOAD_COUNT` or `EAGER_LOAD_SUM` constant must |
| 30 | + * call `validateRelationsLoaded()` somewhere in its method bodies. Without |
| 31 | + * the call, missing eager-load aggregates fail open as `0` / `null` rather |
| 32 | + * than throwing — silently re-introducing the silent-zero bug closed by |
| 33 | + * kendo PR #1079 (KD-0494). |
| 34 | + * |
| 35 | + * Doctrine source: ADR-0009 §EAGER_LOAD validator opt-in. Codified after |
| 36 | + * kendo PR #1084 (Armorer, merged 2026-05-07 at db20ea9cf) added a Pest |
| 37 | + * arch test of this shape; war-room enforcement queue #55 promoted the |
| 38 | + * Level-1 territory-local arch test to a Level-2 cross-territory PHPStan |
| 39 | + * rule on 2026-05-07. |
| 40 | + * |
| 41 | + * Scope: classes whose ancestor chain includes the configured base FQCN |
| 42 | + * (default: `App\Http\Resources\ResourceData`). Inheritance is matched |
| 43 | + * via PHPStan reflection — short-name collisions in unrelated namespaces |
| 44 | + * do not fire. |
| 45 | + * |
| 46 | + * Detection (all three must hold): |
| 47 | + * 1. Class transitively extends the configured base class. |
| 48 | + * 2. Class declares at least one of `EAGER_LOAD_COUNT` / `EAGER_LOAD_SUM` |
| 49 | + * as an own constant with a non-empty array value (empty array `[]` |
| 50 | + * is a no-op and does not fire). |
| 51 | + * 3. Class body does NOT contain a method-call or static-call with name |
| 52 | + * `validateRelationsLoaded` anywhere in any method (recursive walk). |
| 53 | + * |
| 54 | + * Compliant call shapes (any one suffices): |
| 55 | + * - `self::validateRelationsLoaded($model);` |
| 56 | + * - `static::validateRelationsLoaded($model);` |
| 57 | + * - `$this->validateRelationsLoaded($model);` (the base method is |
| 58 | + * `protected static`; the instance form is also accepted for |
| 59 | + * liberal compatibility with the source-of-truth Pest arch test). |
| 60 | + * |
| 61 | + * Implementation note: registers on `InClassNode` rather than `Class_` to |
| 62 | + * guarantee `$scope->getClassReflection()` is available — at the bare |
| 63 | + * `Class_` node, the scope has not yet entered the class so reflection |
| 64 | + * may be null. The AST walk is local; we only need to confirm presence |
| 65 | + * anywhere in the class body, not type-resolve receivers. |
| 66 | + * |
| 67 | + * @implements Rule<InClassNode> |
| 68 | + */ |
| 69 | +final class EnforceResourceDataValidatorOptInRule implements Rule |
| 70 | +{ |
| 71 | + private const array TARGET_CONSTANTS = [ |
| 72 | + 'EAGER_LOAD_COUNT', |
| 73 | + 'EAGER_LOAD_SUM', |
| 74 | + ]; |
| 75 | + |
| 76 | + private const string VALIDATOR_METHOD_NAME = 'validateRelationsLoaded'; |
| 77 | + |
| 78 | + public function __construct( |
| 79 | + private string $resourceDataBaseClass = 'App\Http\Resources\ResourceData', |
| 80 | + ) {} |
| 81 | + |
| 82 | + public function getNodeType(): string |
| 83 | + { |
| 84 | + return InClassNode::class; |
| 85 | + } |
| 86 | + |
| 87 | + public function processNode(Node $node, Scope $scope): array |
| 88 | + { |
| 89 | + $classNode = $node->getOriginalNode(); |
| 90 | + |
| 91 | + if (!$classNode instanceof Class_) { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + if ($classNode->isAbstract()) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + $classReflection = $node->getClassReflection(); |
| 100 | + |
| 101 | + if (!$this->extendsResourceDataBase($classReflection)) { |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + $declaredConstants = $this->declaredAggregateConstants($classNode); |
| 106 | + |
| 107 | + if ($declaredConstants === []) { |
| 108 | + return []; |
| 109 | + } |
| 110 | + |
| 111 | + if ($this->classBodyCallsValidator($classNode)) { |
| 112 | + return []; |
| 113 | + } |
| 114 | + |
| 115 | + return [ |
| 116 | + RuleErrorBuilder::message(sprintf( |
| 117 | + '%s declares %s but does not call validateRelationsLoaded() — silent-zero bug risk (ADR-0009 / war-room queue #55 / kendo PR #1084 opt-in invariant).', |
| 118 | + $classReflection->getName(), |
| 119 | + implode(', ', $declaredConstants), |
| 120 | + )) |
| 121 | + ->identifier('enforceResourceDataValidatorOptIn.missingValidatorCall') |
| 122 | + ->line($classNode->getStartLine()) |
| 123 | + ->build(), |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Inheritance gate: the class must be a (transitive) subclass of the |
| 129 | + * configured base FQCN. Uses PHPStan reflection — handles intermediate |
| 130 | + * abstract layers and namespace-relative `extends` clauses. Short-name |
| 131 | + * collisions in unrelated namespaces do not match. |
| 132 | + */ |
| 133 | + private function extendsResourceDataBase(ClassReflection $classReflection): bool |
| 134 | + { |
| 135 | + if ($classReflection->getName() === $this->resourceDataBaseClass) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + return $classReflection->isSubclassOf($this->resourceDataBaseClass); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns short names of TARGET_CONSTANTS declared on this class with a |
| 144 | + * non-empty array value. Inherited constants are skipped — only own |
| 145 | + * declarations on this class create the obligation. |
| 146 | + * |
| 147 | + * @return list<string> |
| 148 | + */ |
| 149 | + private function declaredAggregateConstants(Class_ $node): array |
| 150 | + { |
| 151 | + $declared = []; |
| 152 | + |
| 153 | + foreach ($node->stmts as $stmt) { |
| 154 | + if (!$stmt instanceof ClassConst) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + foreach ($stmt->consts as $const) { |
| 159 | + $name = $const->name->toString(); |
| 160 | + |
| 161 | + if (!in_array($name, self::TARGET_CONSTANTS, true)) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + if (!$const->value instanceof Array_) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + if ($const->value->items === []) { |
| 170 | + continue; |
| 171 | + } |
| 172 | + |
| 173 | + $declared[] = $name; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return $declared; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Walks every method in the class body looking for a method-call or |
| 182 | + * static-call to `validateRelationsLoaded`. Only the call's *name* is |
| 183 | + * checked — any receiver shape (`self::`, `static::`, `$this->`, |
| 184 | + * `parent::`, `Foo::`) suffices, mirroring the source-of-truth kendo |
| 185 | + * arch test's permissive matcher. |
| 186 | + */ |
| 187 | + private function classBodyCallsValidator(Class_ $node): bool |
| 188 | + { |
| 189 | + $found = false; |
| 190 | + |
| 191 | + foreach ($node->stmts as $stmt) { |
| 192 | + if (!$stmt instanceof ClassMethod) { |
| 193 | + continue; |
| 194 | + } |
| 195 | + |
| 196 | + if ($stmt->stmts === null) { |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + $this->walkNodes($stmt->stmts, function(Node $inner) use (&$found): void { |
| 201 | + if ($found) { |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + if ( |
| 206 | + $inner instanceof MethodCall |
| 207 | + && $inner->name instanceof Identifier |
| 208 | + && $inner->name->toString() === self::VALIDATOR_METHOD_NAME |
| 209 | + ) { |
| 210 | + $found = true; |
| 211 | + |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + if ( |
| 216 | + $inner instanceof StaticCall |
| 217 | + && $inner->name instanceof Identifier |
| 218 | + && $inner->name->toString() === self::VALIDATOR_METHOD_NAME |
| 219 | + ) { |
| 220 | + $found = true; |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + if ($found) { |
| 225 | + return true; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Recursively walk a list of nodes, invoking `$callback` on each one. |
| 234 | + * Mirrors `EnforceActionTransactionsRule::walkNodes()` / |
| 235 | + * `EnforceAuditSnapshotOnRetryRule::walkNodes()` for parity. |
| 236 | + * |
| 237 | + * @param array<int|string, Node|null> $nodes |
| 238 | + */ |
| 239 | + private function walkNodes(array $nodes, callable $callback): void |
| 240 | + { |
| 241 | + foreach ($nodes as $node) { |
| 242 | + if (!$node instanceof Node) { |
| 243 | + continue; |
| 244 | + } |
| 245 | + |
| 246 | + $callback($node); |
| 247 | + |
| 248 | + foreach ($node->getSubNodeNames() as $name) { |
| 249 | + $subNode = $node->{$name}; |
| 250 | + |
| 251 | + if ($subNode instanceof Node) { |
| 252 | + $this->walkNodes([$subNode], $callback); |
| 253 | + } elseif (is_array($subNode)) { |
| 254 | + $this->walkNodes( |
| 255 | + array_filter($subNode, static fn(mixed $item): bool => $item instanceof Node), |
| 256 | + $callback, |
| 257 | + ); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments