class RuboCop::Cop::Metrics::Utils::AbcSizeCalculator
the formula itself, is easier to test.
We separate the calculator from the cop so that the calculation,
> c2.com/cgi/wiki?AbcMetric<br>> of assignments, branches and conditions for a section of code.
> ABC is .. a software size metric .. computed by counting the number
def self.calculate(node)
def self.calculate(node) new(node).calculate end
def branch?(node)
def branch?(node) BRANCH_NODES.include?(node.type) end
def calculate
def calculate @node.each_node do |child| if child.assignment? @assignment += 1 elsif branch?(child) evaluate_branch_nodes(child) elsif condition?(child) evaluate_condition_node(child) end end [ Math.sqrt(@assignment**2 + @branch**2 + @condition**2).round(2), "<#{@assignment}, #{@branch}, #{@condition}>" ] end
def condition?(node)
def condition?(node) CONDITION_NODES.include?(node.type) end
def else_branch?(node)
def else_branch?(node) %i[case if].include?(node.type) && node.else? && node.loc.else.is?('else') end
def evaluate_branch_nodes(node)
def evaluate_branch_nodes(node) if node.comparison_method? @condition += 1 else @branch += 1 end end
def evaluate_condition_node(node)
def evaluate_condition_node(node) @condition += 1 if else_branch?(node) @condition += 1 end
def initialize(node)
def initialize(node) @assignment = 0 @branch = 0 @condition = 0 @node = node end