Skip to content

Commit

Permalink
Extract method from Dbal\DbiMysqli::connect()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MauricioFauth committed Dec 29, 2023
1 parent 21d962d commit 8be31ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
45 changes: 21 additions & 24 deletions src/Dbal/DbiMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
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;
Expand Down Expand Up @@ -106,30 +107,11 @@ public function connect(Server $server): Connection|null
$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));
}

Expand Down Expand Up @@ -356,4 +338,19 @@ public function getWarningCount(Connection $connection): int
// 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');
}
}

0 comments on commit 8be31ed

Please sign in to comment.