-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_all_factories_now.php
More file actions
54 lines (43 loc) · 2.04 KB
/
fix_all_factories_now.php
File metadata and controls
54 lines (43 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
use Illuminate\Support\Facades\DB;
// Get all factories
$factories = glob('database/factories/Domains/**/*Factory.php');
$fixes = 0;
foreach ($factories as $factory_path) {
$content = file_get_contents($factory_path);
// Extract model class
if (preg_match('/protected \$model = ([^:]+)::class;/', $content, $matches)) {
$model_class = trim($matches[1], '\\');
if (class_exists($model_class)) {
try {
$model = new $model_class;
$table = $model->getTable();
// Get actual columns from database
$columns = DB::getSchemaBuilder()->getColumnListing($table);
// Extract factory definition
if (preg_match('/public function definition\(\): array\s*\{(.*?)\}/s', $content, $def_matches)) {
$definition = $def_matches[1];
// Find columns used in factory
preg_match_all("/'([^']+)'\s*=>/", $definition, $factory_cols);
$factory_columns = array_unique($factory_cols[1]);
// Find invalid columns
$invalid = array_diff($factory_columns, $columns);
if (!empty($invalid)) {
echo "FIXING: $factory_path\n";
echo " Model: $model_class\n";
echo " Table: $table\n";
echo " Invalid columns: " . implode(', ', $invalid) . "\n";
echo " Valid columns: " . implode(', ', $columns) . "\n";
$fixes++;
}
}
} catch (\Exception $e) {
// Skip if model can't be instantiated
}
}
}
}
echo "\nTotal factories needing fixes: $fixes\n";