-
Notifications
You must be signed in to change notification settings - Fork 554
/
LegacyTableConfig.php
65 lines (54 loc) · 1.71 KB
/
LegacyTableConfig.php
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
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace A17\Twill\Rector;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use PhpParser\Node;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class LegacyTableConfig extends LaravelAwareRectorRule
{
protected array $tables;
public function configure(array $configuration): void
{
$this->tables = $configuration['tables'] ?? [];
parent::configure($configuration);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add back legacy tables to the config',
[]
);
}
public function getNodeTypes(): array
{
return [Node\Stmt\Return_::class];
}
public function refactor(Node $node)
{
assert($node instanceof Node\Stmt\Return_);
assert($node->expr instanceof Node\Expr\Array_);
// Keep tables that cannot be found in the DB.
$tables = [];
foreach ($this->tables as $name => $table) {
if (!Schema::hasTable(Config::get('twill.' . $name))) {
$tables[$name] = $table;
}
}
// Remove tables already present in the config.
foreach ($node->expr->items as $item) {
if (
$item?->key instanceof Node\Scalar\String_
&& array_key_exists($item?->key->value, $tables)
) {
unset($tables[$item?->key->value]);
}
}
foreach ($tables as $name => $table) {
$node->expr->items[] = new Node\Expr\ArrayItem(
new Node\Scalar\String_($table),
new Node\Scalar\String_($name),
);
}
return $node;
}
}