Implement getErrorCount() in Events

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2024-12-15 23:34:14 +00:00
parent 86aa9da2f2
commit d2b2e735e8
5 changed files with 24 additions and 94 deletions

View File

@ -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 ''\<li\>'' 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

View File

@ -883,9 +883,6 @@
</PossiblyUnusedReturnValue>
</file>
<file src="src/Controllers/Database/EventsController.php">
<InvalidArrayOffset>
<code><![CDATA[$GLOBALS['errors']]]></code>
</InvalidArrayOffset>
<MixedArgument>
<code><![CDATA[Current::$database]]></code>
<code><![CDATA[Current::$database]]></code>
@ -893,7 +890,6 @@
<code><![CDATA[Current::$database]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$GLOBALS['errors']]]></code>
<code><![CDATA[$item['item_original_name']]]></code>
</MixedAssignment>
<PossiblyInvalidArgument>
@ -934,10 +930,6 @@
<code><![CDATA[empty($_REQUEST['edit_item'])]]></code>
<code><![CDATA[empty($_REQUEST['item_name'])]]></code>
</RiskyTruthyFalsyComparison>
<TypeDoesNotContainType>
<code><![CDATA[count($GLOBALS['errors'])]]></code>
<code><![CDATA[count($GLOBALS['errors'])]]></code>
</TypeDoesNotContainType>
</file>
<file src="src/Controllers/Database/ExportController.php">
<InvalidArrayOffset>
@ -4148,29 +4140,6 @@
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InvalidArrayOffset>
<code><![CDATA[$GLOBALS['errors']]]></code>
<code><![CDATA[$GLOBALS['errors']]]></code>
</InvalidArrayOffset>
<MixedArrayAssignment>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
<code><![CDATA[$GLOBALS['errors'][]]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$GLOBALS['errors']]]></code>
<code><![CDATA[$string]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[$string]]></code>
</MixedOperand>
<PossiblyInvalidArgument>
<code><![CDATA[$_POST['item_comment']]]></code>
<code><![CDATA[$_POST['item_definer']]]></code>
@ -4196,12 +4165,6 @@
<PossiblyNullArgument>
<code><![CDATA[$createItem]]></code>
</PossiblyNullArgument>
<PossiblyNullIterator>
<code><![CDATA[$GLOBALS['errors']]]></code>
</PossiblyNullIterator>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$GLOBALS['errors']]]></code>
</PossiblyUndefinedArrayOffset>
<RiskyCast>
<code><![CDATA[$_POST['item_interval_value']]]></code>
</RiskyCast>

View File

@ -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'])
&& (

View File

@ -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<int, string> */
public readonly array $interval;
/** @var list<string> */
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.') . '<br>'
$this->errors[] = __('Sorry, we failed to restore the dropped event.') . '<br>'
. __('The backed up query was:')
. '"' . htmlspecialchars($createItem) . '"<br>'
. __('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(
'<b>'
. __(
@ -155,7 +159,7 @@ class Events
. '</b>',
);
$GLOBALS['message']->addHtml('<ul>');
foreach ($GLOBALS['errors'] as $string) {
foreach ($this->errors as $string) {
$GLOBALS['message']->addHtml('<li>' . $string . '</li>');
}
@ -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);
}
}

View File

@ -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());
}
/**