@Test public void file_complexity_is_sum_of_functions() throws Exception { File file = new File("src/test/resources/metrics/file_cognitive_complexity.php"); ActionParser<Tree> p = PHPParserBuilder.createParser(PHPLexicalGrammar.COMPILATION_UNIT); CompilationUnitTree cut = (CompilationUnitTree) p.parse(file); int complexity = CognitiveComplexityVisitor.complexity(cut); assertThat(complexity).isEqualTo( 1 + 3 + 2 + 1 + 1 + 1 + 4 ); }
public static CognitiveComplexity complexity(FunctionTree functionTree) { CognitiveComplexityVisitor cognitiveComplexityVisitor = new CognitiveComplexityVisitor(); cognitiveComplexityVisitor.scan(functionTree); return cognitiveComplexityVisitor.complexity; }
CognitiveComplexityVisitor extends PHPVisitorCheck { public static CognitiveComplexity complexity(FunctionTree functionTree) { CognitiveComplexityVisitor cognitiveComplexityVisitor = new CognitiveComplexityVisitor(); cognitiveComplexityVisitor.scan(functionTree); return cognitiveComplexityVisitor.complexity; } }
CognitiveComplexityVisitor extends PHPVisitorCheck { public static CognitiveComplexity complexity(FunctionTree functionTree) { CognitiveComplexityVisitor cognitiveComplexityVisitor = new CognitiveComplexityVisitor(); cognitiveComplexityVisitor.scan(functionTree); return cognitiveComplexityVisitor.complexity; }  }
CognitiveComplexityVisitor extends PHPVisitorCheck { public static CognitiveComplexity complexity(FunctionTree functionTree) { CognitiveComplexityVisitor cognitiveComplexityVisitor = new CognitiveComplexityVisitor(); cognitiveComplexityVisitor.scan(functionTree); return cognitiveComplexityVisitor.complexity; }  static CognitiveComplexity complexity(FunctionTree functionTree); static int complexity(CompilationUnitTree cut); @Override void visitIfStatement(IfStatementTree tree); @Override void visitElseifClause(ElseifClauseTree tree); @Override void visitElseClause(ElseClauseTree tree); @Override void visitSwitchStatement(SwitchStatementTree tree); @Override void visitWhileStatement(WhileStatementTree tree); @Override void visitDoWhileStatement(DoWhileStatementTree tree); @Override void visitForStatement(ForStatementTree tree); @Override void visitForEachStatement(ForEachStatementTree tree); @Override void visitCatchBlock(CatchBlockTree tree); @Override void visitFunctionDeclaration(FunctionDeclarationTree tree); @Override void visitFunctionExpression(FunctionExpressionTree tree); @Override void visitArrowFunctionExpression(ArrowFunctionExpressionTree tree); @Override void visitMethodDeclaration(MethodDeclarationTree tree); @Override void visitConditionalExpression(ConditionalExpressionTree tree); @Override void visitBreakStatement(BreakStatementTree tree); @Override void visitContinueStatement(ContinueStatementTree tree); @Override void visitGotoStatement(GotoStatementTree tree); @Override void visitBinaryExpression(BinaryExpressionTree tree); }
CognitiveComplexityVisitor extends PHPVisitorCheck { public static CognitiveComplexity complexity(FunctionTree functionTree) { CognitiveComplexityVisitor cognitiveComplexityVisitor = new CognitiveComplexityVisitor(); cognitiveComplexityVisitor.scan(functionTree); return cognitiveComplexityVisitor.complexity; }  static CognitiveComplexity complexity(FunctionTree functionTree); static int complexity(CompilationUnitTree cut); @Override void visitIfStatement(IfStatementTree tree); @Override void visitElseifClause(ElseifClauseTree tree); @Override void visitElseClause(ElseClauseTree tree); @Override void visitSwitchStatement(SwitchStatementTree tree); @Override void visitWhileStatement(WhileStatementTree tree); @Override void visitDoWhileStatement(DoWhileStatementTree tree); @Override void visitForStatement(ForStatementTree tree); @Override void visitForEachStatement(ForEachStatementTree tree); @Override void visitCatchBlock(CatchBlockTree tree); @Override void visitFunctionDeclaration(FunctionDeclarationTree tree); @Override void visitFunctionExpression(FunctionExpressionTree tree); @Override void visitArrowFunctionExpression(ArrowFunctionExpressionTree tree); @Override void visitMethodDeclaration(MethodDeclarationTree tree); @Override void visitConditionalExpression(ConditionalExpressionTree tree); @Override void visitBreakStatement(BreakStatementTree tree); @Override void visitContinueStatement(ContinueStatementTree tree); @Override void visitGotoStatement(GotoStatementTree tree); @Override void visitBinaryExpression(BinaryExpressionTree tree);  }