Skip to content

Commit 65cd15d

Browse files
Merge pull request #19569 from kamil-tekiela/Replace-trigger_error-in-mysqli
Add an error instead of triggering warning
2 parents 0d04103 + c76d164 commit 65cd15d

File tree

16 files changed

+119
-145
lines changed

16 files changed

+119
-145
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6867,12 +6867,6 @@ parameters:
68676867
count: 1
68686868
path: src/Error/ErrorHandler.php
68696869

6870-
-
6871-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
6872-
identifier: notEqual.notAllowed
6873-
count: 1
6874-
path: src/Error/ErrorHandler.php
6875-
68766870
-
68776871
message: '#^Parameter \#1 \$value of function count expects array\|Countable, mixed given\.$#'
68786872
identifier: argument.type

resources/templates/error/get_display.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
{{ message|raw }}
77

8-
{%- if not is_user_error -%}
8+
{%- if not is_user_error and formatted_backtrace is not empty -%}
99
<p class="mt-3"><strong>Backtrace</strong></p>
1010
{{- formatted_backtrace|raw -}}
1111
{%- endif -%}

src/Config/FormDisplay.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use PhpMyAdmin\Config;
1818
use PhpMyAdmin\Config\Forms\User\UserFormList;
19+
use PhpMyAdmin\Error\ErrorHandler;
1920
use PhpMyAdmin\Html\MySQLDocumentation;
2021
use PhpMyAdmin\Util;
2122

@@ -37,11 +38,8 @@
3738
use function str_ends_with;
3839
use function str_replace;
3940
use function str_starts_with;
40-
use function trigger_error;
4141
use function trim;
4242

43-
use const E_USER_WARNING;
44-
4543
/**
4644
* Form management class, displays and processes forms
4745
*/
@@ -391,7 +389,7 @@ private function displayFieldInput(
391389
return $htmlOutput;
392390

393391
case 'NULL':
394-
trigger_error('Field ' . $systemPath . ' has no type', E_USER_WARNING);
392+
ErrorHandler::getInstance()->addUserError('Field ' . $systemPath . ' has no type');
395393

396394
return null;
397395
}

src/Core.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use function urldecode;
4646

4747
use const DATE_RFC1123;
48-
use const E_USER_WARNING;
4948
use const FILTER_VALIDATE_IP;
5049

5150
/**
@@ -115,7 +114,7 @@ public static function warnMissingExtension(
115114
throw new MissingExtensionException(Sanitize::convertBBCode($message));
116115
}
117116

118-
ErrorHandler::getInstance()->addError($message, E_USER_WARNING, '', 0, false);
117+
ErrorHandler::getInstance()->addUserError($message, false);
119118
}
120119

121120
/**

src/Dbal/DatabaseInterface.php

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@
6060
use function strtr;
6161
use function substr;
6262
use function syslog;
63-
use function trigger_error;
6463
use function uasort;
6564
use function uksort;
6665
use function usort;
6766

68-
use const E_USER_WARNING;
6967
use const LOG_INFO;
7068
use const LOG_NDELAY;
7169
use const LOG_PID;
@@ -1049,20 +1047,21 @@ public function postConnect(Server $currentServer): void
10491047
. $this->quoteString($currentServer->sessionTimeZone);
10501048

10511049
if (! $this->tryQuery($sqlQueryTz)) {
1052-
$errorMessageTz = sprintf(
1053-
__(
1054-
'Unable to use timezone "%1$s" for server %2$d. '
1055-
. 'Please check your configuration setting for '
1056-
. '[em]$cfg[\'Servers\'][%3$d][\'SessionTimeZone\'][/em]. '
1057-
. 'phpMyAdmin is currently using the default time zone '
1058-
. 'of the database server.',
1050+
$errorHandler = ErrorHandler::getInstance();
1051+
$errorHandler->addUserError(
1052+
sprintf(
1053+
__(
1054+
'Unable to use timezone "%1$s" for server %2$d. '
1055+
. 'Please check your configuration setting for '
1056+
. '[em]$cfg[\'Servers\'][%3$d][\'SessionTimeZone\'][/em]. '
1057+
. 'phpMyAdmin is currently using the default time zone '
1058+
. 'of the database server.',
1059+
),
1060+
$currentServer->sessionTimeZone,
1061+
Current::$server,
1062+
Current::$server,
10591063
),
1060-
$currentServer->sessionTimeZone,
1061-
Current::$server,
1062-
Current::$server,
10631064
);
1064-
1065-
trigger_error($errorMessageTz, E_USER_WARNING);
10661065
}
10671066
}
10681067

@@ -1092,10 +1091,8 @@ public function setCollation(string $collation): void
10921091
);
10931092

10941093
if ($result === false) {
1095-
trigger_error(
1096-
__('Failed to set configured collation connection!'),
1097-
E_USER_WARNING,
1098-
);
1094+
$errorHandler = ErrorHandler::getInstance();
1095+
$errorHandler->addUserError(__('Failed to set configured collation connection!'));
10991096

11001097
return;
11011098
}
@@ -1606,33 +1603,20 @@ public function connect(
16061603
try {
16071604
$result = $this->extension->connect($server);
16081605
} catch (ConnectionException $exception) {
1609-
trigger_error($exception->getMessage(), E_USER_WARNING);
1606+
$errorHandler->addUserError($exception->getMessage());
16101607

16111608
return null;
16121609
}
16131610

16141611
$errorHandler->setHideLocation(false);
16151612

1616-
if ($result !== null) {
1617-
$this->connections[$target->value] = $result;
1618-
/* Run post connect for user connections */
1619-
if ($target === ConnectionType::User) {
1620-
$this->postConnect($currentServer);
1621-
}
1622-
1623-
return $result;
1624-
}
1625-
1626-
if ($connectionType === ConnectionType::ControlUser) {
1627-
trigger_error(
1628-
__(
1629-
'Connection for controluser as defined in your configuration failed.',
1630-
),
1631-
E_USER_WARNING,
1632-
);
1613+
$this->connections[$target->value] = $result;
1614+
/* Run post connect for user connections */
1615+
if ($target === ConnectionType::User) {
1616+
$this->postConnect($currentServer);
16331617
}
16341618

1635-
return null;
1619+
return $result;
16361620
}
16371621

16381622
/**
@@ -1702,6 +1686,11 @@ public function getError(ConnectionType $connectionType = ConnectionType::User):
17021686
return $this->extension->getError($this->connections[$connectionType->value]);
17031687
}
17041688

1689+
public function getConnectionErrorNumber(): int
1690+
{
1691+
return $this->extension->getConnectionErrorNumber();
1692+
}
1693+
17051694
/**
17061695
* returns last inserted auto_increment id for given $link
17071696
*/

src/Dbal/DbiExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface DbiExtension
2020
*
2121
* @throws ConnectionException
2222
*/
23-
public function connect(Server $server): Connection|null;
23+
public function connect(Server $server): Connection;
2424

2525
/**
2626
* selects given database
@@ -74,6 +74,11 @@ public function getClientInfo(): string;
7474
*/
7575
public function getError(Connection $connection): string;
7676

77+
/**
78+
* Returns the error code for the most recent connection attempt.
79+
*/
80+
public function getConnectionErrorNumber(): int;
81+
7782
/**
7883
* returns the number of rows affected by last query
7984
*

src/Dbal/DbiMysqli.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
use function __;
1818
use function defined;
19+
use function mysqli_connect_errno;
1920
use function mysqli_get_client_info;
20-
use function mysqli_init;
2121
use function mysqli_report;
2222
use function sprintf;
2323
use function str_contains;
@@ -39,15 +39,11 @@
3939
*/
4040
class DbiMysqli implements DbiExtension
4141
{
42-
public function connect(Server $server): Connection|null
42+
public function connect(Server $server): Connection
4343
{
4444
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
4545

46-
$mysqli = mysqli_init();
47-
48-
if ($mysqli === false) {
49-
return null;
50-
}
46+
$mysqli = new mysqli();
5147

5248
$clientFlags = 0;
5349

@@ -254,6 +250,14 @@ public function getError(Connection $connection): string
254250
return Utilities::formatError($errorNumber, $errorMessage);
255251
}
256252

253+
/**
254+
* Returns the error code for the most recent connection attempt.
255+
*/
256+
public function getConnectionErrorNumber(): int
257+
{
258+
return mysqli_connect_errno();
259+
}
260+
257261
/**
258262
* returns the number of rows affected by last query
259263
*

src/Error/Error.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ public function getBacktraceDisplay(): string
326326
*/
327327
public static function formatBacktrace(array $backtrace): string
328328
{
329+
if ($backtrace === []) {
330+
return '';
331+
}
332+
329333
$retval = '<ol class="list-group">';
330334

331335
foreach ($backtrace as $step) {

src/Error/ErrorHandler.php

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -197,34 +197,24 @@ public function handleError(
197197
string $errfile,
198198
int $errline,
199199
): bool {
200-
if (function_exists('error_reporting')) {
201-
/**
202-
* Check if Error Control Operator (@) was used, but still show
203-
* user errors even in this case.
204-
* See: https://github.com/phpmyadmin/phpmyadmin/issues/16729
205-
*/
206-
$isSilenced = (error_reporting() & $errno) === 0;
207-
208-
$config = Config::getInstance();
209-
if (
210-
$config->config->environment === 'development'
211-
&& ! $isSilenced
212-
) {
213-
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
214-
}
215-
216-
if (
217-
$isSilenced &&
218-
$this->errorReporting != 0 &&
219-
($errno & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE | E_USER_DEPRECATED)) === 0
220-
) {
221-
return false;
222-
}
223-
} elseif (($errno & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE | E_USER_DEPRECATED)) === 0) {
200+
if (! function_exists('error_reporting')) {
224201
return false;
225202
}
226203

227-
$this->addError($errstr, $errno, $errfile, $errline);
204+
/**
205+
* Check if Error Control Operator (@) was used.
206+
* See: https://github.com/phpmyadmin/phpmyadmin/issues/16729
207+
*/
208+
$isSilenced = (error_reporting() & $errno) === 0;
209+
210+
$config = Config::getInstance();
211+
if ($config->config->environment === 'development' && ! $isSilenced) {
212+
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
213+
}
214+
215+
if (! $isSilenced || $this->errorReporting === 0) {
216+
$this->addError($errstr, $errno, $errfile, $errline);
217+
}
228218

229219
return false;
230220
}
@@ -314,6 +304,30 @@ public function addError(
314304
}
315305
}
316306

307+
public function addUserError(string $message, bool $escape = true): void
308+
{
309+
// The file name and line number are not relevant for user errors
310+
$error = new Error(
311+
E_USER_WARNING,
312+
$escape ? htmlspecialchars($message) : $message,
313+
__FILE__,
314+
__LINE__,
315+
);
316+
$this->errors[$error->getHash()] = $error;
317+
}
318+
319+
public function addNotice(string $message, bool $escape = true): void
320+
{
321+
// The file name and line number are not relevant for user errors
322+
$error = new Error(
323+
E_USER_NOTICE,
324+
$escape ? htmlspecialchars($message) : $message,
325+
__FILE__,
326+
__LINE__,
327+
);
328+
$this->errors[$error->getHash()] = $error;
329+
}
330+
317331
/**
318332
* display fatal error and exit
319333
*

src/Navigation/NavigationTree.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\ConfigStorage\RelationParameters;
1313
use PhpMyAdmin\Current;
1414
use PhpMyAdmin\Dbal\DatabaseInterface;
15+
use PhpMyAdmin\Error\ErrorHandler;
1516
use PhpMyAdmin\Favorites\RecentFavoriteTables;
1617
use PhpMyAdmin\Favorites\TableType;
1718
use PhpMyAdmin\Html\Generator;
@@ -67,12 +68,9 @@
6768
use function strrpos;
6869
use function strstr;
6970
use function substr;
70-
use function trigger_error;
7171
use function trim;
7272
use function usort;
7373

74-
use const E_USER_WARNING;
75-
7674
/**
7775
* Displays a collapsible of database objects in the navigation frame
7876
*/
@@ -739,13 +737,12 @@ public function groupNode(Node $node): void
739737
foreach ($prefixes as $key => $value) {
740738
// warn about large groups
741739
if ($value > 500 && ! $this->largeGroupWarning) {
742-
trigger_error(
740+
ErrorHandler::getInstance()->addUserError(
743741
__(
744742
'There are large item groups in navigation panel which '
745743
. 'may affect the performance. Consider disabling item '
746744
. 'grouping in the navigation panel.',
747745
),
748-
E_USER_WARNING,
749746
);
750747
$this->largeGroupWarning = true;
751748
}

0 commit comments

Comments
 (0)