|
7 | 7 | use Exception;
|
8 | 8 | use Illuminate\Database\Eloquent\Model;
|
9 | 9 | use Illuminate\Support\Str;
|
| 10 | +use Larastan\Larastan\Support\ModelHelper; |
10 | 11 | use PhpParser;
|
11 | 12 | use PhpParser\NodeFinder;
|
12 |
| -use PHPStan\Reflection\ReflectionProvider; |
13 | 13 | use PHPStan\Type\ObjectType;
|
14 |
| -use ReflectionException; |
15 | 14 |
|
16 | 15 | use function array_key_exists;
|
17 | 16 | use function array_merge;
|
| 17 | +use function assert; |
18 | 18 | use function class_basename;
|
19 | 19 | use function count;
|
| 20 | +use function is_a; |
20 | 21 | use function is_string;
|
21 | 22 | use function property_exists;
|
22 | 23 | use function strtolower;
|
|
25 | 26 | final class SchemaAggregator
|
26 | 27 | {
|
27 | 28 | /** @param array<string, SchemaTable> $tables */
|
28 |
| - public function __construct(private ReflectionProvider $reflectionProvider, public array $tables = []) |
| 29 | + public function __construct(private ModelHelper $modelHelper, public array $tables = []) |
29 | 30 | {
|
30 | 31 | }
|
31 | 32 |
|
@@ -128,7 +129,8 @@ private function alterTable(PhpParser\Node\Expr\StaticCall|PhpParser\Node\Expr\M
|
128 | 129 | ! isset($call->args[1])
|
129 | 130 | || ! $call->getArgs()[1]->value instanceof PhpParser\Node\Expr\Closure
|
130 | 131 | || count($call->getArgs()[1]->value->params) < 1
|
131 |
| - || ($call->getArgs()[1]->value->params[0]->type instanceof PhpParser\Node\Name |
| 132 | + || ( |
| 133 | + $call->getArgs()[1]->value->params[0]->type instanceof PhpParser\Node\Name |
132 | 134 | && ! (new ObjectType('Illuminate\Database\Schema\Blueprint'))->isSuperTypeOf(new ObjectType($call->getArgs()[1]->value->params[0]->type->toCodeString()))->yes()
|
133 | 135 | )
|
134 | 136 | ) {
|
@@ -213,6 +215,8 @@ private function processColumnUpdates(string $tableName, string $argName, array
|
213 | 215 | continue;
|
214 | 216 | }
|
215 | 217 |
|
| 218 | + assert(is_a($modelClass, Model::class, true)); |
| 219 | + |
216 | 220 | $columnName = Str::snake(class_basename($modelClass)) . '_id';
|
217 | 221 | if ($secondArg instanceof PhpParser\Node\Scalar\String_) {
|
218 | 222 | $columnName = $secondArg->value;
|
@@ -381,17 +385,11 @@ private function renameTable(string $oldTableName, string $newTableName): void
|
381 | 385 | $this->tables[$newTableName] = $table;
|
382 | 386 | }
|
383 | 387 |
|
| 388 | + /** @param class-string<Model> $modelClass */ |
384 | 389 | private function getModelReferenceType(string $modelClass): string|null
|
385 | 390 | {
|
386 |
| - $classReflection = $this->reflectionProvider->getClass($modelClass); |
387 |
| - try { |
388 |
| - /** @var Model $modelInstance */ |
389 |
| - $modelInstance = $classReflection->getNativeReflection()->newInstanceWithoutConstructor(); |
390 |
| - } catch (ReflectionException) { |
391 |
| - return null; |
392 |
| - } |
393 |
| - |
394 |
| - $tableName = $modelInstance->getTable(); |
| 391 | + $modelInstance = $this->modelHelper->getModelInstance($modelClass); |
| 392 | + $tableName = $modelInstance->getTable(); |
395 | 393 |
|
396 | 394 | if (! array_key_exists($tableName, $this->tables)) {
|
397 | 395 | return null;
|
|
0 commit comments