From d700aa06dfe785ab6dee411482f5866d554f6440 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 26 Oct 2023 21:13:46 -0700 Subject: [PATCH] Fixed SonarCloud code smells --- .idea/misc.xml | 1 - .../kotlin/net/thauvin/erik/mobibot/Utils.kt | 22 ++++++++------- .../thauvin/erik/mobibot/commands/Ignore.kt | 2 +- .../erik/mobibot/commands/tell/Tell.kt | 27 +++++++++++-------- .../erik/mobibot/entries/FeedsManager.kt | 2 +- .../erik/mobibot/modules/CurrencyConverter.kt | 27 +++++++++++-------- 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 67f5457..a59e398 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index 51adc88..38b89e3 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -42,7 +42,6 @@ import org.slf4j.Logger import java.io.* import java.net.HttpURLConnection import java.net.URL -import java.net.URLEncoder import java.nio.file.Files import java.nio.file.Paths import java.time.LocalDateTime @@ -125,14 +124,19 @@ object Utils { */ @JvmStatic fun String?.colorize(color: String): String { - return if (isNullOrEmpty()) { - "" - } else if (color == DEFAULT_COLOR) { - this - } else if (Colors.BOLD == color || Colors.REVERSE == color) { - color + this + color - } else { - color + this + Colors.NORMAL + return when { + isNullOrEmpty() -> { + "" + } + color == DEFAULT_COLOR -> { + this + } + Colors.BOLD == color || Colors.REVERSE == color -> { + color + this + color + } + else -> { + color + this + Colors.NORMAL + } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt index 88109e8..723108d 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -129,7 +129,7 @@ class Ignore : AbstractCommand() { } } - if (ignored.size > 0) { + if (ignored.isNotEmpty()) { event.sendMessage("The following nicks are ignored:") event.sendList(ignored.sorted(), 8, isIndent = true) } else { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt index 96800bb..cdeb943 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt @@ -85,18 +85,23 @@ class Tell(private val serialObject: String) : AbstractCommand() { override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) { if (isEnabled()) { - if (args.isBlank()) { - helpResponse(channel, args, event) - } else if (args.startsWith(View.VIEW_CMD)) { - if (event.isChannelOp(channel) && "${View.VIEW_CMD} $TELL_ALL_KEYWORD" == args) { - viewAll(event) - } else { - viewMessages(event) + when { + args.isBlank() -> { + helpResponse(channel, args, event) + } + args.startsWith(View.VIEW_CMD) -> { + if (event.isChannelOp(channel) && "${View.VIEW_CMD} $TELL_ALL_KEYWORD" == args) { + viewAll(event) + } else { + viewMessages(event) + } + } + args.startsWith("$TELL_DEL_KEYWORD ") -> { + deleteMessage(channel, args, event) + } + else -> { + newMessage(channel, args, event) } - } else if (args.startsWith("$TELL_DEL_KEYWORD ")) { - deleteMessage(channel, args, event) - } else { - newMessage(channel, args, event) } if (clean()) { save() diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/entries/FeedsManager.kt b/src/main/kotlin/net/thauvin/erik/mobibot/entries/FeedsManager.kt index a30ba24..2d87dbf 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/entries/FeedsManager.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/entries/FeedsManager.kt @@ -141,7 +141,7 @@ class FeedsManager private constructor() { .append("\">") .append(channel) .append("") - if (comments.size > 0) { + if (comments.isNotEmpty()) { buff.append("

") for (j in comments.indices) { if (j > 0) { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index c194571..3aad379 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -74,19 +74,24 @@ class CurrencyConverter : AbstractModule() { override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { reload(properties[API_KEY_PROP]) - if (SYMBOLS.isEmpty()) { - event.respond(EMPTY_SYMBOLS_TABLE) - } else if (args.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ (to|in) [a-zA-Z]{3}+".toRegex())) { - val msg = convertCurrency(properties[API_KEY_PROP], args) - event.respond(msg.msg) - if (msg.isError) { + when { + SYMBOLS.isEmpty() -> { + event.respond(EMPTY_SYMBOLS_TABLE) + } + args.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ (to|in) [a-zA-Z]{3}+".toRegex()) -> { + val msg = convertCurrency(properties[API_KEY_PROP], args) + event.respond(msg.msg) + if (msg.isError) { + helpResponse(event) + } + } + args.contains(CODES_KEYWORD) -> { + event.sendMessage("The supported currency codes are:") + event.sendList(SYMBOLS.keys.toList(), 11, isIndent = true) + } + else -> { helpResponse(event) } - } else if (args.contains(CODES_KEYWORD)) { - event.sendMessage("The supported currency codes are:") - event.sendList(SYMBOLS.keys.toList(), 11, isIndent = true) - } else { - helpResponse(event) } }