|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpStanMigrationRules\Rules\Laravel; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\ArrowFunction; |
| 10 | +use PhpParser\Node\Expr\Assign; |
| 11 | +use PhpParser\Node\Expr\Closure; |
| 12 | +use PhpParser\Node\Expr\MethodCall; |
| 13 | +use PhpParser\Node\Expr\PropertyFetch; |
| 14 | +use PhpParser\Node\Expr\StaticCall; |
| 15 | +use PhpParser\Node\Expr\Variable; |
| 16 | +use PhpParser\Node\Identifier; |
| 17 | +use PhpParser\Node\Name; |
| 18 | +use PhpParser\Node\Scalar\String_; |
| 19 | +use PHPStan\Analyser\Scope; |
| 20 | +use PHPStan\Rules\Rule; |
| 21 | +use PHPStan\Rules\RuleErrorBuilder; |
| 22 | + |
| 23 | +/** |
| 24 | + * @implements Rule<StaticCall> |
| 25 | + */ |
| 26 | +final readonly class EnforceCollationRule implements Rule |
| 27 | +{ |
| 28 | + private const string RULE_IDENTIFIER = 'laravel.schema.requiredCollation'; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly string $requiredCollation |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return StaticCall::class; |
| 38 | + } |
| 39 | + |
| 40 | + public function processNode(Node $node, Scope $scope): array |
| 41 | + { |
| 42 | + // Only apply inside Laravel migration classes. |
| 43 | + $classReflection = $scope->getClassReflection(); |
| 44 | + if ($classReflection === null) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + if (!$classReflection->isSubclassOf(\Illuminate\Database\Migrations\Migration::class)) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + $callName = $this->getStaticCallName($node); |
| 53 | + if ($callName !== 'create' && $callName !== 'table') { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + if (!$node->class instanceof Name) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + $resolved = $scope->resolveName($node->class); |
| 62 | + if ( |
| 63 | + $resolved !== \Illuminate\Support\Facades\Schema::class |
| 64 | + && $resolved !== 'Illuminate\\Database\\Schema\\Schema' |
| 65 | + ) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $closure = $this->extractBlueprintCallback($node); |
| 70 | + if ($closure === null) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + $tableVarName = $this->extractBlueprintParamName($closure); |
| 75 | + if ($tableVarName === null) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + $collation = $this->findCollationInClosure($closure, $tableVarName); |
| 80 | + |
| 81 | + if ($collation !== $this->requiredCollation) { |
| 82 | + return [ |
| 83 | + RuleErrorBuilder::message(sprintf( |
| 84 | + 'Laravel migrations must set table collation to "%s" in Schema::%s().', |
| 85 | + $this->requiredCollation, |
| 86 | + $callName |
| 87 | + )) |
| 88 | + ->identifier(self::RULE_IDENTIFIER) |
| 89 | + ->build(), |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + return []; |
| 94 | + } |
| 95 | + |
| 96 | + private function getStaticCallName(StaticCall $node): ?string |
| 97 | + { |
| 98 | + return $node->name instanceof Identifier ? $node->name->toString() : null; |
| 99 | + } |
| 100 | + |
| 101 | + private function extractBlueprintCallback(StaticCall $node): ?Closure |
| 102 | + { |
| 103 | + if (!isset($node->args[1]) || !$node->args[1] instanceof Arg) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + $value = $node->args[1]->value; |
| 108 | + return $value instanceof Closure ? $value : null; |
| 109 | + } |
| 110 | + |
| 111 | + private function extractBlueprintParamName(Closure $closure): ?string |
| 112 | + { |
| 113 | + if (!isset($closure->params[0])) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + $var = $closure->params[0]->var; |
| 118 | + if (!$var instanceof Variable) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + return is_string($var->name) ? $var->name : null; |
| 123 | + } |
| 124 | + |
| 125 | + private function findCollationInClosure(Closure $closure, string $tableVarName): ?string |
| 126 | + { |
| 127 | + $stmts = $closure->stmts; |
| 128 | + |
| 129 | + foreach ($stmts as $stmt) { |
| 130 | + $found = $this->scanNodeForCollation($stmt, $tableVarName); |
| 131 | + if ($found !== null) { |
| 132 | + return $found; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + private function scanNodeForCollation(Node $node, string $tableVarName): ?string |
| 140 | + { |
| 141 | + // Do not walk into nested functions; they can capture $table and create false positives. |
| 142 | + if ($node instanceof Closure || $node instanceof ArrowFunction) { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + // $table->collation('utf8mb4') |
| 147 | + if ( |
| 148 | + $node instanceof MethodCall |
| 149 | + && $node->name instanceof Identifier |
| 150 | + && $node->name->toString() === 'collation' |
| 151 | + && $this->isTableVar($node->var, $tableVarName) |
| 152 | + && isset($node->args[0]) |
| 153 | + && $node->args[0] instanceof Arg |
| 154 | + && $node->args[0]->value instanceof String_ |
| 155 | + ) { |
| 156 | + return $node->args[0]->value->value; |
| 157 | + } |
| 158 | + |
| 159 | + // $table->collation = 'utf8mb4' |
| 160 | + if ( |
| 161 | + $node instanceof Assign |
| 162 | + && $node->var instanceof PropertyFetch |
| 163 | + && $node->var->name instanceof Identifier |
| 164 | + && $node->var->name->toString() === 'collation' |
| 165 | + && $this->isTableVar($node->var->var, $tableVarName) |
| 166 | + && $node->expr instanceof String_ |
| 167 | + ) { |
| 168 | + return $node->expr->value; |
| 169 | + } |
| 170 | + |
| 171 | + // Recurse into child nodes |
| 172 | + foreach ($node->getSubNodeNames() as $subNodeName) { |
| 173 | + $subNode = $node->$subNodeName; |
| 174 | + |
| 175 | + if ($subNode instanceof Node) { |
| 176 | + $found = $this->scanNodeForCollation($subNode, $tableVarName); |
| 177 | + if ($found !== null) { |
| 178 | + return $found; |
| 179 | + } |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + if (is_array($subNode)) { |
| 184 | + foreach ($subNode as $item) { |
| 185 | + if ($item instanceof Node) { |
| 186 | + $found = $this->scanNodeForCollation($item, $tableVarName); |
| 187 | + if ($found !== null) { |
| 188 | + return $found; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + private function isTableVar(Node $node, string $tableVarName): bool |
| 199 | + { |
| 200 | + if (!$node instanceof Variable) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + return is_string($node->name) && $node->name === $tableVarName; |
| 205 | + } |
| 206 | +} |
0 commit comments