Class: RuboCop::Cop::Grape::AbcSize

Inherits:
Base
  • Object
show all
Extended by:
ExcludeLimit
Includes:
EndpointHelper
Defined in:
lib/rubocop/cop/grape/abc_size.rb

Overview

Checks that the ABC size of endpoints is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See c2.com/cgi/wiki?AbcMetric and en.wikipedia.org/wiki/ABC_Software_Metric.

Constant Summary collapse

MSG =
'Assignment Branch Condition size is too high. [%<abc_vector>s %<complexity>d/%<max>d]'

Instance Method Summary collapse

Instance Method Details

#check_complexity(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/grape/abc_size.rb', line 24

def check_complexity(node)
  return unless node.body

  max = cop_config['Max']
  complexity, abc_vector = complexity(node.body)
  return unless complexity > max

  msg = format(self.class::MSG, abc_vector: abc_vector, complexity: complexity, max: max)
  add_offense(node, message: msg) { self.max = complexity.ceil }
end

#complexity(node) ⇒ Object



35
36
37
38
39
40
# File 'lib/rubocop/cop/grape/abc_size.rb', line 35

def complexity(node)
  Metrics::Utils::AbcSizeCalculator.calculate(
    node,
    discount_repeated_attributes: !cop_config['CountRepeatedAttributes']
  )
end

#on_block(node) ⇒ Object



18
19
20
21
22
# File 'lib/rubocop/cop/grape/abc_size.rb', line 18

def on_block(node)
  return unless http_method_node?(node)

  check_complexity(node)
end