|
| 1 | +package com.github.ivy.explicit.rule |
| 2 | + |
| 3 | +import io.gitlab.arturbosch.detekt.api.* |
| 4 | +import io.gitlab.arturbosch.detekt.rules.isOverride |
| 5 | +import org.jetbrains.kotlin.psi.KtClass |
| 6 | +import org.jetbrains.kotlin.psi.KtParameter |
| 7 | + |
| 8 | +class DataClassTypedIDsRule(config: Config) : Rule(config) { |
| 9 | + companion object { |
| 10 | + private val ExcludedClassNameEndings by lazy { |
| 11 | + setOf("Dto", "Entity") |
| 12 | + } |
| 13 | + |
| 14 | + private val ExcludedAnnotations by lazy { |
| 15 | + setOf("Entity", "Serializable") |
| 16 | + } |
| 17 | + |
| 18 | + private val IdFieldEndings by lazy { |
| 19 | + setOf("Id", "ID") |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + override val issue = Issue( |
| 24 | + id = "DataClassTypedIDs", |
| 25 | + severity = Severity.Maintainability, |
| 26 | + description = "Domain data models should use type-safe `value class` ids. " + |
| 27 | + "Typed-IDs provide compile-time safety and prevent mixing IDs of different entities.", |
| 28 | + debt = Debt.TWENTY_MINS |
| 29 | + ) |
| 30 | + |
| 31 | + override fun visitClass(klass: KtClass) { |
| 32 | + super.visitClass(klass) |
| 33 | + if (klass.isData() && !klass.isIgnoredClass()) { |
| 34 | + klass.getPrimaryConstructorParameterList() |
| 35 | + ?.parameters |
| 36 | + ?.filter { param -> |
| 37 | + !param.isOverride() && param.seemsLikeID() |
| 38 | + } |
| 39 | + ?.forEach { parameter -> |
| 40 | + report( |
| 41 | + CodeSmell( |
| 42 | + issue, |
| 43 | + Entity.from(parameter), |
| 44 | + message = failureMessage(klass, parameter) |
| 45 | + ) |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private fun KtClass.isIgnoredClass(): Boolean { |
| 52 | + name?.let { klasName -> |
| 53 | + val isIgnored = ExcludedClassNameEndings.any { |
| 54 | + klasName.endsWith(it, ignoreCase = true) |
| 55 | + } |
| 56 | + if (isIgnored) return true |
| 57 | + } |
| 58 | + |
| 59 | + return annotationEntries.any { |
| 60 | + val annotationName = it.shortName?.asString() |
| 61 | + annotationName in ExcludedAnnotations |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private fun KtParameter.seemsLikeID(): Boolean { |
| 66 | + val paramType = typeReference?.text |
| 67 | + if (paramType == "UUID") return true |
| 68 | + |
| 69 | + name?.let { paramName -> |
| 70 | + val endsLikeID = IdFieldEndings.any { |
| 71 | + paramName.endsWith(it, ignoreCase = false) |
| 72 | + } |
| 73 | + if (endsLikeID) return true |
| 74 | + } |
| 75 | + |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + private fun failureMessage(klass: KtClass, parameter: KtParameter) = buildString { |
| 80 | + val paramType = parameter.typeReference?.text |
| 81 | + append("Data class '${klass.name}' should use type-safe IDs ") |
| 82 | + append("instead of $paramType for property '${parameter.name}'. ") |
| 83 | + append("Typed-IDs like `value class SomeId(val id: UUID)` provide ") |
| 84 | + append("compile-time safety and prevent mixing IDs of different entities.") |
| 85 | + } |
| 86 | +} |
0 commit comments