Skip to content

Commit ac325d2

Browse files
Merge pull request #18960 from kamil-tekiela/Refactor-Privileges
Refactor privileges with better return types
2 parents bb6b17a + 8f7af9e commit ac325d2

3 files changed

Lines changed: 36 additions & 111 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13612,7 +13612,7 @@ parameters:
1361213612

1361313613
-
1361413614
message: "#^Cannot access offset 'Host' on mixed\\.$#"
13615-
count: 4
13615+
count: 2
1361613616
path: src/Server/Privileges.php
1361713617

1361813618
-
@@ -13642,7 +13642,7 @@ parameters:
1364213642

1364313643
-
1364413644
message: "#^Cannot access offset 'User' on mixed\\.$#"
13645-
count: 5
13645+
count: 3
1364613646
path: src/Server/Privileges.php
1364713647

1364813648
-
@@ -13677,12 +13677,12 @@ parameters:
1367713677

1367813678
-
1367913679
message: "#^Cannot cast mixed to string\\.$#"
13680-
count: 8
13680+
count: 6
1368113681
path: src/Server/Privileges.php
1368213682

1368313683
-
1368413684
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
13685-
count: 18
13685+
count: 15
1368613686
path: src/Server/Privileges.php
1368713687

1368813688
-
@@ -13797,19 +13797,14 @@ parameters:
1379713797

1379813798
-
1379913799
message: "#^Parameter \\#1 \\$query of method PhpMyAdmin\\\\DatabaseInterface\\:\\:tryQuery\\(\\) expects string, mixed given\\.$#"
13800-
count: 3
13800+
count: 1
1380113801
path: src/Server/Privileges.php
1380213802

1380313803
-
1380413804
message: "#^Parameter \\#1 \\$row of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:extractPrivInfo\\(\\) expects array\\|null, mixed given\\.$#"
1380513805
count: 2
1380613806
path: src/Server/Privileges.php
1380713807

13808-
-
13809-
message: "#^Parameter \\#1 \\$row of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:getSpecificPrivilege\\(\\) expects array, mixed given\\.$#"
13810-
count: 1
13811-
path: src/Server/Privileges.php
13812-
1381313808
-
1381413809
message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#"
1381513810
count: 4
@@ -13845,11 +13840,6 @@ parameters:
1384513840
count: 1
1384613841
path: src/Server/Privileges.php
1384713842

13848-
-
13849-
message: "#^Parameter \\#2 \\$realSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
13850-
count: 1
13851-
path: src/Server/Privileges.php
13852-
1385313843
-
1385413844
message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#"
1385513845
count: 6
@@ -13875,11 +13865,6 @@ parameters:
1387513865
count: 1
1387613866
path: src/Server/Privileges.php
1387713867

13878-
-
13879-
message: "#^Parameter \\#3 \\$sqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
13880-
count: 1
13881-
path: src/Server/Privileges.php
13882-
1388313868
-
1388413869
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, mixed given\\.$#"
1388513870
count: 2
@@ -13910,16 +13895,6 @@ parameters:
1391013895
count: 2
1391113896
path: src/Server/Privileges.php
1391213897

13913-
-
13914-
message: "#^Parameter \\#7 \\$alterRealSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
13915-
count: 1
13916-
path: src/Server/Privileges.php
13917-
13918-
-
13919-
message: "#^Parameter \\#8 \\$alterSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
13920-
count: 1
13921-
path: src/Server/Privileges.php
13922-
1392313898
-
1392413899
message: "#^Possibly invalid array key type array\\<int, string\\>\\|string\\|null\\.$#"
1392513900
count: 1
@@ -17167,24 +17142,14 @@ parameters:
1716717142

1716817143
-
1716917144
message: "#^Cannot call method getMessage\\(\\) on mixed\\.$#"
17170-
count: 6
17145+
count: 2
1717117146
path: tests/classes/Server/PrivilegesTest.php
1717217147

1717317148
-
1717417149
message: "#^PHPDoc tag @var for variable \\$actual has no value type specified in iterable type array\\.$#"
1717517150
count: 1
1717617151
path: tests/classes/Server/PrivilegesTest.php
1717717152

17178-
-
17179-
message: "#^Parameter \\#2 \\$realSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
17180-
count: 1
17181-
path: tests/classes/Server/PrivilegesTest.php
17182-
17183-
-
17184-
message: "#^Parameter \\#3 \\$sqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
17185-
count: 1
17186-
path: tests/classes/Server/PrivilegesTest.php
17187-
1718817153
-
1718917154
message: "#^Cannot access offset 'host' on mixed\\.$#"
1719017155
count: 1

‎psalm-baseline.xml‎

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,12 +2796,10 @@
27962796
</PossiblyUnusedParam>
27972797
</file>
27982798
<file src="src/Controllers/Server/PrivilegesController.php">
2799-
<InvalidArgument>
2800-
<code><![CDATA[$GLOBALS['sql_query']]]></code>
2801-
</InvalidArgument>
28022799
<InvalidArrayOffset>
28032800
<code><![CDATA[$GLOBALS['dbname']]]></code>
28042801
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
2802+
<code><![CDATA[$GLOBALS['sql_query'][$key]]]></code>
28052803
</InvalidArrayOffset>
28062804
<MixedArgument>
28072805
<code><![CDATA[$GLOBALS['message']]]></code>
@@ -2828,16 +2826,16 @@
28282826
<code><![CDATA[$GLOBALS['message']]]></code>
28292827
<code>$dbName</code>
28302828
</MixedAssignment>
2829+
<PossiblyInvalidArgument>
2830+
<code><![CDATA[$GLOBALS['sql_query']]]></code>
2831+
</PossiblyInvalidArgument>
28312832
<RiskyTruthyFalsyComparison>
28322833
<code><![CDATA[empty($GLOBALS['message'])]]></code>
28332834
<code><![CDATA[empty($GLOBALS['message'])]]></code>
28342835
<code><![CDATA[empty($_REQUEST['ajax_page_request'])]]></code>
28352836
<code>empty($routinename)</code>
28362837
<code>empty($routinename)</code>
28372838
</RiskyTruthyFalsyComparison>
2838-
<UnusedVariable>
2839-
<code>$key</code>
2840-
</UnusedVariable>
28412839
</file>
28422840
<file src="src/Controllers/Server/ReplicationController.php">
28432841
<InvalidArrayOffset>
@@ -11014,18 +11012,12 @@
1101411012
</InvalidArrayOffset>
1101511013
<MixedArgument>
1101611014
<code><![CDATA[$GLOBALS['dbname']]]></code>
11017-
<code>$alterRealSqlQuery</code>
11018-
<code>$alterSqlQuery</code>
11019-
<code>$createUserReal</code>
1102011015
<code>$paramDbName</code>
1102111016
<code>$paramDbName</code>
1102211017
<code>$paramRoutineName</code>
1102311018
<code>$paramRoutineName</code>
1102411019
<code>$paramTableName</code>
1102511020
<code>$paramTableName</code>
11026-
<code>$passwordSetReal</code>
11027-
<code>$privilege</code>
11028-
<code>$realSqlQuery</code>
1102911021
<code>$row</code>
1103011022
<code>$row</code>
1103111023
<code><![CDATA[$row1['Type']]]></code>
@@ -11042,7 +11034,6 @@
1104211034
<code><![CDATA[$row['Table_priv']]]></code>
1104311035
<code>$sqlQuery</code>
1104411036
<code>$sqlQuery</code>
11045-
<code>$sqlQuery</code>
1104611037
<code>$user</code>
1104711038
</MixedArgument>
1104811039
<MixedArgumentTypeCoercion>
@@ -11057,10 +11048,6 @@
1105711048
<code><![CDATA[$host['User']]]></code>
1105811049
<code><![CDATA[$host['User']]]></code>
1105911050
<code><![CDATA[$host['privs']]]></code>
11060-
<code><![CDATA[$privilege['Host']]]></code>
11061-
<code><![CDATA[$privilege['Host']]]></code>
11062-
<code><![CDATA[$privilege['User']]]></code>
11063-
<code><![CDATA[$privilege['User']]]></code>
1106411051
<code><![CDATA[$row['Db']]]></code>
1106511052
<code><![CDATA[$row['Grant_priv']]]></code>
1106611053
<code><![CDATA[$row['Proc_priv']]]></code>
@@ -11100,18 +11087,9 @@
1110011087
<code>$paramDbName</code>
1110111088
<code>$paramRoutineName</code>
1110211089
<code>$paramTableName</code>
11103-
<code>$privilege</code>
1110411090
<code><![CDATA[$privilege['routine']]]></code>
1110511091
<code><![CDATA[$privilege['table']]]></code>
11106-
<code>$queriesForDisplay[$tmpCount - 1]</code>
11107-
<code>$queriesForDisplay[$tmpCount - 1]</code>
11108-
<code>$queriesForDisplay[$tmpCount - 2]</code>
11109-
<code>$queriesForDisplay[$tmpCount - 2]</code>
11110-
<code>$queriesForDisplay[$tmpCount - 3]</code>
1111111092
<code>$queries[$tmpCount]</code>
11112-
<code>$queries[]</code>
11113-
<code>$queries[]</code>
11114-
<code>$queries[]</code>
1111511093
<code>$row</code>
1111611094
<code><![CDATA[$row['password']]]></code>
1111711095
<code><![CDATA[$row['password']]]></code>
@@ -11128,12 +11106,8 @@
1112811106
<code>string|null</code>
1112911107
</MixedInferredReturnType>
1113011108
<MixedOperand>
11131-
<code>$createUserShow</code>
1113211109
<code>$oneGrant</code>
1113311110
<code>$origValue</code>
11134-
<code><![CDATA[$privilege['Host']]]></code>
11135-
<code><![CDATA[$privilege['User']]]></code>
11136-
<code>$sqlQuery</code>
1113711111
</MixedOperand>
1113811112
<MixedReturnStatement>
1113911113
<code><![CDATA[$row['password']]]></code>
@@ -11234,6 +11208,8 @@
1123411208
<code>$alterUserQuery</code>
1123511209
<code>$alterUserQuery</code>
1123611210
<code>$alterUserQuery</code>
11211+
<code><![CDATA[$privilege['Host']]]></code>
11212+
<code><![CDATA[$privilege['User']]]></code>
1123711213
</PossiblyNullOperand>
1123811214
<PossiblyUndefinedArrayOffset>
1123911215
<code>$row1[0]</code>
@@ -15442,17 +15418,9 @@
1544215418
<code>Config::getInstance()</code>
1544315419
<code>Config::getInstance()</code>
1544415420
</DeprecatedMethod>
15445-
<MixedArgument>
15446-
<code>$realSqlQuery</code>
15447-
<code>$sqlQuery</code>
15448-
</MixedArgument>
1544915421
<MixedMethodCall>
1545015422
<code>getMessage</code>
1545115423
<code>getMessage</code>
15452-
<code>getMessage</code>
15453-
<code>getMessage</code>
15454-
<code>getMessage</code>
15455-
<code>getMessage</code>
1545615424
</MixedMethodCall>
1545715425
<PossiblyUnusedMethod>
1545815426
<code>providerUnEscapeMysqlWildcards</code>

‎src/Server/Privileges.php‎

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ public function updatePassword(string $errorUrl, string $username, string $hostn
910910
* @param string $hostname host name
911911
* @param string $itemType item type
912912
*
913-
* @return mixed[] ($message, $sql_query)
913+
* @return array{Message, string} ($message, $sql_query)
914914
*/
915915
public function getMessageAndSqlQueryForPrivilegesRevoke(
916916
string $dbname,
@@ -1124,7 +1124,7 @@ private function getSpecificPrivilege(array $row): array
11241124
return $privilege;
11251125
}
11261126

1127-
/** @return mixed[] */
1127+
/** @return array<int, array<string|null>> */
11281128
private function getGlobalAndDatabasePrivileges(DatabaseName $db): array
11291129
{
11301130
$listOfPrivileges = '`Select_priv`,
@@ -1185,7 +1185,7 @@ private function getGlobalAndDatabasePrivileges(DatabaseName $db): array
11851185
return $result->fetchAllAssoc();
11861186
}
11871187

1188-
/** @return mixed[] */
1188+
/** @return array<int, array<string|null>> */
11891189
private function getTablePrivileges(DatabaseName $db, TableName $table): array
11901190
{
11911191
$query = '
@@ -1207,7 +1207,7 @@ private function getTablePrivileges(DatabaseName $db, TableName $table): array
12071207
return $result->fetchAllAssoc();
12081208
}
12091209

1210-
/** @return mixed[] */
1210+
/** @return array<int, array<string|null>> */
12111211
private function getRoutinesPrivileges(DatabaseName $db): array
12121212
{
12131213
$query = '
@@ -1930,7 +1930,7 @@ public function deleteUser(array $queries): array
19301930
/**
19311931
* Update the privileges and return the success or error message
19321932
*
1933-
* @return mixed[] success message or error message for update
1933+
* @return array{string, Message} success message or error message for update
19341934
*/
19351935
public function updatePrivileges(
19361936
string $username,
@@ -2309,21 +2309,19 @@ public function addUser(
23092309
if (empty($_POST['change_copy'])) {
23102310
$error = false;
23112311

2312-
if ($createUserReal !== null) {
2313-
if (! $this->dbi->tryQuery($createUserReal)) {
2314-
$error = true;
2315-
}
2312+
if (! $this->dbi->tryQuery($createUserReal)) {
2313+
$error = true;
2314+
}
23162315

2317-
if (isset($_POST['authentication_plugin']) && ! empty($passwordSetReal)) {
2318-
$this->setProperPasswordHashing($_POST['authentication_plugin']);
2319-
if ($this->dbi->tryQuery($passwordSetReal)) {
2320-
$sqlQuery .= $passwordSetShow;
2321-
}
2316+
if (isset($_POST['authentication_plugin']) && $passwordSetReal !== '') {
2317+
$this->setProperPasswordHashing($_POST['authentication_plugin']);
2318+
if ($this->dbi->tryQuery($passwordSetReal)) {
2319+
$sqlQuery .= $passwordSetShow;
23222320
}
2323-
2324-
$sqlQuery = $createUserShow . $sqlQuery;
23252321
}
23262322

2323+
$sqlQuery = $createUserShow . $sqlQuery;
2324+
23272325
[$sqlQuery, $message] = $this->addUserAndCreateDatabase(
23282326
$error,
23292327
$realSqlQuery,
@@ -2354,13 +2352,10 @@ public function addUser(
23542352
$oldUserGroup = $_POST['old_usergroup'] ?? '';
23552353
$this->setUserGroup($_POST['username'], $oldUserGroup);
23562354

2357-
if ($createUserReal !== null) {
2358-
$queries[] = $createUserReal;
2359-
}
2360-
2355+
$queries[] = $createUserReal;
23612356
$queries[] = $realSqlQuery;
23622357

2363-
if (isset($_POST['authentication_plugin']) && ! empty($passwordSetReal)) {
2358+
if (isset($_POST['authentication_plugin']) && $passwordSetReal !== '') {
23642359
$this->setProperPasswordHashing($_POST['authentication_plugin']);
23652360

23662361
$queries[] = $passwordSetReal;
@@ -2370,11 +2365,9 @@ public function addUser(
23702365
// $queries_for_display, at the same position occupied
23712366
// by the real query in $queries
23722367
$tmpCount = count($queries);
2373-
if (isset($createUserReal)) {
2374-
$queriesForDisplay[$tmpCount - 2] = $createUserShow;
2375-
}
2368+
$queriesForDisplay[$tmpCount - 2] = $createUserShow;
23762369

2377-
if (! empty($passwordSetReal)) {
2370+
if ($passwordSetReal !== '') {
23782371
$queriesForDisplay[$tmpCount - 3] = $createUserShow;
23792372
$queriesForDisplay[$tmpCount - 2] = $sqlQuery;
23802373
$queriesForDisplay[$tmpCount - 1] = $passwordSetShow;
@@ -3017,8 +3010,7 @@ private function checkIfMariaDBPwdCheckPluginActive(): bool
30173010
* @param string $hostname host name
30183011
* @param string $password password
30193012
*
3020-
* @return mixed[] ($create_user_real, $create_user_show, $real_sql_query, $sql_query
3021-
* $password_set_real, $password_set_show, $alter_real_sql_query, $alter_sql_query)
3013+
* @return array{string, string, string, string, string, string, string, string}
30223014
*/
30233015
public function getSqlQueriesForDisplayAndAddUser(string $username, string $hostname, string $password): array
30243016
{
@@ -3079,7 +3071,7 @@ public function getSqlQueriesForDisplayAndAddUser(string $username, string $host
30793071
(Compatibility::isMySqlOrPerconaDb() && $serverVersion >= 50706)
30803072
|| (Compatibility::isMariaDb() && $serverVersion >= 50200)
30813073
) {
3082-
$passwordSetReal = null;
3074+
$passwordSetReal = '';
30833075

30843076
// Required for binding '%' with '%s'
30853077
$createUserStmt = str_replace('%', '%%', $createUserStmt);
@@ -3189,10 +3181,10 @@ public function getSqlQueriesForDisplayAndAddUser(string $username, string $host
31893181
|| (Compatibility::isMariaDb()
31903182
&& $serverVersion >= 50200)
31913183
) {
3192-
$passwordSetReal = null;
3193-
$passwordSetShow = null;
3184+
$passwordSetReal = '';
3185+
$passwordSetShow = '';
31943186
} else {
3195-
if ($passwordSetReal !== null) {
3187+
if ($passwordSetReal !== '') {
31963188
$passwordSetReal .= ';';
31973189
}
31983190

@@ -3239,7 +3231,7 @@ public function getRoutineType(string $dbname, string $routineName): string
32393231
* @param string $database Database name
32403232
* @param string $routine Routine name
32413233
*
3242-
* @return mixed[]
3234+
* @return array<string, string>
32433235
*/
32443236
private function getRoutinePrivileges(
32453237
string $username,

0 commit comments

Comments
 (0)