Class: CodeKeeper::Metrics::CyclomaticComplexity
- Inherits:
-
Object
- Object
- CodeKeeper::Metrics::CyclomaticComplexity
- 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
-
#initialize(file_path) ⇒ CyclomaticComplexity
constructor
A new instance of CyclomaticComplexity.
-
#score ⇒ Object
returns score of cyclomatic complexity.
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
#score ⇒ Object
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 !(node) || node.lvasgn_type? next score if node.csend_type? && discount_for_repeated_csend?(node) next 1 + score end { "#{@path}": final_score } end |