Skip to content

Commit

Permalink
feat(method): Add support for getting Scala methods
Browse files Browse the repository at this point in the history
The count of the Scala methods was missing so I
added support for this.
Tested with unit test
  • Loading branch information
Boyan Bonev committed Aug 29, 2017
1 parent 8fa49b6 commit 9c3f0b0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/main/scala/com/sagacify/sonar/scala/Measures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import scalariform.lexer.Token
import scalariform.lexer.Tokens._

object Measures {

final def count_classes(tokens: List[Token], i: Int = 0): Int = {
def count_classes(tokens: List[Token]): Int = {
var count = 0
tokens.foreach(token => if (token.tokenType == CLASS || token.tokenType == OBJECT) count += 1)

count
}

final def count_methods(tokens: List[Token]): Int = {
var count = 0
tokens.foreach(token => if (token.tokenType == DEF) count += 1)

count
}

/* applied on raw source code */

Expand Down
26 changes: 11 additions & 15 deletions src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,18 @@ class ScalaSensor(scala: Scala, fs: FileSystem) extends Sensor {
val sourceCode = Source.fromFile(inputFile.file, charset).mkString
val tokens = Scala.tokenize(sourceCode, version)

context.saveMeasure(inputFile,
CM.COMMENT_LINES,
Measures.count_comment_lines(tokens))
context.saveMeasure(inputFile,
CM.NCLOC,
Measures.count_ncloc(tokens))

context.saveMeasure(inputFile, CM.COMMENT_LINES, Measures.count_comment_lines(tokens))
context.saveMeasure(inputFile, CM.NCLOC, Measures.count_ncloc(tokens))
context.saveMeasure(inputFile, CM.CLASSES, Measures.count_classes(tokens))
// context.saveMeasure(input, CM.FUNCTIONS, methods)
// context.saveMeasure(input, CM.ACCESSORS, accessors)
// context.saveMeasure(input, CM.COMPLEXITY_IN_FUNCTIONS, complexityInMethods)
// context.saveMeasure(input, CM.COMPLEXITY_IN_CLASSES, fileComplexity)
// context.saveMeasure(input, CM.COMPLEXITY, fileComplexity)
// context.saveMeasure(input, CM.PUBLIC_API, publicApiChecker.getPublicApi())
// context.saveMeasure(input, CM.PUBLIC_DOCUMENTED_API_DENSITY, publicApiChecker.getDocumentedPublicApiDensity())
// context.saveMeasure(input, CM.PUBLIC_UNDOCUMENTED_API, publicApiChecker.getUndocumentedPublicApi())
context.saveMeasure(inputFile, CM.FUNCTIONS, Measures.count_methods(tokens))

// context.saveMeasure(inputFile, CM.ACCESSORS, accessors)
// context.saveMeasure(inputFile, CM.COMPLEXITY_IN_FUNCTIONS, complexityInMethods)
// context.saveMeasure(inputFile, CM.COMPLEXITY_IN_CLASSES, fileComplexity)
// context.saveMeasure(inputFile, CM.COMPLEXITY, fileComplexity)
// context.saveMeasure(inputFile, CM.PUBLIC_API, publicApiChecker.getPublicApi())
// context.saveMeasure(inputFile, CM.PUBLIC_DOCUMENTED_API_DENSITY, publicApiChecker.getDocumentedPublicApiDensity())
// context.saveMeasure(inputFile, CM.PUBLIC_UNDOCUMENTED_API, publicApiChecker.getUndocumentedPublicApi())

}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/ScalaFile2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class ScalaFile2 {
}

object ScalaFile2 {
def func2 = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers {
.saveMeasure(file, CM.COMMENT_LINES, 0)
verify(sensorContext, times(1))
.saveMeasure(file, CM.CLASSES, 1)
verify(sensorContext, times(1))
.saveMeasure(file, CM.FUNCTIONS, 1)

}
}
Expand All @@ -91,6 +93,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers {
.saveMeasure(file, CM.COMMENT_LINES, 1)
verify(sensorContext, times(1))
.saveMeasure(file, CM.CLASSES, 2)
verify(sensorContext, times(1))
.saveMeasure(file, CM.FUNCTIONS, 2)
}
}
}

0 comments on commit 9c3f0b0

Please sign in to comment.