From 17432e0e0a556a5a9da574622880bdfda183e83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Fri, 8 Dec 2023 11:57:48 +0100 Subject: [PATCH] chore: Code refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Util/ArrayUtil.php | 92 --------------------------------------- lib/SP/Util/ErrorUtil.php | 70 +++++++++++++++-------------- 2 files changed, 34 insertions(+), 128 deletions(-) delete mode 100644 lib/SP/Util/ArrayUtil.php diff --git a/lib/SP/Util/ArrayUtil.php b/lib/SP/Util/ArrayUtil.php deleted file mode 100644 index da05a85e1..000000000 --- a/lib/SP/Util/ArrayUtil.php +++ /dev/null @@ -1,92 +0,0 @@ -. - */ - -namespace SP\Util; - -/** - * Class ArrayUtil - * - * @package SP\Util - */ -final class ArrayUtil -{ - /** - * Buscar un objeto en un array según el valor de una propiedad - * - * @param array $array - * @param string $property Nombre de la propiedad - * @param string $value Valor de la propiedad - * @param null $default Valor por defecto - * - * @return false|object - */ - public static function searchInObject(array $array, string $property, string $value, $default = null) - { - foreach ($array as $object) { - if (is_object($object) && isset($object->$property) && $value == $object->$property) { - return $object; - } - } - - return $default ?? false; - } - - /** - * Comprobar si un valor existe en un array de objetos - * - * @param array $objectArray - * @param string $method - * @param mixed $value - * - * @return bool - */ - public static function checkInObjectArrayMethod(array $objectArray, string $method, $value): bool - { - foreach ($objectArray as $object) { - if (is_callable([$object, $method]) && $object->$method() === $value) { - return true; - } - } - - return false; - } - - /** - * Comprobar si un valor existe en un array de objetos - */ - public static function checkInObjectArray(array $objectArray, string $property, $value): bool - { - if (count($objectArray) === 0) { - return false; - } - - foreach ($objectArray as $object) { - if (is_object($object) && isset($object->$property) && $object->$property == $value) { - return true; - } - } - - return false; - } -} \ No newline at end of file diff --git a/lib/SP/Util/ErrorUtil.php b/lib/SP/Util/ErrorUtil.php index 7319aab44..dbc0c3cae 100644 --- a/lib/SP/Util/ErrorUtil.php +++ b/lib/SP/Util/ErrorUtil.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -55,16 +55,16 @@ final class ErrorUtil /** * Establecer la plantilla de error con el código indicado. * - * @param TemplateInterface $view - * @param Exception $e - * @param string|null $replace Template replacement - * @param bool $render + * @param TemplateInterface $view + * @param Exception $e + * @param string|null $replace Template replacement + * @param bool $render */ public static function showExceptionInView( TemplateInterface $view, Exception $e, - ?string $replace = null, - bool $render = true + ?string $replace = null, + bool $render = true ): void { switch (get_class($e)) { case UpdatedMasterPassException::class: @@ -84,15 +84,15 @@ public static function showExceptionInView( /** * Establecer la plantilla de error con el código indicado. * - * @param TemplateInterface $view - * @param int $type int con el tipo de error - * @param bool $render - * @param string|null $replace + * @param TemplateInterface $view + * @param int $type int con el tipo de error + * @param bool $render + * @param string|null $replace */ public static function showErrorInView( TemplateInterface $view, - int $type, - bool $render = true, + int $type, + bool $render = true, ?string $replace = null ): void { self::addErrorTemplate($view, $replace); @@ -102,9 +102,9 @@ public static function showErrorInView( $view->append( 'errors', [ - 'type' => SPException::WARNING, + 'type' => SPException::WARNING, 'description' => $error['txt'], - 'hint' => $error['hint'], + 'hint' => $error['hint'], ] ); @@ -130,14 +130,12 @@ private static function addErrorTemplate(TemplateInterface $view, string $replac } else { $view->addTemplate('error', Template::PARTIALS_DIR); } + } elseif ($view->hasContentTemplates()) { + $view->removeContentTemplate($replace); + $view->addContentTemplate('error', Template::PARTIALS_DIR); } else { - if ($view->hasContentTemplates()) { - $view->removeContentTemplate($replace); - $view->addContentTemplate('error', Template::PARTIALS_DIR); - } else { - $view->removeTemplate($replace); - $view->addTemplate('error', Template::PARTIALS_DIR); - } + $view->removeTemplate($replace); + $view->addTemplate('error', Template::PARTIALS_DIR); } } @@ -147,35 +145,35 @@ private static function addErrorTemplate(TemplateInterface $view, string $replac protected static function getErrorTypes(int $type): array { $errorTypes = [ - self::ERR_UNAVAILABLE => [ - 'txt' => __('Option unavailable'), + self::ERR_UNAVAILABLE => [ + 'txt' => __('Option unavailable'), 'hint' => __('Please contact to the administrator'), ], - self::ERR_ACCOUNT_NO_PERMISSION => [ - 'txt' => __('You don\'t have permission to access this account'), + self::ERR_ACCOUNT_NO_PERMISSION => [ + 'txt' => __('You don\'t have permission to access this account'), 'hint' => __('Please contact to the administrator'), ], - self::ERR_PAGE_NO_PERMISSION => [ - 'txt' => __('You don\'t have permission to access this page'), + self::ERR_PAGE_NO_PERMISSION => [ + 'txt' => __('You don\'t have permission to access this page'), 'hint' => __('Please contact to the administrator'), ], self::ERR_OPERATION_NO_PERMISSION => [ - 'txt' => __('You don\'t have permission to do this operation'), + 'txt' => __('You don\'t have permission to do this operation'), 'hint' => __('Please contact to the administrator'), ], - self::ERR_UPDATE_MPASS => [ - 'txt' => __('Master password updated'), + self::ERR_UPDATE_MPASS => [ + 'txt' => __('Master password updated'), 'hint' => __('Please, restart the session for update it'), ], - self::ERR_EXCEPTION => [ - 'txt' => __('An exception occured'), + self::ERR_EXCEPTION => [ + 'txt' => __('An exception occured'), 'hint' => __('Please contact to the administrator'), ], ]; return $errorTypes[$type] ?? [ - 'txt' => __('An exception occured'), - 'hint' => __('Please contact to the administrator'), - ]; + 'txt' => __('An exception occured'), + 'hint' => __('Please contact to the administrator'), + ]; } }