@Override public void visitNode(Tree tree) { if (tree.is(Kind.COMPILATION_UNIT)) { fileMeasures.setFileComplexity(ComplexityVisitor.complexity(tree)); } else if (tree.is(CLASS_NODES)) { fileMeasures.addClassComplexity(ComplexityVisitor.complexity(tree)); } else if (tree.is(FUNCTION_NODES)) { fileMeasures.addFunctionComplexity(ComplexityVisitor.complexity(tree)); } }
private void setLineAndCommentMeasures() { LineVisitor lineVisitor = new LineVisitor(context().tree()); CommentLineVisitor commentVisitor = new CommentLineVisitor(context().tree()); int linesNumber = lineVisitor.getLinesNumber(); fileMeasures.setLinesNumber(linesNumber); fileMeasures.setLinesOfCodeNumber(lineVisitor.getLinesOfCodeNumber()); fileMeasures.setCommentLinesNumber(commentVisitor.commentLineNumber()); fileMeasures.setNoSonarLines(commentVisitor.noSonarLines()); Set<Integer> linesOfCode = lineVisitor.getLinesOfCode(); Set<Integer> commentLines = commentVisitor.commentLines(); for (int line = 1; line <= linesNumber; line++) { fileLinesContext.setIntValue( CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0); fileLinesContext.setIntValue( CoreMetrics.COMMENT_LINES_DATA_KEY, line, commentLines.contains(line) ? 1 : 0); } fileLinesContext.save(); }
@Test public void test() { String filename = "metrics/lines_of_code.php"; File file = new File(filename); Map<File, Integer> numberOfLinesOfCode = new HashMap<>(); FileLinesContext fileLinesContext = mock(FileLinesContext.class); FileMeasures fileMeasures = new MetricsVisitor() .getFileMeasures(file, parse(filename), fileLinesContext, numberOfLinesOfCode, true); assertThat(fileMeasures.getFileComplexity()).isEqualTo(1); assertThat(fileMeasures.getClassComplexity()).isEqualTo(1); assertThat(fileMeasures.getFunctionComplexity()).isEqualTo(1); assertThat(fileMeasures.getFileComplexityDistribution().build()) .isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0"); assertThat(fileMeasures.getFunctionComplexityDistribution().build()) .isEqualTo("1=1;2=0;4=0;6=0;8=0;10=0;12=0"); assertThat(fileMeasures.getFunctionNumber()).isEqualTo(1); assertThat(fileMeasures.getStatementNumber()).isEqualTo(2); assertThat(fileMeasures.getClassNumber()).isEqualTo(1); assertThat(fileMeasures.getLinesNumber()).isEqualTo(29); assertThat(fileMeasures.getLinesOfCodeNumber()).isEqualTo(7); assertThat(fileMeasures.getNoSonarLines()).containsOnly(18); assertThat(fileMeasures.getCommentLinesNumber()).isEqualTo(5); assertThat(numberOfLinesOfCode.values().iterator().next()) .as("number of lines of code in the file") .isEqualTo(7); verify(fileLinesContext).setIntValue(CoreMetrics.EXECUTABLE_LINES_DATA_KEY, 1, 0); verify(fileLinesContext).setIntValue(CoreMetrics.EXECUTABLE_LINES_DATA_KEY, 13, 1); }
private void setCounterMeasures() { CounterVisitor counter = new CounterVisitor(context().tree()); fileMeasures.setClassNumber(counter.getClassNumber()); fileMeasures.setFunctionNumber(counter.getFunctionNumber()); fileMeasures.setStatementNumber(counter.getStatementNumber()); }