@@ -26,13 +26,13 @@ class DatabaseChecker {
26
26
27
27
private Database $ db ;
28
28
29
- private array $ models ;
29
+ private ? array $ models = null ;
30
30
31
- private array $ metadatas ;
31
+ private ? array $ metadatas = null ;
32
32
33
- private array $ nonExistingTables ;
33
+ private ? array $ nonExistingTables = null ;
34
34
35
- private array $ checkResults ;
35
+ private ? array $ checkResults = null ;
36
36
37
37
public function __construct (string $ dbOffset = 'default ' ) {
38
38
$ this ->dbOffset = $ dbOffset ;
@@ -45,9 +45,9 @@ public function __construct(string $dbOffset = 'default') {
45
45
public function checkDatabase (): bool {
46
46
try {
47
47
$ this ->db = DAO ::getDatabase ($ this ->dbOffset );
48
- return $ this ->databaseExist = isset ($ this ->db ) && $ this ->db ->isConnected ();
49
- }catch (DAOException $ e ){
50
- return $ this ->databaseExist = false ;
48
+ return $ this ->databaseExist = isset ($ this ->db ) && $ this ->db ->isConnected ();
49
+ } catch (DAOException $ e ) {
50
+ return $ this ->databaseExist = false ;
51
51
}
52
52
}
53
53
@@ -57,9 +57,9 @@ public function getNonExistingTables(): array {
57
57
$ existingTables = $ this ->db ->getTablesName ();
58
58
}
59
59
$ tables = Reflexion::getAllJoinTables ($ this ->models );
60
- foreach ($ this ->metadatas as $ model=> $ metas ) {
61
- $ tablename= $ metas ['#tableName ' ];
62
- if (\array_search ($ tablename ,$ existingTables )===false && \array_search ($ tablename ,$ tables )===false ) {
60
+ foreach ($ this ->metadatas as $ model => $ metas ) {
61
+ $ tablename = $ metas ['#tableName ' ];
62
+ if (\array_search ($ tablename , $ existingTables ) === false && \array_search ($ tablename , $ tables ) === false ) {
63
63
$ tables [$ model ] = $ tablename ;
64
64
}
65
65
}
@@ -71,7 +71,7 @@ private function _getNonExistingTables() {
71
71
}
72
72
73
73
protected function tableExists (string $ table ): bool {
74
- return \array_search ($ table ,$ this ->_getNonExistingTables ())===false ;
74
+ return \array_search ($ table , $ this ->_getNonExistingTables ()) === false ;
75
75
}
76
76
77
77
public function checkAll (): array {
@@ -101,46 +101,46 @@ public function checkAll(): array {
101
101
$ result ['manyToMany ' ][$ tableName ] = $ manyToManyUpdateds ;
102
102
}
103
103
}
104
- return $ this ->checkResults = $ result ;
104
+ return $ this ->checkResults = $ result ;
105
105
}
106
106
107
107
public function hasErrors (): bool {
108
- if (\is_array ($ this ->checkResults )){
109
- $ ckR= $ this ->checkResults ;
110
- return ($ ckR ['database ' ]?? false ) || ($ ckR ['nonExistingTables ' ]?? false ) || ($ ckR ['updatedFields ' ]?? false ) || ($ ckR ['pks ' ]?? false ) || ($ ckR ['manyToOne ' ]?? false ) || ($ ckR ['manyToMany ' ]?? false );
108
+ if (\is_array ($ this ->checkResults )) {
109
+ $ ckR = $ this ->checkResults ;
110
+ return ($ ckR ['database ' ] ?? false ) || ($ ckR ['nonExistingTables ' ] ?? false ) || ($ ckR ['updatedFields ' ] ?? false ) || ($ ckR ['pks ' ] ?? false ) || ($ ckR ['manyToOne ' ] ?? false ) || ($ ckR ['manyToMany ' ] ?? false );
111
111
}
112
112
return false ;
113
113
}
114
114
115
- public function getResultDatabaseNotExist ():bool {
116
- return $ this ->checkResults ['database ' ]?? false ;
115
+ public function getResultDatabaseNotExist (): bool {
116
+ return $ this ->checkResults ['database ' ] ?? false ;
117
117
}
118
118
119
- public function getResultNonExistingTables ():array {
120
- return $ this ->checkResults ['nonExistingTables ' ]?? [];
119
+ public function getResultNonExistingTables (): array {
120
+ return $ this ->checkResults ['nonExistingTables ' ] ?? [];
121
121
}
122
122
123
- public function getResultUpdatedFields ():array {
124
- return $ this ->checkResults ['updatedFields ' ]?? [];
123
+ public function getResultUpdatedFields (): array {
124
+ return $ this ->checkResults ['updatedFields ' ] ?? [];
125
125
}
126
-
127
- public function getResultPrimaryKeys ():array {
128
- return $ this ->checkResults ['pks ' ]?? [];
126
+
127
+ public function getResultPrimaryKeys (): array {
128
+ return $ this ->checkResults ['pks ' ] ?? [];
129
129
}
130
130
131
- public function getResultManyToOne ():array {
132
- return $ this ->checkResults ['manyToOne ' ]?? [];
131
+ public function getResultManyToOne (): array {
132
+ return $ this ->checkResults ['manyToOne ' ] ?? [];
133
133
}
134
134
135
- public function getResultManyToMany ():array {
136
- return $ this ->checkResults ['manyToMany ' ]?? [];
135
+ public function getResultManyToMany (): array {
136
+ return $ this ->checkResults ['manyToMany ' ] ?? [];
137
137
}
138
138
139
139
public function getUpdatedFields (string $ model ): array {
140
140
$ result = [];
141
141
$ metadatas = $ this ->metadatas [$ model ];
142
- $ tableName= $ metadatas ['#tableName ' ];
143
- if ($ this ->tableExists ($ tableName )) {
142
+ $ tableName = $ metadatas ['#tableName ' ];
143
+ if ($ this ->tableExists ($ tableName )) {
144
144
$ fields = $ metadatas ['#fieldNames ' ];
145
145
$ fieldTypes = $ metadatas ['#fieldTypes ' ];
146
146
$ nullables = $ metadatas ['#nullable ' ];
@@ -155,31 +155,34 @@ public function getUpdatedFields(string $model): array {
155
155
$ fieldInfos = [
156
156
'table ' => $ tableName ,
157
157
'name ' => $ field ,
158
- 'attributes ' => ['type ' => $ fieldTypes [$ member ], 'extra ' => $ nullable ? '' : 'NOT NULL ' ]
158
+ 'attributes ' => [
159
+ 'type ' => $ fieldTypes [$ member ],
160
+ 'extra ' => $ nullable ? '' : 'NOT NULL '
161
+ ]
159
162
];
160
- if (!isset ($ originalFieldInfos [$ field ])) {
163
+ if (! isset ($ originalFieldInfos [$ field ])) {
161
164
$ result ['missing ' ][$ model ][] = $ fieldInfos ;
162
- } elseif ($ fieldTypes [$ member ] !== 'mixed ' && ($ fieldTypes [$ member ] !== $ originalFieldInfos [$ field ]['Type ' ]) || ($ originalFieldInfos [$ field ]['Nullable ' ] !== 'NO ' && !$ nullable )) {
165
+ } elseif ($ fieldTypes [$ member ] !== 'mixed ' && ($ fieldTypes [$ member ] !== $ originalFieldInfos [$ field ]['Type ' ]) || ($ originalFieldInfos [$ field ]['Nullable ' ] !== 'NO ' && ! $ nullable )) {
163
166
$ result ['updated ' ][$ model ][] = $ fieldInfos ;
164
167
}
165
168
}
166
169
}
167
170
}
168
171
return $ result ;
169
172
}
170
-
171
- public function concatArrayKeyValue (array $ array ,callable $ callable ,string $ sep= ', ' ){
172
- $ results= [];
173
- foreach ($ array as $ value ){
174
- $ results []= $ callable ($ value );
173
+
174
+ public function concatArrayKeyValue (array $ array , callable $ callable , string $ sep = ', ' ) {
175
+ $ results = [];
176
+ foreach ($ array as $ value ) {
177
+ $ results [] = $ callable ($ value );
175
178
}
176
- return \implode ($ sep ,$ results );
179
+ return \implode ($ sep , $ results );
177
180
}
178
181
179
182
public function checkPrimaryKeys (string $ model ): array {
180
183
$ metadatas = $ this ->metadatas [$ model ];
181
184
$ tableName = $ metadatas ['#tableName ' ];
182
- if ($ this ->tableExists ($ tableName )) {
185
+ if ($ this ->tableExists ($ tableName )) {
183
186
$ pks = $ metadatas ['#primaryKeys ' ];
184
187
$ originalPks = [];
185
188
if ($ this ->databaseExist ) {
@@ -188,7 +191,11 @@ public function checkPrimaryKeys(string $model): array {
188
191
if (\is_array ($ pks )) {
189
192
foreach ($ pks as $ pk ) {
190
193
if (\array_search ($ pk , $ originalPks ) === false ) {
191
- return ['table ' =>$ tableName ,'primaryKeys ' =>$ pks ,'model ' =>$ model ];
194
+ return [
195
+ 'table ' => $ tableName ,
196
+ 'primaryKeys ' => $ pks ,
197
+ 'model ' => $ model
198
+ ];
192
199
}
193
200
}
194
201
}
@@ -202,7 +209,7 @@ public function checkManyToOne(string $model): array {
202
209
$ joinColumns = $ metadatas ['#joinColumn ' ] ?? [];
203
210
$ table = $ metadatas ['#tableName ' ];
204
211
$ result = [];
205
- if ($ this ->tableExists ($ table )) {
212
+ if ($ this ->tableExists ($ table )) {
206
213
foreach ($ manyToOnes as $ manyToOneMember ) {
207
214
$ joinColumn = $ joinColumns [$ manyToOneMember ];
208
215
$ fkClass = $ joinColumn ['className ' ];
@@ -243,9 +250,9 @@ public function checkManyToMany(string $model): array {
243
250
$ metadatas = $ this ->metadatas [$ model ];
244
251
$ manyToManys = $ metadatas ['#manyToMany ' ] ?? [];
245
252
$ joinTables = $ metadatas ['#joinTable ' ] ?? [];
246
- $ table= $ metadatas ['#tableName ' ];
253
+ $ table = $ metadatas ['#tableName ' ];
247
254
$ result = [];
248
- if ($ this ->tableExists ($ table )) {
255
+ if ($ this ->tableExists ($ table )) {
249
256
foreach ($ manyToManys as $ member => $ manyToManyInfos ) {
250
257
$ joinTableInfos = $ joinTables [$ member ];
251
258
$ joinTableName = $ joinTableInfos ['name ' ];
@@ -261,74 +268,74 @@ public function checkManyToMany(string $model): array {
261
268
}
262
269
263
270
/**
271
+ *
264
272
* @return Database
265
273
*/
266
274
public function getDb (): Database {
267
275
return $ this ->db ;
268
276
}
269
-
270
- public function displayAll (callable $ displayCallable ){
271
- $ dbResults= $ this ->checkResults ;
272
-
273
- if (isset ($ dbResults ['database ' ])){
274
- $ displayCallable ('error ' ,'database ' , "The database at offset <b> " . $ dbResults ['database ' ] . "</b> does not exist! " );
277
+
278
+ public function displayAll (callable $ displayCallable ) {
279
+ $ dbResults = $ this ->checkResults ;
280
+
281
+ if (isset ($ dbResults ['database ' ])) {
282
+ $ displayCallable ('error ' , 'database ' , "The database at offset <b> " . $ dbResults ['database ' ] . "</b> does not exist! " );
275
283
}
276
- if (\count ($ notExistingTables= $ dbResults ['nonExistingTables ' ])> 0 ) {
277
- $ notExistingTables= \array_unique ($ notExistingTables );
278
- foreach ($ notExistingTables as $ model=> $ table ) {
279
- if (\is_string ($ model )) {
280
- $ displayCallable ('warning ' ,'Missing table ' , "The table <b> " . $ table . "</b> does not exist for the model <b> " . $ model . "</b>. " );
281
- }else {
282
- $ displayCallable ('warning ' ,'Missing table ' , "The table <b> " . $ table . "</b> does not exist. " );
284
+ if (\count ($ notExistingTables = $ dbResults ['nonExistingTables ' ]) > 0 ) {
285
+ $ notExistingTables = \array_unique ($ notExistingTables );
286
+ foreach ($ notExistingTables as $ model => $ table ) {
287
+ if (\is_string ($ model )) {
288
+ $ displayCallable ('warning ' , 'Missing table ' , "The table <b> " . $ table . "</b> does not exist for the model <b> " . $ model . "</b>. " );
289
+ } else {
290
+ $ displayCallable ('warning ' , 'Missing table ' , "The table <b> " . $ table . "</b> does not exist. " );
283
291
}
284
292
}
285
293
}
286
- if (\count ($ uFields= $ this ->getResultUpdatedFields ())> 0 ) {
287
- foreach ($ uFields as $ table=> $ updatedFieldInfos ){
288
- if (isset ($ updatedFieldInfos ['missing ' ])) {
294
+ if (\count ($ uFields = $ this ->getResultUpdatedFields ()) > 0 ) {
295
+ foreach ($ uFields as $ table => $ updatedFieldInfos ) {
296
+ if (isset ($ updatedFieldInfos ['missing ' ])) {
289
297
$ model = \array_key_first ($ updatedFieldInfos ['missing ' ]);
290
298
if (\count ($ fInfos = $ updatedFieldInfos ['missing ' ][$ model ] ?? []) > 0 ) {
291
- $ names = $ this ->concatArrayKeyValue ($ fInfos ,function ($ value ){
299
+ $ names = $ this ->concatArrayKeyValue ($ fInfos , function ($ value ) {
292
300
return $ value ['name ' ];
293
301
});
294
- $ displayCallable ('warning ' ,'Missing columns ' , "Missing fields in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
302
+ $ displayCallable ('warning ' , 'Missing columns ' , "Missing fields in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
295
303
}
296
304
}
297
- if (isset ($ updatedFieldInfos ['updated ' ])) {
305
+ if (isset ($ updatedFieldInfos ['updated ' ])) {
298
306
$ model = \array_key_first ($ updatedFieldInfos ['updated ' ]);
299
307
if (\count ($ fInfos = $ updatedFieldInfos ['updated ' ][$ model ] ?? []) > 0 ) {
300
- $ names = $ this ->concatArrayKeyValue ($ fInfos ,function ($ value ){
308
+ $ names = $ this ->concatArrayKeyValue ($ fInfos , function ($ value ) {
301
309
return $ value ['name ' ];
302
310
});
303
- $ displayCallable ('warning ' ,'Updated columns ' , "Updated fields in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
311
+ $ displayCallable ('warning ' , 'Updated columns ' , "Updated fields in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
304
312
}
305
313
}
306
314
}
307
315
}
308
- if (\count ($ pks= $ this ->getResultPrimaryKeys ())> 0 ) {
309
- foreach ($ pks as $ table=> $ pksFieldInfos ){
310
- $ model= $ pksFieldInfos ['model ' ];
311
- $ names= implode (', ' ,$ pksFieldInfos ['primaryKeys ' ]);
312
- $ displayCallable ('warning ' ,'Missing key ' , "Missing primary keys in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
316
+ if (\count ($ pks = $ this ->getResultPrimaryKeys ()) > 0 ) {
317
+ foreach ($ pks as $ table => $ pksFieldInfos ) {
318
+ $ model = $ pksFieldInfos ['model ' ];
319
+ $ names = implode (', ' , $ pksFieldInfos ['primaryKeys ' ]);
320
+ $ displayCallable ('warning ' , 'Missing key ' , "Missing primary keys in table <b>` $ table`</b> for the model <b>` $ model`</b>: <b>( $ names)</b> " );
313
321
}
314
322
}
315
- if (\count ($ manyToOnes= $ this ->getResultManyToOne ())> 0 ) {
316
- foreach ($ manyToOnes as $ table=> $ manyToOneFieldInfos ){
317
- $ names = $ this ->concatArrayKeyValue ($ manyToOneFieldInfos ,function ($ value ){
318
- return $ value ['table ' ]. '. ' . $ value ['column ' ]. ' => ' . $ value ['fkTable ' ]. '. ' . $ value ['fkId ' ];
323
+ if (\count ($ manyToOnes = $ this ->getResultManyToOne ()) > 0 ) {
324
+ foreach ($ manyToOnes as $ table => $ manyToOneFieldInfos ) {
325
+ $ names = $ this ->concatArrayKeyValue ($ manyToOneFieldInfos , function ($ value ) {
326
+ return $ value ['table ' ] . '. ' . $ value ['column ' ] . ' => ' . $ value ['fkTable ' ] . '. ' . $ value ['fkId ' ];
319
327
});
320
- $ displayCallable ('warning ' ,'Missing hashtag (manyToOne) ' , "Missing foreign keys in table <b>` $ table`</b> : <b>( $ names)</b> " );
328
+ $ displayCallable ('warning ' , 'Missing hashtag (manyToOne) ' , "Missing foreign keys in table <b>` $ table`</b> : <b>( $ names)</b> " );
321
329
}
322
330
}
323
331
324
- if (\count ($ manyToManys= $ this ->getResultManyToMany ())> 0 ) {
325
- foreach ($ manyToManys as $ table=> $ manyToManyFieldInfos ){
326
- $ names = $ this ->concatArrayKeyValue ($ manyToManyFieldInfos ,function ($ value ){
327
- return $ value ['table ' ]. '. ' . $ value ['column ' ]. ' => ' . $ value ['fkTable ' ]. '. ' . $ value ['fkId ' ];
332
+ if (\count ($ manyToManys = $ this ->getResultManyToMany ()) > 0 ) {
333
+ foreach ($ manyToManys as $ table => $ manyToManyFieldInfos ) {
334
+ $ names = $ this ->concatArrayKeyValue ($ manyToManyFieldInfos , function ($ value ) {
335
+ return $ value ['table ' ] . '. ' . $ value ['column ' ] . ' => ' . $ value ['fkTable ' ] . '. ' . $ value ['fkId ' ];
328
336
});
329
- $ displayCallable ('warning ' ,'Missing hashtag (manyToMany) ' , "Missing foreign keys for manyToMany with table <b>` $ table`</b> : <b>( $ names)</b> " );
337
+ $ displayCallable ('warning ' , 'Missing hashtag (manyToMany) ' , "Missing foreign keys for manyToMany with table <b>` $ table`</b> : <b>( $ names)</b> " );
330
338
}
331
339
}
332
340
}
333
-
334
341
}
0 commit comments