-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnforceCollationRule.php
More file actions
138 lines (112 loc) · 3.74 KB
/
EnforceCollationRule.php
File metadata and controls
138 lines (112 loc) · 3.74 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace PhpStanMigrationRules\Rules\Phinx;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
/**
* @extends PhinxRule<MethodCall>
*/
final class EnforceCollationRule extends PhinxRule
{
// Same identifier as Laravel
private const string RULE_IDENTIFIER = 'phinx.schema.requiredCollation';
private const string MESSAGE_MISSING =
'Required: table collation must be "%s". '
. 'Why: prevents environment-dependent defaults and keeps schema consistent. '
. 'Fix: set the table collation explicitly in the migration.';
private const string MESSAGE_WRONG =
'Required: table collation must be "%s". Found: "%s". '
. 'Why: prevents environment-dependent defaults and keeps schema consistent. '
. 'Fix: set the table collation explicitly in the migration.';
public function __construct(private readonly string $requiredCollation)
{
}
public function getNodeType(): string
{
return MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$this->isPhinxMigration($scope)) {
return [];
}
$methodName = $node->name instanceof Node\Identifier ? $node->name->toString() : null;
if ($methodName !== 'table') {
return [];
}
return $this->validateTableMethod($node, $scope);
}
/**
* @return list<IdentifierRuleError>
*/
private function validateTableMethod(MethodCall $node, Scope $scope): array
{
$args = $node->getArgs();
// Missing options argument
if (!isset($args[1])) {
return $this->missing($node);
}
$optionsArg = $args[1]->value;
if (!$optionsArg instanceof Array_) {
// options provided but not as array -> effectively missing for our policy
return $this->missing($node);
}
$collationValue = null;
foreach ($optionsArg->items as $item) {
if ($item->key === null) {
continue;
}
$keyValue = $this->getStringValue($item->key, $scope);
if ($keyValue !== 'collation') {
continue;
}
$collationValue = $this->getStringValue($item->value, $scope);
break;
}
if ($collationValue === null) {
return $this->missing($node);
}
if ($collationValue !== $this->requiredCollation) {
return $this->wrong($node, $collationValue);
}
return [];
}
/**
* @return list<IdentifierRuleError>
*/
private function missing(MethodCall $node): array
{
return [
RuleErrorBuilder::message(sprintf(self::MESSAGE_MISSING, $this->requiredCollation))
->line($node->getStartLine())
->identifier(self::RULE_IDENTIFIER)
->build(),
];
}
/**
* @return list<IdentifierRuleError>
*/
private function wrong(MethodCall $node, string $found): array
{
return [
RuleErrorBuilder::message(sprintf(self::MESSAGE_WRONG, $this->requiredCollation, $found))
->line($node->getStartLine())
->identifier(self::RULE_IDENTIFIER)
->build(),
];
}
private function getStringValue(Expr $node, Scope $scope): ?string
{
$type = $scope->getType($node);
$constants = $type->getConstantStrings();
if ($constants === []) {
return null;
}
return $constants[0]->getValue();
}
}