phpmyadmin/libraries/classes/Controllers/Server/Databases/DestroyController.php
Kamil Tekiela 51f4017b32
Refactor destroy controller (#17194)
* Remove redundant conditions

They were always true. First, the variable is set to -1.
If the variable is -1, then set it to Message.
If the variable is Message then set the JSON array. All of this is
redundant.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $sqlQuery is not used anywhere

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Enforce $selected to be an array

I can't remove it as it is a global. Previously, it was set to a POST
variable. Since we validate that the variable is set and is not null,
we can use the value from $params. If we validate that it's also an
array we can replace for loop with foreach.
There is no passing by reference, so this change should be safe.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Replace $rebuildDatabaseList with input validation

The validation message says 'No databases selected.' so I assume that
we should check that we actually got at least one entry in the list.
If that is true, then the $rebuildDatabaseList is not needed and the
condition redundant.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* drop_selected_dbs is always set and is redundant

I am not sure if this is necessary. The AJAX request sends a value of "1"
but the code doesn't really do anything with it other than checking if
it is actually set. Is there some other path possible?

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Flatten the array with a single element

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Remove redundant isset

The variable is set, so !isset === is_null. Why check for null if we can
check just for !is_array.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Update psalm-baseline.xml

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Adhere to the coding standard

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
2021-11-19 14:26:04 -03:00

104 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Server\Databases;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use function __;
use function _ngettext;
use function count;
use function is_array;
final class DestroyController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var Transformations */
private $transformations;
/** @var RelationCleanup */
private $relationCleanup;
public function __construct(
ResponseRenderer $response,
Template $template,
DatabaseInterface $dbi,
Transformations $transformations,
RelationCleanup $relationCleanup
) {
parent::__construct($response, $template);
$this->dbi = $dbi;
$this->transformations = $transformations;
$this->relationCleanup = $relationCleanup;
}
public function __invoke(): void
{
global $selected, $errorUrl, $cfg, $dblist, $reload;
$selected_dbs = $_POST['selected_dbs'] ?? null;
if (
! $this->response->isAjax()
|| (! $this->dbi->isSuperUser() && ! $cfg['AllowUserDropDatabase'])
) {
$message = Message::error();
$json = ['message' => $message];
$this->response->setRequestStatus($message->isSuccess());
$this->response->addJSON($json);
return;
}
if (
! is_array($selected_dbs)
|| $selected_dbs === []
) {
$message = Message::error(__('No databases selected.'));
$json = ['message' => $message];
$this->response->setRequestStatus($message->isSuccess());
$this->response->addJSON($json);
return;
}
$errorUrl = Url::getFromRoute('/server/databases');
$selected = $selected_dbs;
$numberOfDatabases = count($selected_dbs);
foreach ($selected_dbs as $database) {
$this->relationCleanup->database($database);
$aQuery = 'DROP DATABASE ' . Util::backquote($database);
$reload = true;
$this->dbi->query($aQuery);
$this->transformations->clear($database);
}
$dblist->databases->build();
$message = Message::success(
_ngettext(
'%1$d database has been dropped successfully.',
'%1$d databases have been dropped successfully.',
$numberOfDatabases
)
);
$message->addParam($numberOfDatabases);
$json = ['message' => $message];
$this->response->setRequestStatus($message->isSuccess());
$this->response->addJSON($json);
}
}