From d2b2e735e847b154ecf936e5811e04777072a68d Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Sun, 15 Dec 2024 23:34:14 +0000 Subject: [PATCH] Implement getErrorCount() in Events Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 30 --------------- psalm-baseline.xml | 37 ------------------- src/Controllers/Database/EventsController.php | 10 +---- src/Database/Events.php | 37 +++++++++++-------- tests/unit/Database/EventsTest.php | 4 +- 5 files changed, 24 insertions(+), 94 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c4a04c1246..52a3d96293 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2127,12 +2127,6 @@ parameters: count: 16 path: src/Controllers/Database/EventsController.php - - - message: '#^Only booleans are allowed in \|\|, int\<0, max\> given on the left side\.$#' - identifier: booleanOr.leftNotBoolean - count: 1 - path: src/Controllers/Database/EventsController.php - - message: '#^Parameter \#1 \$identifier of static method PhpMyAdmin\\Util\:\:backquote\(\) expects string\|Stringable\|null, mixed given\.$#' identifier: argument.type @@ -2151,12 +2145,6 @@ parameters: count: 1 path: src/Controllers/Database/EventsController.php - - - message: '#^Parameter \#1 \$value of function count expects array\|Countable, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Database/EventsController.php - - message: '#^Parameter \#2 \$name of method PhpMyAdmin\\Database\\Events\:\:getDetails\(\) expects string, mixed given\.$#' identifier: argument.type @@ -6621,18 +6609,6 @@ parameters: count: 1 path: src/Database/Designer/Common.php - - - message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#' - identifier: foreach.nonIterable - count: 1 - path: src/Database/Events.php - - - - message: '#^Binary operation "\." between ''\'' and mixed results in an error\.$#' - identifier: binaryOp.invalid - count: 1 - path: src/Database/Events.php - - message: '#^Binary operation "\.\=" between non\-falsy\-string and mixed results in an error\.$#' identifier: assignOp.invalid @@ -6648,12 +6624,6 @@ parameters: count: 1 path: src/Database/Events.php - - - message: '#^Cannot access an offset on mixed\.$#' - identifier: offsetAccess.nonOffsetAccessible - count: 7 - path: src/Database/Events.php - - message: '#^Cannot cast mixed to int\.$#' identifier: cast.int diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8cdb2db30f..8a74230ca8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -883,9 +883,6 @@ - - - @@ -893,7 +890,6 @@ - @@ -934,10 +930,6 @@ - - - - @@ -4148,29 +4140,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -4196,12 +4165,6 @@ - - - - - - diff --git a/src/Controllers/Database/EventsController.php b/src/Controllers/Database/EventsController.php index 2fc25e9f4d..5b797b2ada 100644 --- a/src/Controllers/Database/EventsController.php +++ b/src/Controllers/Database/EventsController.php @@ -18,7 +18,6 @@ use PhpMyAdmin\Template; use PhpMyAdmin\Util; use function __; -use function count; use function htmlspecialchars; use function mb_strtoupper; use function sprintf; @@ -37,8 +36,6 @@ final class EventsController implements InvocableController public function __invoke(ServerRequest $request): Response { - $GLOBALS['errors'] ??= null; - $this->response->addScriptFiles(['database/events.js', 'sql.js']); if (! $request->isAjax()) { @@ -56,11 +53,6 @@ final class EventsController implements InvocableController $this->dbi->selectDb(Current::$database); } - /** - * Keep a list of errors that occurred while - * processing an 'Add' or 'Edit' operation. - */ - $GLOBALS['errors'] = []; $GLOBALS['message'] ??= null; if (! empty($_POST['editor_process_add']) || ! empty($_POST['editor_process_edit'])) { @@ -109,7 +101,7 @@ final class EventsController implements InvocableController * Display a form used to add/edit a trigger, if necessary */ if ( - count($GLOBALS['errors']) + $this->events->getErrorCount() > 0 || empty($_POST['editor_process_add']) && empty($_POST['editor_process_edit']) && ( diff --git a/src/Database/Events.php b/src/Database/Events.php index 7512804a43..ca7f732c44 100644 --- a/src/Database/Events.php +++ b/src/Database/Events.php @@ -15,6 +15,7 @@ use PhpMyAdmin\Util; use function __; use function array_column; use function array_multisort; +use function count; use function explode; use function htmlspecialchars; use function in_array; @@ -39,6 +40,9 @@ class Events /** @var array */ public readonly array $interval; + /** @var list */ + private array $errors = []; + public function __construct(private DatabaseInterface $dbi) { $this->status = [ @@ -75,7 +79,7 @@ class Events $itemQuery = $this->getQueryFromRequest(); // set by getQueryFromRequest() - if ($GLOBALS['errors'] === []) { + if ($this->errors === []) { // Execute the created query if (! empty($_POST['editor_process_edit'])) { // Backup the old trigger, in case something goes wrong @@ -85,7 +89,7 @@ class Events . ";\n"; $result = $this->dbi->tryQuery($dropItem); if (! $result) { - $GLOBALS['errors'][] = sprintf( + $this->errors[] = sprintf( __('The following query has failed: "%s"'), htmlspecialchars($dropItem), ) @@ -94,7 +98,7 @@ class Events } else { $result = $this->dbi->tryQuery($itemQuery); if (! $result) { - $GLOBALS['errors'][] = sprintf( + $this->errors[] = sprintf( __('The following query has failed: "%s"'), htmlspecialchars($itemQuery), ) @@ -109,7 +113,7 @@ class Events // and now even the backup query does not execute! // This should not happen, but we better handle // this just in case. - $GLOBALS['errors'][] = __('Sorry, we failed to restore the dropped event.') . '
' + $this->errors[] = __('Sorry, we failed to restore the dropped event.') . '
' . __('The backed up query was:') . '"' . htmlspecialchars($createItem) . '"
' . __('MySQL said: ') . $this->dbi->getError(); @@ -128,7 +132,7 @@ class Events // 'Add a new item' mode $result = $this->dbi->tryQuery($itemQuery); if (! $result) { - $GLOBALS['errors'][] = sprintf( + $this->errors[] = sprintf( __('The following query has failed: "%s"'), htmlspecialchars($itemQuery), ) @@ -146,7 +150,7 @@ class Events } } - if ($GLOBALS['errors'] !== []) { + if ($this->errors !== []) { $GLOBALS['message'] = Message::error( '' . __( @@ -155,7 +159,7 @@ class Events . '', ); $GLOBALS['message']->addHtml('
    '); - foreach ($GLOBALS['errors'] as $string) { + foreach ($this->errors as $string) { $GLOBALS['message']->addHtml('
  • ' . $string . '
  • '); } @@ -256,8 +260,6 @@ class Events */ public function getQueryFromRequest(): string { - $GLOBALS['errors'] ??= null; - $query = 'CREATE '; if (! empty($_POST['item_definer'])) { if (str_contains($_POST['item_definer'], '@')) { @@ -265,7 +267,7 @@ class Events $query .= 'DEFINER=' . Util::backquote($arr[0]); $query .= '@' . Util::backquote($arr[1]) . ' '; } else { - $GLOBALS['errors'][] = __('The definer must be in the "username@hostname" format!'); + $this->errors[] = __('The definer must be in the "username@hostname" format!'); } } @@ -273,7 +275,7 @@ class Events if (! empty($_POST['item_name'])) { $query .= Util::backquote($_POST['item_name']) . ' '; } else { - $GLOBALS['errors'][] = __('You must provide an event name!'); + $this->errors[] = __('You must provide an event name!'); } $query .= 'ON SCHEDULE '; @@ -287,7 +289,7 @@ class Events $query .= 'EVERY ' . (int) $_POST['item_interval_value'] . ' '; $query .= $_POST['item_interval_field'] . ' '; } else { - $GLOBALS['errors'][] = __('You must provide a valid interval value for the event.'); + $this->errors[] = __('You must provide a valid interval value for the event.'); } if (! empty($_POST['item_starts'])) { @@ -300,10 +302,10 @@ class Events } elseif (! empty($_POST['item_execute_at'])) { $query .= 'AT ' . $this->dbi->quoteString($_POST['item_execute_at']) . ' '; } else { - $GLOBALS['errors'][] = __('You must provide a valid execution time for the event.'); + $this->errors[] = __('You must provide a valid execution time for the event.'); } } else { - $GLOBALS['errors'][] = __('You must provide a valid type for the event.'); + $this->errors[] = __('You must provide a valid type for the event.'); } $query .= 'ON COMPLETION '; @@ -329,7 +331,7 @@ class Events if (! empty($_POST['item_definition'])) { $query .= $_POST['item_definition']; } else { - $GLOBALS['errors'][] = __('You must provide an event definition.'); + $this->errors[] = __('You must provide an event definition.'); } return $query; @@ -388,4 +390,9 @@ class Events return is_string($result) ? $result : null; } + + public function getErrorCount(): int + { + return count($this->errors); + } } diff --git a/tests/unit/Database/EventsTest.php b/tests/unit/Database/EventsTest.php index c3db69b8cb..d5d245b064 100644 --- a/tests/unit/Database/EventsTest.php +++ b/tests/unit/Database/EventsTest.php @@ -144,8 +144,6 @@ class EventsTest extends AbstractTestCase #[DataProvider('providerGetQueryFromRequest')] public function testGetQueryFromRequest(array $request, string $query, int $numErr): void { - $GLOBALS['errors'] = []; - unset($_POST); $_POST = $request; @@ -155,7 +153,7 @@ class EventsTest extends AbstractTestCase DatabaseInterface::$instance = $dbi; self::assertSame($query, $this->events->getQueryFromRequest()); - self::assertCount($numErr, $GLOBALS['errors']); + self::assertSame($numErr, $this->events->getErrorCount()); } /**