Extract method from Dbal\DbiMysqli::connect()

Extracts the isSslRequiredByServer() private method from
PhpMyAdmin\Dbal\DbiMysqli::connect().

Uses the error message and code from the exception instead of mysqli
object.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2023-12-29 13:47:31 -03:00
parent 21d962dec7
commit 8be31eddc1
3 changed files with 21 additions and 35 deletions

View File

@ -7460,11 +7460,6 @@ parameters:
count: 1
path: src/Dbal/DbalInterface.php
-
message: "#^Parameter \\#1 \\$haystack of function stripos expects string, string\\|null given\\.$#"
count: 1
path: src/Dbal/DbiMysqli.php
-
message: "#^Method PhpMyAdmin\\\\Dbal\\\\MysqliResult\\:\\:fetchAllAssoc\\(\\) should return array\\<int, array\\<string\\|null\\>\\> but returns array\\.$#"
count: 1

View File

@ -5273,12 +5273,6 @@
<DeprecatedMethod>
<code>Config::getInstance()</code>
</DeprecatedMethod>
<PossiblyNullArgument>
<code>$errorMessage</code>
</PossiblyNullArgument>
<PossiblyNullOperand>
<code>$errorMessage</code>
</PossiblyNullOperand>
</file>
<file src="src/Dbal/MysqliResult.php">
<InvalidReturnStatement>

View File

@ -21,7 +21,8 @@ use function mysqli_get_client_info;
use function mysqli_init;
use function mysqli_report;
use function sprintf;
use function stripos;
use function str_contains;
use function strtolower;
use function trigger_error;
use const E_USER_ERROR;
@ -106,30 +107,11 @@ class DbiMysqli implements DbiExtension
$server->socket,
$clientFlags,
);
} catch (mysqli_sql_exception) {
/**
* Switch to SSL if server asked us to do so, unfortunately
* there are more ways MySQL server can tell this:
*
* - MySQL 8.0 and newer should return error 3159
* - #2001 - SSL Connection is required. Please specify SSL options and retry.
* - #9002 - SSL connection is required. Please specify SSL options and retry.
*/
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$errorNumber = $mysqli->connect_errno;
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$errorMessage = $mysqli->connect_error;
if (
! $server->ssl
&& ($errorNumber == 3159
|| (($errorNumber == 2001 || $errorNumber == 9002)
&& stripos($errorMessage, 'SSL Connection is required') !== false))
) {
trigger_error(
__('SSL connection enforced by server, automatically enabling it.'),
E_USER_WARNING,
);
} catch (mysqli_sql_exception $exception) {
$errorNumber = $exception->getCode();
$errorMessage = $exception->getMessage();
if (! $server->ssl && $this->isSslRequiredByServer($errorNumber, $errorMessage)) {
return self::connect($server->withSSL(true));
}
@ -356,4 +338,19 @@ class DbiMysqli implements DbiExtension
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return $mysqli->warning_count;
}
/**
* Switch to SSL if server asked us to do so, unfortunately
* there are more ways MySQL server can tell this:
*
* - MySQL 8.0 and newer should return error 3159
* - #2001 - SSL Connection is required. Please specify SSL options and retry.
* - #9002 - SSL connection is required. Please specify SSL options and retry.
*/
private function isSslRequiredByServer(int $errorNumber, string $errorMessage): bool
{
return $errorNumber === 3159
|| ($errorNumber === 2001 || $errorNumber === 9002)
&& str_contains(strtolower($errorMessage), 'ssl connection is required');
}
}