Class: CodeKeeper::Metrics::CyclomaticComplexity

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

Overview

Caluculate cyclomatic complexity

Constant Summary collapse

CONSIDERED_NODES =
%i[if while until for csend block block_pass rescue when and or or_asgnand_asgn].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ CyclomaticComplexity

Returns a new instance of CyclomaticComplexity.



12
13
14
15
16
# File 'lib/code_keeper/metrics/cyclomatic_complexity.rb', line 12

def initialize(file_path)
  @path = file_path
  ps = Parser.parse(@path)
  @body = ps.ast
end

Instance Method Details

#scoreObject

returns score of cyclomatic complexity



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

def score
  final_score = @body.each_node(:lvasgn, *CONSIDERED_NODES).reduce(1) do |score, node|
    next score if !iterating_block?(node) || node.lvasgn_type?
    next score if node.csend_type? && discount_for_repeated_csend?(node)

    next 1 + score
  end
  { "#{@path}": final_score }
end