Skip to content

Commit ec1ff9d

Browse files
committed
Update DatabaseChecker.php
1 parent 6542ed4 commit ec1ff9d

File tree

1 file changed

+88
-81
lines changed

1 file changed

+88
-81
lines changed

src/Ubiquity/orm/reverse/DatabaseChecker.php

Lines changed: 88 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class DatabaseChecker {
2626

2727
private Database $db;
2828

29-
private array $models;
29+
private ?array $models = null;
3030

31-
private array $metadatas;
31+
private ?array $metadatas = null;
3232

33-
private array $nonExistingTables;
33+
private ?array $nonExistingTables = null;
3434

35-
private array $checkResults;
35+
private ?array $checkResults = null;
3636

3737
public function __construct(string $dbOffset = 'default') {
3838
$this->dbOffset = $dbOffset;
@@ -45,9 +45,9 @@ public function __construct(string $dbOffset = 'default') {
4545
public function checkDatabase(): bool {
4646
try {
4747
$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;
5151
}
5252
}
5353

@@ -57,9 +57,9 @@ public function getNonExistingTables(): array {
5757
$existingTables = $this->db->getTablesName();
5858
}
5959
$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) {
6363
$tables[$model] = $tablename;
6464
}
6565
}
@@ -71,7 +71,7 @@ private function _getNonExistingTables() {
7171
}
7272

7373
protected function tableExists(string $table): bool {
74-
return \array_search($table,$this->_getNonExistingTables())===false;
74+
return \array_search($table, $this->_getNonExistingTables()) === false;
7575
}
7676

7777
public function checkAll(): array {
@@ -101,46 +101,46 @@ public function checkAll(): array {
101101
$result['manyToMany'][$tableName] = $manyToManyUpdateds;
102102
}
103103
}
104-
return $this->checkResults=$result;
104+
return $this->checkResults = $result;
105105
}
106106

107107
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);
111111
}
112112
return false;
113113
}
114114

115-
public function getResultDatabaseNotExist():bool{
116-
return $this->checkResults['database']??false;
115+
public function getResultDatabaseNotExist(): bool {
116+
return $this->checkResults['database'] ?? false;
117117
}
118118

119-
public function getResultNonExistingTables():array{
120-
return $this->checkResults['nonExistingTables']??[];
119+
public function getResultNonExistingTables(): array {
120+
return $this->checkResults['nonExistingTables'] ?? [];
121121
}
122122

123-
public function getResultUpdatedFields():array{
124-
return $this->checkResults['updatedFields']??[];
123+
public function getResultUpdatedFields(): array {
124+
return $this->checkResults['updatedFields'] ?? [];
125125
}
126-
127-
public function getResultPrimaryKeys():array{
128-
return $this->checkResults['pks']??[];
126+
127+
public function getResultPrimaryKeys(): array {
128+
return $this->checkResults['pks'] ?? [];
129129
}
130130

131-
public function getResultManyToOne():array{
132-
return $this->checkResults['manyToOne']??[];
131+
public function getResultManyToOne(): array {
132+
return $this->checkResults['manyToOne'] ?? [];
133133
}
134134

135-
public function getResultManyToMany():array{
136-
return $this->checkResults['manyToMany']??[];
135+
public function getResultManyToMany(): array {
136+
return $this->checkResults['manyToMany'] ?? [];
137137
}
138138

139139
public function getUpdatedFields(string $model): array {
140140
$result = [];
141141
$metadatas = $this->metadatas[$model];
142-
$tableName=$metadatas['#tableName'];
143-
if($this->tableExists($tableName)) {
142+
$tableName = $metadatas['#tableName'];
143+
if ($this->tableExists($tableName)) {
144144
$fields = $metadatas['#fieldNames'];
145145
$fieldTypes = $metadatas['#fieldTypes'];
146146
$nullables = $metadatas['#nullable'];
@@ -155,31 +155,34 @@ public function getUpdatedFields(string $model): array {
155155
$fieldInfos = [
156156
'table' => $tableName,
157157
'name' => $field,
158-
'attributes' => ['type' => $fieldTypes[$member], 'extra' => $nullable ? '' : 'NOT NULL']
158+
'attributes' => [
159+
'type' => $fieldTypes[$member],
160+
'extra' => $nullable ? '' : 'NOT NULL'
161+
]
159162
];
160-
if (!isset($originalFieldInfos[$field])) {
163+
if (! isset($originalFieldInfos[$field])) {
161164
$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)) {
163166
$result['updated'][$model][] = $fieldInfos;
164167
}
165168
}
166169
}
167170
}
168171
return $result;
169172
}
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);
175178
}
176-
return \implode($sep,$results);
179+
return \implode($sep, $results);
177180
}
178181

179182
public function checkPrimaryKeys(string $model): array {
180183
$metadatas = $this->metadatas[$model];
181184
$tableName = $metadatas['#tableName'];
182-
if($this->tableExists($tableName)) {
185+
if ($this->tableExists($tableName)) {
183186
$pks = $metadatas['#primaryKeys'];
184187
$originalPks = [];
185188
if ($this->databaseExist) {
@@ -188,7 +191,11 @@ public function checkPrimaryKeys(string $model): array {
188191
if (\is_array($pks)) {
189192
foreach ($pks as $pk) {
190193
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+
];
192199
}
193200
}
194201
}
@@ -202,7 +209,7 @@ public function checkManyToOne(string $model): array {
202209
$joinColumns = $metadatas['#joinColumn'] ?? [];
203210
$table = $metadatas['#tableName'];
204211
$result = [];
205-
if($this->tableExists($table)) {
212+
if ($this->tableExists($table)) {
206213
foreach ($manyToOnes as $manyToOneMember) {
207214
$joinColumn = $joinColumns[$manyToOneMember];
208215
$fkClass = $joinColumn['className'];
@@ -243,9 +250,9 @@ public function checkManyToMany(string $model): array {
243250
$metadatas = $this->metadatas[$model];
244251
$manyToManys = $metadatas['#manyToMany'] ?? [];
245252
$joinTables = $metadatas['#joinTable'] ?? [];
246-
$table=$metadatas['#tableName'];
253+
$table = $metadatas['#tableName'];
247254
$result = [];
248-
if($this->tableExists($table)) {
255+
if ($this->tableExists($table)) {
249256
foreach ($manyToManys as $member => $manyToManyInfos) {
250257
$joinTableInfos = $joinTables[$member];
251258
$joinTableName = $joinTableInfos['name'];
@@ -261,74 +268,74 @@ public function checkManyToMany(string $model): array {
261268
}
262269

263270
/**
271+
*
264272
* @return Database
265273
*/
266274
public function getDb(): Database {
267275
return $this->db;
268276
}
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!");
275283
}
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.");
283291
}
284292
}
285293
}
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'])) {
289297
$model = \array_key_first($updatedFieldInfos['missing']);
290298
if (\count($fInfos = $updatedFieldInfos['missing'][$model] ?? []) > 0) {
291-
$names = $this->concatArrayKeyValue($fInfos,function($value){
299+
$names = $this->concatArrayKeyValue($fInfos, function ($value) {
292300
return $value['name'];
293301
});
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>");
295303
}
296304
}
297-
if(isset($updatedFieldInfos['updated'])) {
305+
if (isset($updatedFieldInfos['updated'])) {
298306
$model = \array_key_first($updatedFieldInfos['updated']);
299307
if (\count($fInfos = $updatedFieldInfos['updated'][$model] ?? []) > 0) {
300-
$names = $this->concatArrayKeyValue($fInfos,function($value){
308+
$names = $this->concatArrayKeyValue($fInfos, function ($value) {
301309
return $value['name'];
302310
});
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>");
304312
}
305313
}
306314
}
307315
}
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>");
313321
}
314322
}
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'];
319327
});
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>");
321329
}
322330
}
323331

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'];
328336
});
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>");
330338
}
331339
}
332340
}
333-
334341
}

0 commit comments

Comments
 (0)