Skip to content

Commit d6b72de

Browse files
committed
Improve return type of Index::getPrimary method
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 9d2350d commit d6b72de

5 files changed

Lines changed: 17 additions & 28 deletions

File tree

‎libraries/classes/Controllers/Table/RelationController.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,8 @@ public function getDropdownValueForTable(): void
358358

359359
$this->response->addJSON('columns', $columnList);
360360

361-
// @todo should be: $server->db($db)->table($table)->primary()
362361
$primary = Index::getPrimary($this->dbi, $foreignTable, $_POST['foreignDb']);
363-
if ($primary === false) {
362+
if ($primary === null) {
364363
return;
365364
}
366365

‎libraries/classes/Controllers/Table/StructureController.php‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,17 @@ public function __invoke(ServerRequest $request): void
158158
/**
159159
* Displays the table structure ('show table' works correct since 3.23.03)
160160
*
161-
* @param array $columns_with_unique_index Columns with unique index
162-
* @param Index|false $primary_index primary index or false if no one exists
163-
* @param array $fields Fields
164-
* @param array $columns_with_index Columns with index
161+
* @param array $columns_with_unique_index Columns with unique index
162+
* @param array $fields Fields
163+
* @param array $columns_with_index Columns with index
165164
* @psalm-param non-empty-string $route
166165
*
167166
* @return string
168167
*/
169168
protected function displayStructure(
170169
RelationParameters $relationParameters,
171170
array $columns_with_unique_index,
172-
$primary_index,
171+
?Index $primaryIndex,
173172
array $fields,
174173
array $columns_with_index,
175174
bool $isSystemSchema,
@@ -234,7 +233,7 @@ protected function displayStructure(
234233
$row_comments[$rownum] = $comments_map[$field['Field']];
235234
}
236235

237-
if ($primary_index && $primary_index->hasColumn($field['Field'])) {
236+
if ($primaryIndex !== null && $primaryIndex->hasColumn($field['Field'])) {
238237
$displayed_fields[$rownum]->icon .= Generator::getImage('b_primary', __('Primary'));
239238
}
240239

@@ -272,7 +271,7 @@ protected function displayStructure(
272271
'tbl_is_view' => $GLOBALS['tbl_is_view'],
273272
'mime_map' => $mime_map,
274273
'tbl_storage_engine' => $GLOBALS['tbl_storage_engine'],
275-
'primary' => $primary_index,
274+
'primary' => $primaryIndex,
276275
'columns_with_unique_index' => $columns_with_unique_index,
277276
'columns_list' => $columns_list,
278277
'table_stats' => $tablestats ?? null,

‎libraries/classes/Index.php‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,11 @@ public static function getFromTableByChoice($table, $schema, $choices = 31)
185185
return $indexes;
186186
}
187187

188-
/**
189-
* return primary if set, false otherwise
190-
*
191-
* @return Index|false primary index or false if no one exists
192-
*/
193-
public static function getPrimary(DatabaseInterface $dbi, string $table, string $schema)
188+
public static function getPrimary(DatabaseInterface $dbi, string $table, string $schema): ?Index
194189
{
195190
self::loadIndexes($dbi, $table, $schema);
196191

197-
if (isset(self::$registry[$schema][$table]['PRIMARY'])) {
198-
return self::$registry[$schema][$table]['PRIMARY'];
199-
}
200-
201-
return false;
192+
return self::$registry[$schema][$table]['PRIMARY'] ?? null;
202193
}
203194

204195
/**
@@ -462,7 +453,7 @@ public static function getIndexTypes()
462453

463454
public function hasPrimary(): bool
464455
{
465-
return (bool) self::getPrimary($GLOBALS['dbi'], $this->table, $this->schema);
456+
return self::getPrimary($GLOBALS['dbi'], $this->table, $this->schema) !== null;
466457
}
467458

468459
/**

‎libraries/classes/Normalization.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public function getHtmlContentsFor1NFStep2($db, $table)
277277
$hasPrimaryKey = '0';
278278
$legendText = __('Step 1.') . $step . ' ' . $stepTxt;
279279
$extra = '';
280-
if ($primary !== false) {
280+
if ($primary !== null) {
281281
$headText = __('Primary key already exists.');
282282
$subText = __('Taking you to next step…');
283283
$hasPrimaryKey = '1';
@@ -376,7 +376,7 @@ public function getHtmlContentsFor1NFStep3($db, $table)
376376
. '<input class="btn btn-secondary" type="submit" value="' . __('No repeating group')
377377
. '" onclick="goToStep4();">';
378378
$primary = Index::getPrimary($this->dbi, $table, $db);
379-
$primarycols = $primary === false ? [] : $primary->getColumns();
379+
$primarycols = $primary === null ? [] : $primary->getColumns();
380380
$pk = [];
381381
foreach ($primarycols as $col) {
382382
$pk[] = $col->getName();
@@ -403,7 +403,7 @@ public function getHtmlFor2NFstep1($db, $table)
403403
{
404404
$legendText = __('Step 2.') . '1 ' . __('Find partial dependencies');
405405
$primary = Index::getPrimary($this->dbi, $table, $db);
406-
$primarycols = $primary === false ? [] : $primary->getColumns();
406+
$primarycols = $primary === null ? [] : $primary->getColumns();
407407
$pk = [];
408408
$subText = '';
409409
$selectPkForm = '';
@@ -627,7 +627,7 @@ public function getHtmlForNewTables3NF($dependencies, array $tables, $db)
627627
}
628628

629629
$primary = Index::getPrimary($this->dbi, $table, $db);
630-
$primarycols = $primary === false ? [] : $primary->getColumns();
630+
$primarycols = $primary === null ? [] : $primary->getColumns();
631631
$pk = [];
632632
foreach ($primarycols as $col) {
633633
$pk[] = $col->getName();
@@ -879,7 +879,7 @@ public function getHtmlFor3NFstep1($db, array $tables)
879879
$cnt = 0;
880880
foreach ($tables as $table) {
881881
$primary = Index::getPrimary($this->dbi, $table, $db);
882-
$primarycols = $primary === false ? [] : $primary->getColumns();
882+
$primarycols = $primary === null ? [] : $primary->getColumns();
883883
$selectTdForm = '';
884884
$pk = [];
885885
foreach ($primarycols as $col) {
@@ -961,7 +961,7 @@ public function findPartialDependencies($table, $db)
961961
);
962962
$totalRows = $totalRowsRes[0];
963963
$primary = Index::getPrimary($this->dbi, $table, $db);
964-
$primarycols = $primary === false ? [] : $primary->getColumns();
964+
$primarycols = $primary === null ? [] : $primary->getColumns();
965965
$pk = [];
966966
foreach ($primarycols as $col) {
967967
$pk[] = Util::backquote($col->getName());

‎libraries/classes/Sql.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public function getDefaultSqlQueryForBrowse($db, $table): string
504504
$primaryKey = null;
505505
$primary = Index::getPrimary($this->dbi, $table, $db);
506506

507-
if ($primary !== false) {
507+
if ($primary !== null) {
508508
$primarycols = $primary->getColumns();
509509

510510
foreach ($primarycols as $col) {

0 commit comments

Comments
 (0)