Created
July 22, 2017 09:39
-
-
Save anna-yn/469a612649ed4b00ab5240d77dd50615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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