Skip to content

Instantly share code, notes, and snippets.

@anna-yn
Created July 22, 2017 09:39
Show Gist options
  • Select an option

  • Save anna-yn/469a612649ed4b00ab5240d77dd50615 to your computer and use it in GitHub Desktop.

Select an option

Save anna-yn/469a612649ed4b00ab5240d77dd50615 to your computer and use it in GitHub Desktop.
/**
* @author Artur Bosch
* https://github.com/arturbosch/detekt/blob/master/detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NestedBlockDepth.kt
*/
class NestedBlockDepth(config: Config = Config.empty, threshold: Int = 3) : ThresholdRule(config, threshold) {
// ...
override fun visitNamedFunction(function: KtNamedFunction) {
val visitor = FunctionDepthVisitor(threshold)
visitor.visitNamedFunction(function)
if (visitor.isTooDeep)
report(ThresholdedCodeSmell(issue, Entity.from(function), Metric("SIZE", visitor.maxDepth, threshold)))
}
private class FunctionDepthVisitor(val threshold: Int) : DetektVisitor() {
internal var depth = 0
internal var maxDepth = 0
internal var isTooDeep = false
private fun inc() {
depth++
if (depth > threshold) {
isTooDeep = true
if (depth > maxDepth) maxDepth = depth
}
}
private fun dec() {
depth--
}
override fun visitLoopExpression(loopExpression: KtLoopExpression) {
inc()
super.visitLoopExpression(loopExpression)
dec()
}
// visit other blocks
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment