Skip to content

Commit 4bbf1e8

Browse files
Merge pull request #18029 from kamil-tekiela/Forms-refactoring
Refactor config forms
2 parents 7a22334 + d1bda5c commit 4bbf1e8

16 files changed

Lines changed: 60 additions & 120 deletions

libraries/classes/Config/FormDisplay.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,11 @@ private function displayFieldInput(
479479
/**
480480
* Displays errors
481481
*
482-
* @return string|null HTML for errors
482+
* @return string HTML for errors
483483
*/
484-
public function displayErrors()
484+
public function displayErrors(): string
485485
{
486486
$this->validate();
487-
if (count($this->errors) === 0) {
488-
return null;
489-
}
490487

491488
$htmlOutput = '';
492489

@@ -509,7 +506,7 @@ public function displayErrors()
509506
public function fixErrors(): void
510507
{
511508
$this->validate();
512-
if (count($this->errors) === 0) {
509+
if ($this->errors === []) {
513510
return;
514511
}
515512

@@ -674,7 +671,7 @@ public function save(array $forms, bool $allowPartialSave = true): bool
674671
}
675672

676673
// save forms
677-
if (! $allowPartialSave && ! empty($this->errors)) {
674+
if (! $allowPartialSave && $this->errors !== []) {
678675
// don't look for non-critical errors
679676
$this->validate();
680677

@@ -724,7 +721,7 @@ public function save(array $forms, bool $allowPartialSave = true): bool
724721
*/
725722
public function hasErrors(): bool
726723
{
727-
return count($this->errors) > 0;
724+
return $this->errors !== [];
728725
}
729726

730727
/**

libraries/classes/Config/Forms/BaseFormList.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,36 @@
99

1010
use PhpMyAdmin\Config\ConfigFile;
1111

12+
use function array_key_exists;
13+
use function array_keys;
1214
use function array_merge;
13-
use function class_exists;
14-
use function in_array;
1515

1616
class BaseFormList
1717
{
1818
/**
1919
* List of all forms
2020
*
21-
* @var string[]
21+
* @var array<string, class-string<BaseForm>>
2222
*/
2323
protected static $all = [];
2424

25-
/** @var string */
26-
protected static $ns = 'PhpMyAdmin\\Config\\Forms\\';
27-
28-
/** @var array */
25+
/** @var BaseForm[] */
2926
private $forms;
3027

3128
/**
3229
* @return string[]
3330
*/
34-
public static function getAll()
31+
public static function getAllFormNames()
3532
{
36-
return static::$all;
33+
return array_keys(static::$all);
3734
}
3835

3936
/**
4037
* @param string $name Name
4138
*/
42-
public static function isValid($name): bool
39+
public static function isValid(string $name): bool
4340
{
44-
return in_array($name, static::$all);
41+
return array_key_exists($name, static::$all);
4542
}
4643

4744
/**
@@ -50,13 +47,10 @@ public static function isValid($name): bool
5047
* @return string|null
5148
* @psalm-return class-string<BaseForm>|null
5249
*/
53-
public static function get($name)
50+
public static function get(string $name)
5451
{
5552
if (static::isValid($name)) {
56-
/** @var class-string<BaseForm> $class */
57-
$class = static::$ns . $name . 'Form';
58-
59-
return $class;
53+
return static::$all[$name];
6054
}
6155

6256
return null;
@@ -68,12 +62,7 @@ public static function get($name)
6862
final public function __construct(ConfigFile $cf)
6963
{
7064
$this->forms = [];
71-
foreach (static::$all as $form) {
72-
$class = (string) static::get($form);
73-
if (! class_exists($class)) {
74-
continue;
75-
}
76-
65+
foreach (static::$all as $class) {
7766
$this->forms[] = new $class($cf);
7867
}
7968
}
@@ -141,12 +130,7 @@ public function hasErrors(): bool
141130
public static function getFields()
142131
{
143132
$names = [];
144-
foreach (static::$all as $form) {
145-
$class = (string) static::get($form);
146-
if (! class_exists($class)) {
147-
continue;
148-
}
149-
133+
foreach (static::$all as $class) {
150134
$names = array_merge($names, $class::getFields());
151135
}
152136

libraries/classes/Config/Forms/Page/PageFormList.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77

88
namespace PhpMyAdmin\Config\Forms\Page;
99

10+
use PhpMyAdmin\Config\Forms\BaseForm;
1011
use PhpMyAdmin\Config\Forms\BaseFormList;
1112

1213
class PageFormList extends BaseFormList
1314
{
14-
/** @var string[] */
15+
/** @var array<string, class-string<BaseForm>> */
1516
protected static $all = [
16-
'Browse',
17-
'DbStructure',
18-
'Edit',
19-
'Export',
20-
'Import',
21-
'Navi',
22-
'Sql',
23-
'TableStructure',
17+
'Browse' => BrowseForm::class,
18+
'DbStructure' => DbStructureForm::class,
19+
'Edit' => EditForm::class,
20+
'Export' => ExportForm::class,
21+
'Import' => ImportForm::class,
22+
'Navi' => NaviForm::class,
23+
'Sql' => SqlForm::class,
24+
'TableStructure' => TableStructureForm::class,
2425
];
25-
/** @var string */
26-
protected static $ns = 'PhpMyAdmin\\Config\\Forms\\Page\\';
2726
}

libraries/classes/Config/Forms/Setup/SetupFormList.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77

88
namespace PhpMyAdmin\Config\Forms\Setup;
99

10+
use PhpMyAdmin\Config\Forms\BaseForm;
1011
use PhpMyAdmin\Config\Forms\BaseFormList;
1112

1213
class SetupFormList extends BaseFormList
1314
{
14-
/** @var string[] */
15+
/** @var array<string, class-string<BaseForm>> */
1516
protected static $all = [
16-
'Config',
17-
'Export',
18-
'Features',
19-
'Import',
20-
'Main',
21-
'Navi',
22-
'Servers',
23-
'Sql',
17+
'Config' => ConfigForm::class,
18+
'Export' => ExportForm::class,
19+
'Features' => FeaturesForm::class,
20+
'Import' => ImportForm::class,
21+
'Main' => MainForm::class,
22+
'Navi' => NaviForm::class,
23+
'Servers' => ServersForm::class,
24+
'Sql' => SqlForm::class,
2425
];
25-
/** @var string */
26-
protected static $ns = 'PhpMyAdmin\\Config\\Forms\\Setup\\';
2726
}

libraries/classes/Config/Forms/User/UserFormList.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77

88
namespace PhpMyAdmin\Config\Forms\User;
99

10+
use PhpMyAdmin\Config\Forms\BaseForm;
1011
use PhpMyAdmin\Config\Forms\BaseFormList;
1112

1213
class UserFormList extends BaseFormList
1314
{
14-
/** @var string[] */
15+
/** @var array<string, class-string<BaseForm>> */
1516
protected static $all = [
16-
'Features',
17-
'Sql',
18-
'Navi',
19-
'Main',
20-
'Export',
21-
'Import',
17+
'Features' => FeaturesForm::class,
18+
'Sql' => SqlForm::class,
19+
'Navi' => NaviForm::class,
20+
'Main' => MainForm::class,
21+
'Export' => ExportForm::class,
22+
'Import' => ImportForm::class,
2223
];
23-
/** @var string */
24-
protected static $ns = 'PhpMyAdmin\\Config\\Forms\\User\\';
2524
}

libraries/classes/Controllers/Preferences/ExportController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function __invoke(ServerRequest $request): void
9393
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
9494
]);
9595

96-
if ($formDisplay->hasErrors()) {
97-
$formErrors = $formDisplay->displayErrors();
98-
}
96+
$formErrors = $formDisplay->displayErrors();
9997

10098
$this->render('preferences/forms/main', [
10199
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
102100
'has_errors' => $formDisplay->hasErrors(),
103-
'errors' => $formErrors ?? null,
101+
'errors' => $formErrors,
104102
'form' => $formDisplay->getDisplay(
105103
true,
106104
Url::getFromRoute('/preferences/export'),

libraries/classes/Controllers/Preferences/FeaturesController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function __invoke(ServerRequest $request): void
9393
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
9494
]);
9595

96-
if ($formDisplay->hasErrors()) {
97-
$formErrors = $formDisplay->displayErrors();
98-
}
96+
$formErrors = $formDisplay->displayErrors();
9997

10098
$this->render('preferences/forms/main', [
10199
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
102100
'has_errors' => $formDisplay->hasErrors(),
103-
'errors' => $formErrors ?? null,
101+
'errors' => $formErrors,
104102
'form' => $formDisplay->getDisplay(
105103
true,
106104
Url::getFromRoute('/preferences/features'),

libraries/classes/Controllers/Preferences/ImportController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function __invoke(ServerRequest $request): void
9393
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
9494
]);
9595

96-
if ($formDisplay->hasErrors()) {
97-
$formErrors = $formDisplay->displayErrors();
98-
}
96+
$formErrors = $formDisplay->displayErrors();
9997

10098
$this->render('preferences/forms/main', [
10199
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
102100
'has_errors' => $formDisplay->hasErrors(),
103-
'errors' => $formErrors ?? null,
101+
'errors' => $formErrors,
104102
'form' => $formDisplay->getDisplay(
105103
true,
106104
Url::getFromRoute('/preferences/import'),

libraries/classes/Controllers/Preferences/MainPanelController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function __invoke(ServerRequest $request): void
9393
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
9494
]);
9595

96-
if ($formDisplay->hasErrors()) {
97-
$formErrors = $formDisplay->displayErrors();
98-
}
96+
$formErrors = $formDisplay->displayErrors();
9997

10098
$this->render('preferences/forms/main', [
10199
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
102100
'has_errors' => $formDisplay->hasErrors(),
103-
'errors' => $formErrors ?? null,
101+
'errors' => $formErrors,
104102
'form' => $formDisplay->getDisplay(
105103
true,
106104
Url::getFromRoute('/preferences/main-panel'),

libraries/classes/Controllers/Preferences/NavigationController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public function __invoke(ServerRequest $request): void
9393
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
9494
]);
9595

96-
if ($formDisplay->hasErrors()) {
97-
$formErrors = $formDisplay->displayErrors();
98-
}
96+
$formErrors = $formDisplay->displayErrors();
9997

10098
$this->render('preferences/forms/main', [
10199
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
102100
'has_errors' => $formDisplay->hasErrors(),
103-
'errors' => $formErrors ?? null,
101+
'errors' => $formErrors,
104102
'form' => $formDisplay->getDisplay(
105103
true,
106104
Url::getFromRoute('/preferences/navigation'),

0 commit comments

Comments
 (0)