Exemplo n.º 1
0
 def ratio_of_variable_naming_starting_with_lowercase_letters(self, filepaths: Set[str]) -> float:
     """
     This metric uses ast of files so you will need to generate files stated below with PathMiner.
     :param filepaths: paths to files for which metric should be calculated
     :return: variables starting with lowercase letters metric
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.number_of_variables_starting_with_lowercase_for_file[filepath] for filepath in filepaths]),
         sum([self.number_of_variables_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating metric ratio of variable naming starting with lowercase letters for " + str(filepaths)
     )
Exemplo n.º 2
0
 def ratio_of_if_statements_to_all_conditional_statements(self, filepaths: Set[str]) -> float:
     """
     Conditional statements are: if (including if else) and switch.
     :param filepaths: paths to files for which metric should be calculated
     :return: (number of if statements) / (number of all conditional statements) * 100
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.ifs_for_file[filepath] for filepath in filepaths]),
         sum([self.ifs_for_file[filepath] + self.switches_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating ratio of if statements to all conditional statements for " + str(filepaths)
     )
Exemplo n.º 3
0
 def ratio_of_for_statements_to_all_loop_statements(self, filepaths: Set[str]) -> float:
     """
     Loop statements are: while (including do while) and for.
     :param filepaths: paths to files for which metric should be calculated
     :return: (number of for statements) / (number of all loop statements) * 100
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.fors_for_file[filepath] for filepath in filepaths]),
         sum([self.fors_for_file[filepath] + self.whiles_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating ratio of for statements to all loop statements for " + str(filepaths)
     )
Exemplo n.º 4
0
 def preference_for_cyclic_variables(self, filepaths: Set[str]) -> float:
     """
     Strange metric. It is interpreted as ratio of variables declared in for loops
     to all variables.
     :param filepaths: paths to files for which metric should be calculated
     :return: ratio of loops variables to all variables
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.number_of_variables_in_for_control_for_file[filepath] for filepath in filepaths]),
         sum([self.number_of_variables_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating metric preference_for_cyclic_variables for " + str(filepaths)
     )
    def ratio_of_block_comments_to_all_comment_lines(self, filepaths: Set[str]) -> float:
        """
        Block comment size is number of comment lines in block comment.

        :param filepaths: paths to files for which metric should be calculated
        :return: block comment lines metric
        """
        return divide_ratio_with_handling_zero_division(
            sum([self.comment_lines_for_file[filepath] - self.single_line_comments_for_file[filepath]
                 for filepath in filepaths]),
            sum([self.comment_lines_for_file[filepath] for filepath in filepaths]),
            self.LOGGER,
            "calculating ratio of block comments to all comment lines for " + str(filepaths)
        )
    def ratio_of_close_braces_alone_in_a_line(self, filepaths: Set[str]) -> float:
        """
        Counts the following: (number of lines with alone close braces) / (number of lines that contain close braces) * 100
        If close brace is located in comment then it will be counted too.
        That is not good but this situation occurs rare.

        :param filepaths: paths to files for which metric should be calculated
        :return: close braces alone in a line metric
        """
        return divide_ratio_with_handling_zero_division(
            sum([self.alone_close_braces_for_file[filepath] for filepath in filepaths]),
            sum([self.close_braces_for_file[filepath] for filepath in filepaths]),
            self.LOGGER,
            "calculating ratio of close braces alone in a line for " + str(filepaths)
        )
Exemplo n.º 7
0
 def ratio_of_catch_statements_when_dealing_with_exceptions(self, filepaths: Set[str]) -> float:
     """
     TODO: Consider if account only one catch per try even if it has several catches.
     TODO: Find out why was below zero? write tests.
     Catches are accounted with tries, that is why we need to subtract catches from tries.
     :param filepaths: paths to files for which metric should be calculated
     :return: (number of catches statements) / (number of tries statements) * 100
     """
     catches = sum([self.catches_for_file[filepath] for filepath in filepaths])
     tries = max(sum([self.tries_for_file[filepath] for filepath in filepaths]), catches)
     return divide_ratio_with_handling_zero_division(
         catches,
         tries,
         self.LOGGER,
         "calculating ratio of if statements to all conditional statements for " + str(filepaths)
     )
Exemplo n.º 8
0
 def ratio_of_try_structure(self, filepaths: Set[str]) -> float:
     """
     Strange metric. Ratio to what?
     Possible variants of interpretation:
     1. number of tries / number of characters
     2. number of tries
     1 variant was chosen.
     :param filepaths: paths to files for which metric should be calculated
     :return: number of tries / number of characters
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.tries_for_file[filepath] for filepath in filepaths]),
         sum([self.character_number_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating ratio of ratio_of_try_structure for " + str(filepaths)
     )
Exemplo n.º 9
0
 def ratio_of_branch_statements(self, filepaths: Set[str]) -> float:
     """
     Strange metric. Branch statements are: break, continue, return
     Possible variants of interpretation:
     1. number of branch / all statements
     2. number of break / (number of break + number of continue)
     3. (number of break + number of continue) / (number of break, continue, return)
     4. number of break + number of continue + number of return
     5. number of branch / number of characters
     6. (number of break + number of continue) / number of characters
     6 variant was chosen.
     :param filepaths: paths to files for which metric should be calculated
     :return: (number of break + number of continue) / number of characters
     """
     return divide_ratio_with_handling_zero_division(
         sum([self.breaks_for_file[filepath] + self.continues_for_file[filepath] for filepath in filepaths]),
         sum([self.character_number_for_file[filepath] for filepath in filepaths]),
         self.LOGGER,
         "calculating ratio of ratio_of_branch_statements for " + str(filepaths)
     )