Skip to content

Commit 5f9ba2e

Browse files
committed
Extract Relation and Template dependencies from UserPreferences
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent f70c730 commit 5f9ba2e

16 files changed

Lines changed: 83 additions & 36 deletions

libraries/classes/Config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\Config\Settings;
88
use PhpMyAdmin\Config\Settings\Server;
9+
use PhpMyAdmin\ConfigStorage\Relation;
910
use PhpMyAdmin\Dbal\Connection;
1011
use PhpMyAdmin\Exceptions\ConfigException;
1112
use PhpMyAdmin\Routing\Routing;
@@ -396,7 +397,7 @@ public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumC
396397
! isset($_SESSION['cache'][$cacheKey]['userprefs'])
397398
|| $_SESSION['cache'][$cacheKey]['config_mtime'] < $this->sourceMtime
398399
) {
399-
$userPreferences = new UserPreferences($GLOBALS['dbi']);
400+
$userPreferences = new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());
400401
$prefs = $userPreferences->load();
401402
$_SESSION['cache'][$cacheKey]['userprefs'] = $userPreferences->apply($prefs['config_data']);
402403
$_SESSION['cache'][$cacheKey]['userprefs_mtime'] = $prefs['mtime'];
@@ -495,7 +496,7 @@ public function setUserValue(
495496
mixed $newCfgValue,
496497
string|null $defaultValue = null,
497498
): bool|Message {
498-
$userPreferences = new UserPreferences($GLOBALS['dbi']);
499+
$userPreferences = new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());
499500
$result = true;
500501
// use permanent user preferences if possible
501502
$prefsType = $this->get('user_preferences');

libraries/classes/Controllers/ErrorReportController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace PhpMyAdmin\Controllers;
99

10+
use PhpMyAdmin\ConfigStorage\Relation;
1011
use PhpMyAdmin\DatabaseInterface;
1112
use PhpMyAdmin\ErrorHandler;
1213
use PhpMyAdmin\ErrorReport;
@@ -133,7 +134,7 @@ public function __invoke(ServerRequest $request): void
133134

134135
/* Persist always send settings */
135136
if ($alwaysSend === 'true') {
136-
$userPreferences = new UserPreferences($this->dbi);
137+
$userPreferences = new UserPreferences($this->dbi, new Relation($this->dbi), $this->template);
137138
$userPreferences->persistOption('SendErrorReports', 'always', 'ask');
138139
}
139140
}

libraries/classes/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function __construct()
9696
$this->addDefaultScripts();
9797
$this->headerIsSent = false;
9898

99-
$this->userPreferences = new UserPreferences($GLOBALS['dbi']);
99+
$this->userPreferences = new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), $this->template);
100100
}
101101

102102
/**

libraries/classes/Navigation/Navigation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public function getDisplay(): string
8989
}
9090

9191
if (! defined('PMA_DISABLE_NAVI_SETTINGS')) {
92-
$pageSettings = new PageSettings(new UserPreferences($this->dbi));
92+
$pageSettings = new PageSettings(
93+
new UserPreferences($this->dbi, new Relation($this->dbi), $this->template),
94+
);
9395
$pageSettings->init('Navi', 'pma_navigation_settings');
9496
$response->addHTML($pageSettings->getErrorHTML());
9597
$navigationSettings = $pageSettings->getHTML();

libraries/classes/TwoFactor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ class TwoFactor
5252
*/
5353
public function __construct(public string $user)
5454
{
55-
(new Relation($GLOBALS['dbi']))->initRelationParamsCache();
55+
$relation = new Relation($GLOBALS['dbi']);
56+
$relation->initRelationParamsCache();
5657

57-
$this->userPreferences = new UserPreferences($GLOBALS['dbi']);
58+
$this->userPreferences = new UserPreferences($GLOBALS['dbi'], $relation, new Template());
5859
$this->available = $this->getAvailableBackends();
5960
$this->config = $this->readConfig();
6061
$this->writable = ($this->config['type'] === 'db');

libraries/classes/UserPreferences.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030
*/
3131
class UserPreferences
3232
{
33-
private Relation $relation;
34-
35-
public Template $template;
36-
37-
public function __construct(private DatabaseInterface $dbi)
38-
{
39-
$this->relation = new Relation($this->dbi);
40-
$this->template = new Template();
33+
public function __construct(
34+
private readonly DatabaseInterface $dbi,
35+
private readonly Relation $relation,
36+
private readonly Template $template,
37+
) {
4138
}
4239

4340
/**

libraries/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
'class' => UserPassword::class,
205205
'arguments' => ['@server_privileges', '@' . AuthenticationPluginFactory::class, '@dbi'],
206206
],
207-
'user_preferences' => ['class' => UserPreferences::class, 'arguments' => ['@dbi']],
207+
'user_preferences' => ['class' => UserPreferences::class, 'arguments' => ['@dbi', '@relation', '@template']],
208208
'version_information' => ['class' => VersionInformation::class],
209209
DatabaseInterface::class => 'dbi',
210210
PhpMyAdmin\FlashMessages::class => 'flash',

phpstan-baseline.neon

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ parameters:
535535
count: 1
536536
path: libraries/classes/Config.php
537537

538+
-
539+
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
540+
count: 2
541+
path: libraries/classes/Config.php
542+
538543
-
539544
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\UserPreferences constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
540545
count: 2
@@ -14242,7 +14247,7 @@ parameters:
1424214247

1424314248
-
1424414249
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
14245-
count: 2
14250+
count: 3
1424614251
path: libraries/classes/Header.php
1424714252

1424814253
-
@@ -27460,6 +27465,11 @@ parameters:
2746027465
count: 1
2746127466
path: test/classes/Config/PageSettingsTest.php
2746227467

27468+
-
27469+
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
27470+
count: 3
27471+
path: test/classes/Config/PageSettingsTest.php
27472+
2746327473
-
2746427474
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\UserPreferences constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
2746527475
count: 3
@@ -29020,6 +29030,11 @@ parameters:
2902029030
count: 1
2902129031
path: test/classes/Controllers/Table/SqlControllerTest.php
2902229032

29033+
-
29034+
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
29035+
count: 1
29036+
path: test/classes/Controllers/Table/SqlControllerTest.php
29037+
2902329038
-
2902429039
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\UserPreferences constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
2902529040
count: 1
@@ -29080,6 +29095,11 @@ parameters:
2908029095
count: 1
2908129096
path: test/classes/Controllers/Table/StructureControllerTest.php
2908229097

29098+
-
29099+
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
29100+
count: 1
29101+
path: test/classes/Controllers/Table/StructureControllerTest.php
29102+
2908329103
-
2908429104
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\UserPreferences constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
2908529105
count: 1
@@ -32740,6 +32760,11 @@ parameters:
3274032760
count: 1
3274132761
path: test/classes/UserPreferencesTest.php
3274232762

32763+
-
32764+
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\ConfigStorage\\\\Relation constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
32765+
count: 10
32766+
path: test/classes/UserPreferencesTest.php
32767+
3274332768
-
3274432769
message: "#^Parameter \\#1 \\$dbi of class PhpMyAdmin\\\\UserPreferences constructor expects PhpMyAdmin\\\\DatabaseInterface, mixed given\\.$#"
3274532770
count: 8

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6608,6 +6608,7 @@
66086608
</PossiblyInvalidArgument>
66096609
<PossiblyNullArgument>
66106610
<code><![CDATA[$GLOBALS['dbi']]]></code>
6611+
<code><![CDATA[$GLOBALS['dbi']]]></code>
66116612
</PossiblyNullArgument>
66126613
<PropertyNotSetInConstructor>
66136614
<code>$menu</code>

test/classes/Config/PageSettingsTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace PhpMyAdmin\Tests\Config;
66

77
use PhpMyAdmin\Config\PageSettings;
8+
use PhpMyAdmin\ConfigStorage\Relation;
89
use PhpMyAdmin\ResponseRenderer;
10+
use PhpMyAdmin\Template;
911
use PhpMyAdmin\Tests\AbstractTestCase;
1012
use PhpMyAdmin\UserPreferences;
1113
use PHPUnit\Framework\Attributes\BackupStaticProperties;
@@ -41,7 +43,7 @@ protected function setUp(): void
4143
*/
4244
public function testShowGroupNonExistent(): void
4345
{
44-
$object = new PageSettings(new UserPreferences($GLOBALS['dbi']));
46+
$object = new PageSettings(new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template()));
4547
$object->init('NonExistent');
4648

4749
$this->assertEquals('', $object->getHTML());
@@ -55,7 +57,9 @@ public function testShowGroupBrowse(): void
5557
{
5658
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
5759

58-
$object = new PageSettings(new UserPreferences($GLOBALS['dbi']));
60+
$object = new PageSettings(
61+
new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template()),
62+
);
5963
$object->init('Browse');
6064

6165
$html = $object->getHTML();
@@ -85,7 +89,9 @@ public function testShowGroupBrowse(): void
8589
*/
8690
public function testGetNaviSettings(): void
8791
{
88-
$pageSettings = new PageSettings(new UserPreferences($GLOBALS['dbi']));
92+
$pageSettings = new PageSettings(
93+
new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template()),
94+
);
8995
$pageSettings->init('Navi', 'pma_navigation_settings');
9096

9197
$html = $pageSettings->getHTML();

0 commit comments

Comments
 (0)