Class: CodeKeeper::Metrics::AbcMetric

Inherits:
Object
  • Object
show all
Includes:
RuboCop::Cop::Metrics::Utils::IteratingBlock, RuboCop::Cop::Metrics::Utils::RepeatedCsendDiscount
Defined in:
lib/code_keeper/metrics/abc_metric.rb

Overview

Caluculate cyclomatic complexity

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ AbcMetric

Returns a new instance of AbcMetric.



10
11
12
13
14
15
16
17
# File 'lib/code_keeper/metrics/abc_metric.rb', line 10

def initialize(file_path)
  ps = Parser.parse(file_path)
  @path = file_path
  @body = ps.ast
  @assignments = 0
  @branches = 0
  @conditionals = 0
end

Instance Method Details

#scoreObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/code_keeper/metrics/abc_metric.rb', line 19

def score
  caluculator = ::RuboCop::Cop::Metrics::Utils::AbcSizeCalculator.new(@body)
  caluculator.calculate
  @assignments = caluculator.instance_variable_get('@assignment')
  @conditionals = caluculator.instance_variable_get('@condition')
  @branches = caluculator.instance_variable_get('@branch')

  value = Math.sqrt(@assignments**2 + @branches**2 + @conditionals**2).round(4)
  { "#{@path}": value }
end