Class: RuboCop::Cop::Metrics::Utils::AbcSizeCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/metrics/utils/abc_size_calculator.rb

Overview

> ABC is .. a software size metric .. computed by counting the number > of assignments, branches and conditions for a section of code. > c2.com/cgi/wiki?AbcMetric

We separate the calculator from the cop so that the calculation, the formula itself, is easier to test.

Constant Summary collapse

BRANCH_NODES =

> Branch – an explicit forward program branch out of scope – a > function call, class method call .. > c2.com/cgi/wiki?AbcMetric

%i[send csend].freeze
CONDITION_NODES =

> Condition – a logical/Boolean test, == != <= >= < > else case > default try catch ? and unary conditionals. > c2.com/cgi/wiki?AbcMetric

CyclomaticComplexity::COUNTED_NODES.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ AbcSizeCalculator

Returns a new instance of AbcSizeCalculator.



28
29
30
31
32
33
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 28

def initialize(node)
  @assignment = 0
  @branch = 0
  @condition = 0
  @node = node
end

Class Method Details

.calculate(node) ⇒ Object



24
25
26
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 24

def self.calculate(node)
  new(node).calculate
end

Instance Method Details

#calculateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 35

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

#else_branch?(node) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 65

def else_branch?(node)
  %i[case if].include?(node.type) &&
    node.else? &&
    node.loc.else.is?('else')
end

#evaluate_branch_nodes(node) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 52

def evaluate_branch_nodes(node)
  if node.comparison_method?
    @condition += 1
  else
    @branch += 1
  end
end

#evaluate_condition_node(node) ⇒ Object



60
61
62
63
# File 'lib/rubocop/cop/metrics/utils/abc_size_calculator.rb', line 60

def evaluate_condition_node(node)
  @condition += 1 if else_branch?(node)
  @condition += 1
end