|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\App\CodeChecker; |
|
25
|
|
|
|
|
26
|
|
|
use PhpParser\Node; |
|
27
|
|
|
use PhpParser\Node\Name; |
|
28
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
29
|
|
|
|
|
30
|
|
|
class MigrationSchemaChecker extends NodeVisitorAbstract { |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $schemaVariableName = null; |
|
34
|
|
|
/** @var array */ |
|
35
|
|
|
protected $tableVariableNames = []; |
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
public $errors = []; |
|
38
|
|
|
|
|
39
|
|
|
public function enterNode(Node $node) { |
|
40
|
|
|
/** |
|
41
|
|
|
* Check tables |
|
42
|
|
|
*/ |
|
43
|
|
|
if ($this->schemaVariableName !== null && |
|
44
|
|
|
$node instanceof Node\Expr\Assign && |
|
|
|
|
|
|
45
|
|
|
$node->var instanceof Node\Expr\Variable && |
|
|
|
|
|
|
46
|
|
|
$node->expr instanceof Node\Expr\MethodCall && |
|
|
|
|
|
|
47
|
|
|
$node->expr->var instanceof Node\Expr\Variable && |
|
|
|
|
|
|
48
|
|
|
$node->expr->var->name === $this->schemaVariableName) { |
|
49
|
|
|
|
|
50
|
|
|
if ($node->expr->name === 'createTable') { |
|
51
|
|
|
if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
52
|
|
|
if (!$this->checkNameLength($node->expr->args[0]->value->value)) { |
|
53
|
|
|
$this->errors[] = [ |
|
54
|
|
|
'line' => $node->getLine(), |
|
55
|
|
|
'disallowedToken' => $node->expr->args[0]->value->value, |
|
56
|
|
|
'reason' => 'Table name is too long (max. 27)', |
|
57
|
|
|
]; |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} else if ($node->expr->name === 'getTable') { |
|
63
|
|
|
if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
64
|
|
|
$this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} else if ($this->schemaVariableName !== null && |
|
68
|
|
|
$node instanceof Node\Expr\MethodCall && |
|
|
|
|
|
|
69
|
|
|
$node->var instanceof Node\Expr\Variable && |
|
|
|
|
|
|
70
|
|
|
$node->var->name === $this->schemaVariableName) { |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
if ($node->name === 'renameTable') { |
|
73
|
|
|
$this->errors[] = [ |
|
74
|
|
|
'line' => $node->getLine(), |
|
75
|
|
|
'disallowedToken' => 'Deprecated method', |
|
76
|
|
|
'reason' => sprintf( |
|
77
|
|
|
'`$%s->renameTable()` must not be used', |
|
78
|
|
|
$node->var->name |
|
79
|
|
|
), |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check columns and Indexes |
|
85
|
|
|
*/ |
|
86
|
|
|
} else if (!empty($this->tableVariableNames) && |
|
87
|
|
|
$node instanceof Node\Expr\MethodCall && |
|
|
|
|
|
|
88
|
|
|
$node->var instanceof Node\Expr\Variable && |
|
|
|
|
|
|
89
|
|
|
isset($this->tableVariableNames[$node->var->name])) { |
|
90
|
|
|
|
|
91
|
|
|
if ($node->name === 'addColumn' || $node->name === 'changeColumn') { |
|
92
|
|
|
if (isset($node->args[0]) && $node->args[0]->value instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
93
|
|
|
if (!$this->checkNameLength($node->args[0]->value->value)) { |
|
94
|
|
|
$this->errors[] = [ |
|
95
|
|
|
'line' => $node->getLine(), |
|
96
|
|
|
'disallowedToken' => $node->args[0]->value->value, |
|
97
|
|
|
'reason' => sprintf( |
|
98
|
|
|
'Column name is too long on table `%s` (max. 27)', |
|
99
|
|
|
$this->tableVariableNames[$node->var->name] |
|
100
|
|
|
), |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// On autoincrement the max length of the table name is 21 instead of 27 |
|
105
|
|
|
if (isset($node->args[2]) && $node->args[2]->value instanceof Node\Expr\Array_) { |
|
|
|
|
|
|
106
|
|
|
/** @var Node\Expr\Array_ $options */ |
|
107
|
|
|
$options = $node->args[2]->value; |
|
108
|
|
|
if ($this->checkColumnForAutoincrement($options)) { |
|
109
|
|
|
if (!$this->checkNameLength($this->tableVariableNames[$node->var->name], true)) { |
|
110
|
|
|
$this->errors[] = [ |
|
111
|
|
|
'line' => $node->getLine(), |
|
112
|
|
|
'disallowedToken' => $this->tableVariableNames[$node->var->name], |
|
113
|
|
|
'reason' => 'Table name is too long because of autoincrement (max. 21)', |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} else if ($node->name === 'addIndex' || |
|
120
|
|
|
$node->name === 'addUniqueIndex' || |
|
121
|
|
|
$node->name === 'renameIndex' || |
|
122
|
|
|
$node->name === 'setPrimaryKey') { |
|
123
|
|
View Code Duplication |
if (isset($node->args[1]) && $node->args[1]->value instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
124
|
|
|
if (!$this->checkNameLength($node->args[1]->value->value)) { |
|
125
|
|
|
$this->errors[] = [ |
|
126
|
|
|
'line' => $node->getLine(), |
|
127
|
|
|
'disallowedToken' => $node->args[1]->value->value, |
|
128
|
|
|
'reason' => sprintf( |
|
129
|
|
|
'Index name is too long on table `%s` (max. 27)', |
|
130
|
|
|
$this->tableVariableNames[$node->var->name] |
|
131
|
|
|
), |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} else if ($node->name === 'addForeignKeyConstraint') { |
|
136
|
|
View Code Duplication |
if (isset($node->args[4]) && $node->args[4]->value instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
137
|
|
|
if (!$this->checkNameLength($node->args[4]->value->value)) { |
|
138
|
|
|
$this->errors[] = [ |
|
139
|
|
|
'line' => $node->getLine(), |
|
140
|
|
|
'disallowedToken' => $node->args[4]->value->value, |
|
141
|
|
|
'reason' => sprintf( |
|
142
|
|
|
'Constraint name is too long on table `%s` (max. 27)', |
|
143
|
|
|
$this->tableVariableNames[$node->var->name] |
|
144
|
|
|
), |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
View Code Duplication |
} else if ($node->name === 'renameColumn') { |
|
149
|
|
|
$this->errors[] = [ |
|
150
|
|
|
'line' => $node->getLine(), |
|
151
|
|
|
'disallowedToken' => 'Deprecated method', |
|
152
|
|
|
'reason' => sprintf( |
|
153
|
|
|
'`$%s->renameColumn()` must not be used', |
|
154
|
|
|
$node->var->name |
|
155
|
|
|
), |
|
156
|
|
|
]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Find the schema |
|
161
|
|
|
*/ |
|
162
|
|
|
} else if ($node instanceof Node\Expr\Assign && |
|
|
|
|
|
|
163
|
|
|
$node->expr instanceof Node\Expr\FuncCall && |
|
|
|
|
|
|
164
|
|
|
$node->var instanceof Node\Expr\Variable && |
|
|
|
|
|
|
165
|
|
|
$node->expr->name instanceof Node\Expr\Variable && |
|
|
|
|
|
|
166
|
|
|
$node->expr->name->name === 'schemaClosure') { |
|
167
|
|
|
// E.g. $schema = $schemaClosure(); |
|
|
|
|
|
|
168
|
|
|
$this->schemaVariableName = $node->var->name; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
protected function checkNameLength($tableName, $hasAutoincrement = false) { |
|
173
|
|
|
if ($hasAutoincrement) { |
|
174
|
|
|
return strlen($tableName) <= 21; |
|
175
|
|
|
} |
|
176
|
|
|
return strlen($tableName) <= 27; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param Node\Expr\Array_ $optionsArray |
|
181
|
|
|
* @return bool Whether the column is an autoincrement column |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function checkColumnForAutoincrement(Node\Expr\Array_ $optionsArray) { |
|
184
|
|
|
foreach ($optionsArray->items as $option) { |
|
185
|
|
|
if ($option->key instanceof Node\Scalar\String_) { |
|
|
|
|
|
|
186
|
|
|
if ($option->key->value === 'autoincrement' && |
|
187
|
|
|
$option->value instanceof Node\Expr\ConstFetch) { |
|
|
|
|
|
|
188
|
|
|
/** @var Node\Expr\ConstFetch $const */ |
|
189
|
|
|
$const = $option->value; |
|
190
|
|
|
|
|
191
|
|
|
if ($const->name instanceof Name && |
|
|
|
|
|
|
192
|
|
|
$const->name->parts === ['true']) { |
|
193
|
|
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.