Skip to content

Commit f0617f5

Browse files
kamil-tekielaMauricioFauth
authored andcommitted
Refactoring of ErrorHandler
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent fd7e1dc commit f0617f5

2 files changed

Lines changed: 14 additions & 93 deletions

File tree

‎src/Error/ErrorHandler.php‎

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Throwable;
1717

1818
use function __;
19+
use function array_filter;
1920
use function array_splice;
2021
use function count;
2122
use function defined;
@@ -44,7 +45,7 @@
4445
*/
4546
class ErrorHandler
4647
{
47-
public static self|null $instance = null;
48+
private static self|null $instance = null;
4849

4950
/**
5051
* holds errors to be displayed or reported later ...
@@ -343,7 +344,7 @@ public function dispUserErrors(): void
343344
/**
344345
* Renders user errors not displayed
345346
*/
346-
public function getDispUserErrors(): string
347+
private function getDispUserErrors(): string
347348
{
348349
$retval = '';
349350
foreach ($this->getErrors() as $error) {
@@ -377,9 +378,7 @@ public function getDispErrors(): string
377378
$retval .= $this->getDispUserErrors();
378379
}
379380

380-
// if preference is not 'never' and
381-
// there are 'actual' errors to be reported
382-
if ($config->settings['SendErrorReports'] !== 'never' && $this->countErrors() !== $this->countUserErrors()) {
381+
if ($config->settings['SendErrorReports'] !== 'never' && $this->countActualErrors() !== 0) {
383382
// add report button.
384383
$retval .= '<form method="post" action="' . Url::getFromRoute('/error-report')
385384
. '" id="pma_report_errors_form"';
@@ -422,7 +421,7 @@ public function getDispErrors(): string
422421
/**
423422
* look in session for saved errors
424423
*/
425-
protected function checkSavedErrors(): void
424+
private function checkSavedErrors(): void
426425
{
427426
if (! isset($_SESSION['errors'])) {
428427
return;
@@ -454,63 +453,26 @@ public function countErrors(bool $check = true): int
454453
return count($this->getErrors($check));
455454
}
456455

457-
/**
458-
* return count of user errors
459-
*
460-
* @return int number of user errors occurred
461-
*/
462456
public function countUserErrors(): int
463457
{
464-
$count = 0;
465-
if ($this->countErrors() !== 0) {
466-
foreach ($this->getErrors() as $error) {
467-
if (! $error->isUserError()) {
468-
continue;
469-
}
470-
471-
$count++;
472-
}
473-
}
474-
475-
return $count;
458+
return count(array_filter($this->getErrors(), static fn (Error $error): bool => $error->isUserError()));
476459
}
477460

478-
/**
479-
* whether use errors occurred or not
480-
*/
481-
public function hasUserErrors(): bool
461+
private function countActualErrors(): int
482462
{
483-
return (bool) $this->countUserErrors();
463+
return count(array_filter($this->getErrors(), static fn (Error $error): bool => ! $error->isUserError()));
484464
}
485465

486466
/**
487-
* whether errors occurred or not
488-
*/
489-
public function hasErrors(): bool
490-
{
491-
return (bool) $this->countErrors();
492-
}
493-
494-
/**
495-
* number of errors to be displayed
496-
*
497-
* @return int number of errors to be displayed
467+
* whether there are errors to display or not
498468
*/
499-
public function countDisplayErrors(): int
469+
public function hasDisplayErrors(): bool
500470
{
501471
if (Config::getInstance()->settings['SendErrorReports'] !== 'never') {
502-
return $this->countErrors();
472+
return $this->getErrors() !== [];
503473
}
504474

505-
return $this->countUserErrors();
506-
}
507-
508-
/**
509-
* whether there are errors to display or not
510-
*/
511-
public function hasDisplayErrors(): bool
512-
{
513-
return (bool) $this->countDisplayErrors();
475+
return $this->countUserErrors() !== 0;
514476
}
515477

516478
/**
@@ -534,7 +496,7 @@ public function savePreviousErrors(): void
534496
public function hasErrorsForPrompt(): bool
535497
{
536498
return Config::getInstance()->settings['SendErrorReports'] !== 'never'
537-
&& $this->countErrors() !== $this->countUserErrors();
499+
&& $this->countActualErrors() !== 0;
538500
}
539501

540502
/**
@@ -544,9 +506,7 @@ public function hasErrorsForPrompt(): bool
544506
*/
545507
public function reportErrors(): void
546508
{
547-
// if there're no actual errors,
548-
if (! $this->hasErrors() || $this->countErrors() === $this->countUserErrors()) {
549-
// then simply return.
509+
if ($this->getErrors() === [] || $this->countActualErrors() === 0) {
550510
return;
551511
}
552512

‎tests/unit/Error/ErrorHandlerTest.php‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ protected function tearDown(): void
6767

6868
public function testUniqueness(): void
6969
{
70-
ErrorHandler::$instance = null;
7170
$instanceOne = ErrorHandler::getInstance();
7271
$instanceTwo = ErrorHandler::getInstance();
7372
self::assertSame($instanceOne, $instanceTwo);
@@ -248,44 +247,6 @@ public function testCountUserErrors(): void
248247
);
249248
}
250249

251-
/**
252-
* Test for hasUserErrors
253-
*/
254-
public function testHasUserErrors(): void
255-
{
256-
self::assertFalse($this->object->hasUserErrors());
257-
}
258-
259-
/**
260-
* Test for hasErrors
261-
*/
262-
public function testHasErrors(): void
263-
{
264-
self::assertFalse($this->object->hasErrors());
265-
}
266-
267-
/**
268-
* Test for countDisplayErrors
269-
*/
270-
public function testCountDisplayErrorsForDisplayTrue(): void
271-
{
272-
self::assertSame(
273-
0,
274-
$this->object->countDisplayErrors(),
275-
);
276-
}
277-
278-
/**
279-
* Test for countDisplayErrors
280-
*/
281-
public function testCountDisplayErrorsForDisplayFalse(): void
282-
{
283-
self::assertSame(
284-
0,
285-
$this->object->countDisplayErrors(),
286-
);
287-
}
288-
289250
/**
290251
* Test for hasDisplayErrors
291252
*/

0 commit comments

Comments
 (0)