Exemplo n.º 1
0
    def _get_complexity(self, ast: AST, each_block: int,
                        nested_level: int) -> int:
        each_block_name = self._get_node_name(ast, each_block)
        each_block_type = ast.get_type(each_block)
        complexity = 0

        if each_block_type == ASTNodeType.METHOD_DECLARATION and each_block_name != self.__method_name:
            complexity += self._nested_methods(ast, each_block, nested_level)

        elif each_block_type == ASTNodeType.IF_STATEMENT:
            complexity += self._check_if_statement(ast, each_block,
                                                   nested_level)

        elif each_block_type in increment_and_nested_for:
            complexity += 1 + nested_level
            complexity += self._traverse_childs(ast, each_block,
                                                nested_level + 1)

        elif each_block_type in only_increment_for:
            complexity += self._process_not_nested_structure(
                ast, each_block, nested_level)

        else:
            complexity += self._traverse_childs(ast, each_block, nested_level)
        return complexity
Exemplo n.º 2
0
    def _create_logical_operators_sequence(self, ast: AST, binary_operation_node: int) -> List[str]:
        if ast.get_type(binary_operation_node) != ASTNodeType.BINARY_OPERATION:
            return []

        operator, left_side_node, right_side_node = ast.get_binary_operation_params(binary_operation_node)
        if operator not in logical_operators:
            return []

        left_sequence = self._create_logical_operators_sequence(ast, left_side_node)
        right_sequence = self._create_logical_operators_sequence(ast, right_side_node)
        return left_sequence + [operator] + right_sequence
Exemplo n.º 3
0
    def _process_not_nested_structure(self, ast: AST, each_block: int, nested_level: int) -> int:
        complexity = 0
        each_block_type = ast.get_type(each_block)
        if each_block_type == ASTNodeType.BINARY_OPERATION:
            bin_operator = ast.get_binary_operation_name(each_block)
            if bin_operator in logical_operators:
                complexity += self._increment_logical_operators(ast, each_block)

        elif each_block_type == ASTNodeType.METHOD_INVOCATION:
            is_recursion = self._is_recursion_call(ast, each_block)
            complexity += is_recursion

        else:
            complexity += 1
            complexity += self._traverse_childs(ast, each_block, nested_level)

        return complexity