/
NamedArguments.kt
80 lines (71 loc) · 3.3 KB
/
NamedArguments.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package io.gitlab.arturbosch.detekt.rules.complexity
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.resolve.calls.util.getParameterForArgument
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
/**
* Reports function invocations which have more arguments than a certain threshold and are all not named. Calls with
* too many arguments are more difficult to understand so a named arguments help.
*
* <noncompliant>
* fun sum(a: Int, b: Int, c: Int, d: Int) {
* }
* sum(1, 2, 3, 4)
* </noncompliant>
*
* <compliant>
* fun sum(a: Int, b: Int, c: Int, d: Int) {
* }
* sum(a = 1, b = 2, c = 3, d = 4)
* </compliant>
*/
@RequiresTypeResolution
class NamedArguments(config: Config = Config.empty) : Rule(config) {
override val issue = Issue(
"NamedArguments",
Severity.Maintainability,
"Named arguments are required for function calls with many arguments.",
Debt.FIVE_MINS
)
@Configuration("number of arguments that triggers this inspection")
private val threshold: Int by config(defaultValue = 3)
@Configuration("ignores when argument values are the same as the parameter names")
private val ignoreArgumentsMatchingNames: Boolean by config(defaultValue = false)
override fun visitCallExpression(expression: KtCallExpression) {
val valueArguments = expression.valueArguments.filterNot { it is KtLambdaArgument }
if (valueArguments.size > threshold && expression.canNameArguments()) {
val message = "This function call has ${valueArguments.size} arguments. To call a function with more " +
"than $threshold arguments you should set the name of each argument."
report(CodeSmell(issue, Entity.from(expression), message))
} else {
super.visitCallExpression(expression)
}
}
@Suppress("ReturnCount")
private fun KtCallExpression.canNameArguments(): Boolean {
val resolvedCall = getResolvedCall(bindingContext) ?: return false
if (!resolvedCall.candidateDescriptor.hasStableParameterNames()) return false
val unnamedArguments = valueArguments.mapNotNull { argument ->
if (argument.isNamed() || argument is KtLambdaArgument) return@mapNotNull null
val parameter = resolvedCall.getParameterForArgument(argument) ?: return@mapNotNull null
if (ignoreArgumentsMatchingNames &&
parameter.name.asString() == argument.getArgumentExpression()?.text
) return@mapNotNull null
argument to parameter
}
if (unnamedArguments.isEmpty()) return false
return unnamedArguments.all { (argument, parameter) ->
argument.getSpreadElement() != null || parameter.varargElementType == null
}
}
}