From 44d1600ce2efa0e6f43a1915253cdd0e596056a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 11 Apr 2020 14:51:56 -0300 Subject: [PATCH] Use early exit in Rte\Export class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Rte/Export.php | 135 ++++++++++++++++--------------- phpstan-baseline.neon | 10 --- 2 files changed, 71 insertions(+), 74 deletions(-) diff --git a/libraries/classes/Rte/Export.php b/libraries/classes/Rte/Export.php index 0472d12fb7..63e0d896c1 100644 --- a/libraries/classes/Rte/Export.php +++ b/libraries/classes/Rte/Export.php @@ -34,12 +34,12 @@ class Export * This function is called from one of the other functions in this file * and it completes the handling of the export functionality. * - * @param string $export_data The SQL query to create the requested item - * @param string $type RTE type (routine|trigger|event). + * @param string|false $export_data The SQL query to create the requested item + * @param string $type RTE type (routine|trigger|event). * * @return void */ - private function handle($export_data, string $type) + private function handle($export_data, string $type): void { global $db; @@ -63,32 +63,37 @@ class Export if ($export_data !== false) { $export_data = htmlspecialchars(trim($export_data)); $title = sprintf($exportMessage, $item_name); + if ($response->isAjax()) { $response->addJSON('message', $export_data); $response->addJSON('title', $title); - exit; - } else { - $export_data = ''; - echo "
\n" - , '' . $title . "\n" - , $export_data - , "
\n"; - } - } else { - $_db = htmlspecialchars(Util::backquote($db)); - $message = __('Error in processing request:') . ' ' - . sprintf($noViewMessage, $item_name, $_db); - $message = Message::error($message); - if ($response->isAjax()) { - $response->setRequestStatus(false); - $response->addJSON('message', $message); exit; - } else { - $message->display(); } + + $export_data = ''; + echo "
\n" + , '' . $title . "\n" + , $export_data + , "
\n"; + + return; } + + $_db = htmlspecialchars(Util::backquote($db)); + $message = __('Error in processing request:') . ' ' + . sprintf($noViewMessage, $item_name, $_db); + $message = Message::error($message); + + if ($response->isAjax()) { + $response->setRequestStatus(false); + $response->addJSON('message', $message); + + exit; + } + + $message->display(); } /** @@ -97,18 +102,22 @@ class Export * * @return void */ - public function events() + public function events(): void { global $db; - if (! empty($_GET['export_item']) && ! empty($_GET['item_name'])) { - $item_name = $_GET['item_name']; - $export_data = $this->dbi->getDefinition($db, 'EVENT', $item_name); - if (! $export_data) { - $export_data = false; - } - $this->handle($export_data, 'event'); + if (empty($_GET['export_item']) || empty($_GET['item_name'])) { + return; } + + $item_name = $_GET['item_name']; + $export_data = $this->dbi->getDefinition($db, 'EVENT', $item_name); + + if (! $export_data) { + $export_data = false; + } + + $this->handle($export_data, 'event'); } /** @@ -117,32 +126,26 @@ class Export * * @return void */ - public function routines() + public function routines(): void { global $db; - if (! empty($_GET['export_item']) - && ! empty($_GET['item_name']) - && ! empty($_GET['item_type']) - ) { - if ($_GET['item_type'] == 'FUNCTION' || $_GET['item_type'] == 'PROCEDURE') { - $rtn_definition - = $this->dbi->getDefinition( - $db, - $_GET['item_type'], - $_GET['item_name'] - ); - if ($rtn_definition === null) { - $export_data = false; - } else { - $export_data = "DELIMITER $$\n" - . $rtn_definition - . "$$\nDELIMITER ;\n"; - } - - $this->handle($export_data, 'routine'); - } + if (empty($_GET['export_item']) || empty($_GET['item_name']) || empty($_GET['item_type'])) { + return; } + + if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') { + return; + } + + $rtn_definition = $this->dbi->getDefinition($db, $_GET['item_type'], $_GET['item_name']); + $export_data = false; + + if ($rtn_definition !== null) { + $export_data = "DELIMITER $$\n" . $rtn_definition . "$$\nDELIMITER ;\n"; + } + + $this->handle($export_data, 'routine'); } /** @@ -151,21 +154,25 @@ class Export * * @return void */ - public function triggers() + public function triggers(): void { global $db, $table; - if (! empty($_GET['export_item']) && ! empty($_GET['item_name'])) { - $item_name = $_GET['item_name']; - $triggers = $this->dbi->getTriggers($db, $table, ''); - $export_data = false; - foreach ($triggers as $trigger) { - if ($trigger['name'] === $item_name) { - $export_data = $trigger['create']; - break; - } - } - $this->handle($export_data, 'trigger'); + if (empty($_GET['export_item']) || empty($_GET['item_name'])) { + return; } + + $item_name = $_GET['item_name']; + $triggers = $this->dbi->getTriggers($db, $table, ''); + $export_data = false; + + foreach ($triggers as $trigger) { + if ($trigger['name'] === $item_name) { + $export_data = $trigger['create']; + break; + } + } + + $this->handle($export_data, 'trigger'); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6ce30e75da..4e6ac8724a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3510,16 +3510,6 @@ parameters: count: 1 path: libraries/classes/Rte/Events.php - - - message: "#^Else branch is unreachable because previous condition is always true\\.$#" - count: 1 - path: libraries/classes/Rte/Export.php - - - - message: "#^Parameter \\#1 \\$export_data of method PhpMyAdmin\\\\Rte\\\\Export\\:\\:handle\\(\\) expects string, string\\|false given\\.$#" - count: 2 - path: libraries/classes/Rte/Export.php - - message: "#^Cannot access offset 'item_name' on array\\|true\\.$#" count: 1