Class: Rubocop::Cop::Style::CyclomaticComplexity

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/cyclomatic_complexity.rb

Overview

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Constant Summary collapse

MSG =
'Cyclomatic complexity for %s is too high. [%d/%d]'
DECISION_POINT_NODES =
[:if, :while, :until, :for, :rescue, :when,
:and, :or]

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_def(node) ⇒ Object



21
22
23
24
# File 'lib/rubocop/cop/style/cyclomatic_complexity.rb', line 21

def on_def(node)
  method_name, _args, _body = *node
  check(node, method_name)
end

#on_defs(node) ⇒ Object



26
27
28
29
# File 'lib/rubocop/cop/style/cyclomatic_complexity.rb', line 26

def on_defs(node)
  _scope, method_name, _args, _body = *node
  check(node, method_name)
end