Skip to content

Commit 2d75038

Browse files
Merge pull request #18634 from MauricioFauth/error-handler-singleton
Replace ErrorHandler global var with a singleton object
2 parents 2dd24ef + 678a829 commit 2d75038

17 files changed

Lines changed: 40 additions & 83 deletions

libraries/classes/Application.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ public static function init(): self
7575

7676
public function run(bool $isSetupPage = false): void
7777
{
78-
$GLOBALS['errorHandler'] = $this->errorHandler;
79-
8078
$requestHandler = new QueueRequestHandler(new ApplicationHandler($this));
8179
$requestHandler->add(new ErrorHandling($this->errorHandler));
8280
$requestHandler->add(new OutputBuffering());

libraries/classes/Core.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ public static function warnMissingExtension(
105105
bool $fatal = false,
106106
string $extra = '',
107107
): void {
108-
$GLOBALS['errorHandler'] ??= null;
109-
110108
$message = 'The %s extension is missing. Please check your PHP configuration.';
111109

112110
/* Gettext does not have to be loaded yet here */
@@ -124,7 +122,7 @@ public static function warnMissingExtension(
124122
throw new MissingExtensionException(Sanitize::sanitizeMessage($message));
125123
}
126124

127-
$GLOBALS['errorHandler']->addError($message, E_USER_WARNING, '', 0, false);
125+
ErrorHandler::getInstance()->addError($message, E_USER_WARNING, '', 0, false);
128126
}
129127

130128
/**

libraries/classes/DatabaseInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,9 +1577,10 @@ public function connect(Server $currentServer, int $connectionType, int|null $ta
15771577
$target ??= $connectionType;
15781578

15791579
// Do not show location and backtrace for connection errors
1580-
$GLOBALS['errorHandler']->setHideLocation(true);
1580+
$errorHandler = ErrorHandler::getInstance();
1581+
$errorHandler->setHideLocation(true);
15811582
$result = $this->extension->connect($server);
1582-
$GLOBALS['errorHandler']->setHideLocation(false);
1583+
$errorHandler->setHideLocation(false);
15831584

15841585
if ($result !== null) {
15851586
$this->connections[$target] = $result;

libraries/classes/ErrorHandler.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242
class ErrorHandler
4343
{
44+
public static self|null $instance = null;
45+
4446
/**
4547
* holds errors to be displayed or reported later ...
4648
*
@@ -67,6 +69,15 @@ public function __construct()
6769
$this->errorReporting = error_reporting();
6870
}
6971

72+
public static function getInstance(): self
73+
{
74+
if (self::$instance === null) {
75+
self::$instance = new self();
76+
}
77+
78+
return self::$instance;
79+
}
80+
7081
/**
7182
* Destructor
7283
*
@@ -519,7 +530,7 @@ public function hasDisplayErrors(): bool
519530
public function savePreviousErrors(): void
520531
{
521532
unset($_SESSION['prev_errors']);
522-
$_SESSION['prev_errors'] = $GLOBALS['errorHandler']->getCurrentErrors();
533+
$_SESSION['prev_errors'] = $this->getCurrentErrors();
523534
}
524535

525536
/**
@@ -538,7 +549,7 @@ public function hasErrorsForPrompt(): bool
538549
/**
539550
* Function to report all the collected php errors.
540551
* Must be called at the end of each script
541-
* by the $GLOBALS['errorHandler'] only.
552+
* by the {@see getInstance()} only.
542553
*/
543554
public function reportErrors(): void
544555
{

libraries/classes/Footer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ public function getSelfUrl(): string
152152
public function getErrorMessages(): string
153153
{
154154
$retval = '';
155-
if ($GLOBALS['errorHandler']->hasDisplayErrors()) {
156-
$retval .= $GLOBALS['errorHandler']->getDispErrors();
155+
$errorHandler = ErrorHandler::getInstance();
156+
if ($errorHandler->hasDisplayErrors()) {
157+
$retval .= $errorHandler->getDispErrors();
157158
}
158159

159160
/**
160161
* Report php errors
161162
*/
162-
$GLOBALS['errorHandler']->reportErrors();
163+
$errorHandler->reportErrors();
163164

164165
return $retval;
165166
}

libraries/classes/Plugins/Auth/AuthenticationConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace PhpMyAdmin\Plugins\Auth;
99

1010
use PhpMyAdmin\Config;
11+
use PhpMyAdmin\ErrorHandler;
1112
use PhpMyAdmin\Html\Generator;
1213
use PhpMyAdmin\Plugins\AuthenticationPlugin;
1314
use PhpMyAdmin\ResponseRenderer;
@@ -132,7 +133,7 @@ public function showFailure(string $failure): never
132133
echo Generator::mysqlDie($connError, '', true, '', false);
133134
}
134135

135-
$GLOBALS['errorHandler']->dispUserErrors();
136+
ErrorHandler::getInstance()->dispUserErrors();
136137
echo '</td>
137138
</tr>
138139
<tr>

libraries/classes/Plugins/Auth/AuthenticationCookie.php

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

1010
use PhpMyAdmin\Config;
1111
use PhpMyAdmin\Core;
12+
use PhpMyAdmin\ErrorHandler;
1213
use PhpMyAdmin\Exceptions\SessionHandlerException;
1314
use PhpMyAdmin\LanguageManager;
1415
use PhpMyAdmin\Message;
@@ -153,8 +154,9 @@ public function showLoginForm(): never
153154
}
154155

155156
$errors = '';
156-
if ($GLOBALS['errorHandler']->hasDisplayErrors()) {
157-
$errors = $GLOBALS['errorHandler']->getDispErrors();
157+
$errorHandler = ErrorHandler::getInstance();
158+
if ($errorHandler->hasDisplayErrors()) {
159+
$errors = $errorHandler->getDispErrors();
158160
}
159161

160162
// close the wrapping div tag, if the request is after session timeout

libraries/classes/ResponseRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private function ajaxResponse(): string
307307
$this->addJSON('errors', $errors);
308308
}
309309

310-
$promptPhpErrors = $GLOBALS['errorHandler']->hasErrorsForPrompt();
310+
$promptPhpErrors = ErrorHandler::getInstance()->hasErrorsForPrompt();
311311
$this->addJSON('promptPhpErrors', $promptPhpErrors);
312312

313313
if (empty($GLOBALS['error_message'])) {

libraries/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
'class' => Common::class,
9393
'arguments' => ['$dbi' => '@dbi', '$relation' => '@relation'],
9494
],
95-
'error_handler' => ['class' => ErrorHandler::class],
95+
'error_handler' => ['class' => ErrorHandler::class, 'factory' => [ErrorHandler::class, 'getInstance']],
9696
'error_report' => [
9797
'class' => ErrorReport::class,
9898
'arguments' => ['@http_request', '@relation', '@template', '@config'],

phpstan-baseline.neon

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10195,11 +10195,6 @@ parameters:
1019510195
count: 10
1019610196
path: libraries/classes/Core.php
1019710197

10198-
-
10199-
message: "#^Cannot call method addError\\(\\) on mixed\\.$#"
10200-
count: 1
10201-
path: libraries/classes/Core.php
10202-
1020310198
-
1020410199
message: "#^Cannot call method numRows\\(\\) on mixed\\.$#"
1020510200
count: 1
@@ -11535,11 +11530,6 @@ parameters:
1153511530
count: 3
1153611531
path: libraries/classes/DatabaseInterface.php
1153711532

11538-
-
11539-
message: "#^Cannot call method setHideLocation\\(\\) on mixed\\.$#"
11540-
count: 2
11541-
path: libraries/classes/DatabaseInterface.php
11542-
1154311533
-
1154411534
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
1154511535
count: 1
@@ -12640,11 +12630,6 @@ parameters:
1264012630
count: 1
1264112631
path: libraries/classes/ErrorHandler.php
1264212632

12643-
-
12644-
message: "#^Cannot call method getCurrentErrors\\(\\) on mixed\\.$#"
12645-
count: 1
12646-
path: libraries/classes/ErrorHandler.php
12647-
1264812633
-
1264912634
message: "#^Only booleans are allowed in a negated boolean, int given\\.$#"
1265012635
count: 1
@@ -13225,26 +13210,11 @@ parameters:
1322513210
count: 1
1322613211
path: libraries/classes/Footer.php
1322713212

13228-
-
13229-
message: "#^Cannot call method getDispErrors\\(\\) on mixed\\.$#"
13230-
count: 1
13231-
path: libraries/classes/Footer.php
13232-
13233-
-
13234-
message: "#^Cannot call method hasDisplayErrors\\(\\) on mixed\\.$#"
13235-
count: 1
13236-
path: libraries/classes/Footer.php
13237-
1323813213
-
1323913214
message: "#^Cannot call method isConnected\\(\\) on mixed\\.$#"
1324013215
count: 1
1324113216
path: libraries/classes/Footer.php
1324213217

13243-
-
13244-
message: "#^Cannot call method reportErrors\\(\\) on mixed\\.$#"
13245-
count: 1
13246-
path: libraries/classes/Footer.php
13247-
1324813218
-
1324913219
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
1325013220
count: 4
@@ -17450,11 +17420,6 @@ parameters:
1745017420
count: 1
1745117421
path: libraries/classes/Plugins/Auth/AuthenticationConfig.php
1745217422

17453-
-
17454-
message: "#^Cannot call method dispUserErrors\\(\\) on mixed\\.$#"
17455-
count: 1
17456-
path: libraries/classes/Plugins/Auth/AuthenticationConfig.php
17457-
1745817423
-
1745917424
message: "#^Cannot call method getError\\(\\) on mixed\\.$#"
1746017425
count: 1
@@ -17610,16 +17575,6 @@ parameters:
1761017575
count: 1
1761117576
path: libraries/classes/Plugins/Auth/AuthenticationCookie.php
1761217577

17613-
-
17614-
message: "#^Cannot call method getDispErrors\\(\\) on mixed\\.$#"
17615-
count: 1
17616-
path: libraries/classes/Plugins/Auth/AuthenticationCookie.php
17617-
17618-
-
17619-
message: "#^Cannot call method hasDisplayErrors\\(\\) on mixed\\.$#"
17620-
count: 1
17621-
path: libraries/classes/Plugins/Auth/AuthenticationCookie.php
17622-
1762317578
-
1762417579
message: "#^Cannot cast mixed to int\\.$#"
1762517580
count: 1
@@ -22805,11 +22760,6 @@ parameters:
2280522760
count: 1
2280622761
path: libraries/classes/ResponseRenderer.php
2280722762

22808-
-
22809-
message: "#^Cannot call method hasErrorsForPrompt\\(\\) on mixed\\.$#"
22810-
count: 1
22811-
path: libraries/classes/ResponseRenderer.php
22812-
2281322763
-
2281422764
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
2281522765
count: 5

0 commit comments

Comments
 (0)