Skip to content

Commit d3a267f

Browse files
committed
Refactor DbiMysqli::connect to use Server value object
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 183f193 commit d3a267f

7 files changed

Lines changed: 57 additions & 95 deletions

File tree

libraries/classes/Config/Settings/Server.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ public function __construct(array $server = [])
575575
$this->hide_connection_errors = $this->setHideConnectionErrors($server);
576576
}
577577

578+
/**
579+
* @return static
580+
*/
581+
public function withSSL(bool $ssl): Server
582+
{
583+
$clone = clone $this;
584+
$clone->ssl = $ssl;
585+
586+
return $clone;
587+
}
588+
578589
/**
579590
* @param array<int|string, mixed> $server
580591
*/

libraries/classes/DatabaseInterface.php

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

88
namespace PhpMyAdmin;
99

10+
use PhpMyAdmin\Config\Settings\Server;
1011
use PhpMyAdmin\ConfigStorage\Relation;
1112
use PhpMyAdmin\Database\DatabaseList;
1213
use PhpMyAdmin\Dbal\DatabaseName;
@@ -1860,7 +1861,7 @@ public function connect(int $mode, ?array $server = null, ?int $target = null)
18601861
$target = $mode;
18611862
}
18621863

1863-
if ($user === null || $password === null) {
1864+
if ($user === null || $password === null || ! is_array($server)) {
18641865
trigger_error(
18651866
__('Missing connection parameters!'),
18661867
E_USER_WARNING
@@ -1869,9 +1870,11 @@ public function connect(int $mode, ?array $server = null, ?int $target = null)
18691870
return false;
18701871
}
18711872

1873+
$server['host'] = ! is_string($server['host']) || $server['host'] === '' ? 'localhost' : $server['host'];
1874+
18721875
// Do not show location and backtrace for connection errors
18731876
$GLOBALS['errorHandler']->setHideLocation(true);
1874-
$result = $this->extension->connect($user, $password, $server);
1877+
$result = $this->extension->connect($user, $password, new Server($server));
18751878
$GLOBALS['errorHandler']->setHideLocation(false);
18761879

18771880
if ($result) {

libraries/classes/Dbal/DbiExtension.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77

88
namespace PhpMyAdmin\Dbal;
99

10+
use PhpMyAdmin\Config\Settings\Server;
11+
1012
/**
1113
* Contract for every database extension supported by phpMyAdmin
1214
*/
1315
interface DbiExtension
1416
{
1517
/**
16-
* connects to the database server
17-
*
18-
* @param string $user user name
19-
* @param string $password user password
20-
* @param array $server host/port/socket/persistent
18+
* Connects to the database server.
2119
*
22-
* @return mixed false on error or a connection object on success
20+
* @return object|bool A connection object on success or false on failure.
2321
*/
24-
public function connect(
25-
$user,
26-
$password,
27-
array $server
28-
);
22+
public function connect(string $user, string $password, Server $server);
2923

3024
/**
3125
* selects given database

libraries/classes/Dbal/DbiMysqli.php

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use mysqli;
1111
use mysqli_sql_exception;
1212
use mysqli_stmt;
13+
use PhpMyAdmin\Config\Settings\Server;
1314
use PhpMyAdmin\DatabaseInterface;
1415
use PhpMyAdmin\Query\Utilities;
1516

@@ -43,22 +44,12 @@
4344
class DbiMysqli implements DbiExtension
4445
{
4546
/**
46-
* connects to the database server
47+
* Connects to the database server.
4748
*
48-
* @param string $user mysql user name
49-
* @param string $password mysql user password
50-
* @param array $server host/port/socket/persistent
51-
*
52-
* @return mysqli|bool false on error or a mysqli object on success
49+
* @return object|bool A connection object on success or false on failure.
5350
*/
54-
public function connect($user, $password, array $server)
51+
public function connect(string $user, string $password, Server $server)
5552
{
56-
if ($server) {
57-
$server['host'] = empty($server['host'])
58-
? 'localhost'
59-
: $server['host'];
60-
}
61-
6253
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
6354

6455
$mysqli = mysqli_init();
@@ -70,44 +61,46 @@ public function connect($user, $password, array $server)
7061
$client_flags = 0;
7162

7263
/* Optionally compress connection */
73-
if ($server['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
64+
if ($server->compress && defined('MYSQLI_CLIENT_COMPRESS')) {
7465
$client_flags |= MYSQLI_CLIENT_COMPRESS;
7566
}
7667

68+
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
7769
/* Optionally enable SSL */
78-
if ($server['ssl']) {
70+
if ($server->ssl) {
7971
$client_flags |= MYSQLI_CLIENT_SSL;
8072
if (
81-
! empty($server['ssl_key']) ||
82-
! empty($server['ssl_cert']) ||
83-
! empty($server['ssl_ca']) ||
84-
! empty($server['ssl_ca_path']) ||
85-
! empty($server['ssl_ciphers'])
73+
$server->ssl_key !== null && $server->ssl_key !== '' ||
74+
$server->ssl_cert !== null && $server->ssl_cert !== '' ||
75+
$server->ssl_ca !== null && $server->ssl_ca !== '' ||
76+
$server->ssl_ca_path !== null && $server->ssl_ca_path !== '' ||
77+
$server->ssl_ciphers !== null && $server->ssl_ciphers !== ''
8678
) {
8779
$mysqli->ssl_set(
88-
$server['ssl_key'] ?? '',
89-
$server['ssl_cert'] ?? '',
90-
$server['ssl_ca'] ?? '',
91-
$server['ssl_ca_path'] ?? '',
92-
$server['ssl_ciphers'] ?? ''
80+
$server->ssl_key ?? '',
81+
$server->ssl_cert ?? '',
82+
$server->ssl_ca ?? '',
83+
$server->ssl_ca_path ?? '',
84+
$server->ssl_ciphers ?? ''
9385
);
9486
}
9587

96-
/*
88+
/**
9789
* disables SSL certificate validation on mysqlnd for MySQL 5.6 or later
90+
*
9891
* @link https://bugs.php.net/bug.php?id=68344
9992
* @link https://github.com/phpmyadmin/phpmyadmin/pull/11838
10093
*/
101-
if (! $server['ssl_verify']) {
102-
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, (int) $server['ssl_verify']);
94+
if (! $server->ssl_verify) {
95+
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, (int) $server->ssl_verify);
10396
$client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
10497
}
10598
}
10699

107100
if ($GLOBALS['cfg']['PersistentConnections']) {
108-
$host = 'p:' . $server['host'];
101+
$host = 'p:' . $server->host;
109102
} else {
110-
$host = $server['host'];
103+
$host = $server->host;
111104
}
112105

113106
try {
@@ -116,8 +109,8 @@ public function connect($user, $password, array $server)
116109
$user,
117110
$password,
118111
'',
119-
$server['port'],
120-
(string) $server['socket'],
112+
(int) $server->port,
113+
$server->socket,
121114
$client_flags
122115
);
123116
} catch (mysqli_sql_exception $exception) {
@@ -129,12 +122,10 @@ public function connect($user, $password, array $server)
129122
* - #2001 - SSL Connection is required. Please specify SSL options and retry.
130123
* - #9002 - SSL connection is required. Please specify SSL options and retry.
131124
*/
132-
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
133125
$error_number = $mysqli->connect_errno;
134126
$error_message = $mysqli->connect_error;
135-
// phpcs:enable
136127
if (
137-
! $server['ssl']
128+
! $server->ssl
138129
&& ($error_number == 3159
139130
|| (($error_number == 2001 || $error_number == 9002)
140131
&& stripos($error_message, 'SSL Connection is required') !== false))
@@ -143,12 +134,11 @@ public function connect($user, $password, array $server)
143134
__('SSL connection enforced by server, automatically enabling it.'),
144135
E_USER_WARNING
145136
);
146-
$server['ssl'] = true;
147137

148-
return self::connect($user, $password, $server);
138+
return self::connect($user, $password, $server->withSSL(true));
149139
}
150140

151-
if ($error_number === 1045 && $server['hide_connection_errors']) {
141+
if ($error_number === 1045 && $server->hide_connection_errors) {
152142
trigger_error(
153143
sprintf(
154144
__(
@@ -169,6 +159,8 @@ public function connect($user, $password, array $server)
169159
return false;
170160
}
171161

162+
// phpcs:enable
163+
172164
$mysqli->options(MYSQLI_OPT_LOCAL_INFILE, (int) defined('PMA_ENABLE_LDI'));
173165

174166
mysqli_report(MYSQLI_REPORT_OFF);

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,16 +2725,6 @@ parameters:
27252725
count: 1
27262726
path: libraries/classes/Dbal/DbalInterface.php
27272727

2728-
-
2729-
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbiExtension\\:\\:connect\\(\\) has parameter \\$server with no value type specified in iterable type array\\.$#"
2730-
count: 1
2731-
path: libraries/classes/Dbal/DbiExtension.php
2732-
2733-
-
2734-
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbiMysqli\\:\\:connect\\(\\) has parameter \\$server with no value type specified in iterable type array\\.$#"
2735-
count: 1
2736-
path: libraries/classes/Dbal/DbiMysqli.php
2737-
27382728
-
27392729
message: "#^Method PhpMyAdmin\\\\Dbal\\\\MysqliResult\\:\\:fetchAllKeyPair\\(\\) should return array\\<string, string\\|null\\> but returns array\\<int\\|string, mixed\\>\\.$#"
27402730
count: 2
@@ -9975,11 +9965,6 @@ parameters:
99759965
count: 1
99769966
path: test/classes/StorageEngineTest.php
99779967

9978-
-
9979-
message: "#^Method PhpMyAdmin\\\\Tests\\\\Stubs\\\\DbiDummy\\:\\:connect\\(\\) has parameter \\$server with no value type specified in iterable type array\\.$#"
9980-
count: 1
9981-
path: test/classes/Stubs/DbiDummy.php
9982-
99839968
-
99849969
message: "#^Method PhpMyAdmin\\\\Tests\\\\Stubs\\\\DbiDummy\\:\\:fetchAny\\(\\) return type has no value type specified in iterable type array\\.$#"
99859970
count: 1

psalm-baseline.xml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,7 +5682,7 @@
56825682
<code>$this-&gt;links[$link]</code>
56835683
<code>$this-&gt;links[$link]</code>
56845684
</MixedArrayOffset>
5685-
<MixedAssignment occurrences="37">
5685+
<MixedAssignment occurrences="36">
56865686
<code>$aLength</code>
56875687
<code>$bLength</code>
56885688
<code>$database</code>
@@ -5700,7 +5700,6 @@
57005700
<code>$oneResult['name']</code>
57015701
<code>$oneResult['table']</code>
57025702
<code>$oneShow</code>
5703-
<code>$result</code>
57045703
<code>$resultRows[$row[$key]]</code>
57055704
<code>$resultRows[]</code>
57065705
<code>$resultTarget</code>
@@ -5787,22 +5786,6 @@
57875786
</PossiblyNullReference>
57885787
</file>
57895788
<file src="libraries/classes/Dbal/DbiMysqli.php">
5790-
<MixedArgument occurrences="7">
5791-
<code>$host</code>
5792-
<code>$server['port']</code>
5793-
<code>$server['ssl_ca'] ?? ''</code>
5794-
<code>$server['ssl_ca_path'] ?? ''</code>
5795-
<code>$server['ssl_cert'] ?? ''</code>
5796-
<code>$server['ssl_ciphers'] ?? ''</code>
5797-
<code>$server['ssl_key'] ?? ''</code>
5798-
</MixedArgument>
5799-
<MixedAssignment occurrences="2">
5800-
<code>$host</code>
5801-
<code>$server['host']</code>
5802-
</MixedAssignment>
5803-
<MixedOperand occurrences="1">
5804-
<code>$server['host']</code>
5805-
</MixedOperand>
58065789
<MoreSpecificImplementedParamType occurrences="12">
58075790
<code>$link</code>
58085791
<code>$link</code>

test/classes/Stubs/DbiDummy.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace PhpMyAdmin\Tests\Stubs;
1313

14+
use PhpMyAdmin\Config\Settings\Server;
1415
use PhpMyAdmin\Dbal\DatabaseName;
1516
use PhpMyAdmin\Dbal\DbiExtension;
1617
use PhpMyAdmin\Dbal\ResultInterface;
@@ -94,19 +95,12 @@ public function __construct()
9495
}
9596

9697
/**
97-
* connects to the database server
98+
* Connects to the database server.
9899
*
99-
* @param string $user mysql user name
100-
* @param string $password mysql user password
101-
* @param array $server host/port/socket/persistent
102-
*
103-
* @return mixed false on error or a mysqli object on success
100+
* @return object|bool A connection object on success or false on failure.
104101
*/
105-
public function connect(
106-
$user,
107-
$password,
108-
array $server = []
109-
) {
102+
public function connect(string $user, string $password, Server $server)
103+
{
110104
return true;
111105
}
112106

0 commit comments

Comments
 (0)