class Rubocop::Cop::Style::MethodLength

This cop checks if the length a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Constants

MSG

Public Instance Methods

count_comments?() click to toggle source
# File lib/rubocop/cop/style/method_length.rb, line 28
def count_comments?
  MethodLength.config['CountComments']
end
max_length() click to toggle source
# File lib/rubocop/cop/style/method_length.rb, line 24
def max_length
  MethodLength.config['Max']
end
on_def(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/method_length.rb, line 12
def on_def(node)
  check(node)

  super
end
on_defs(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/method_length.rb, line 18
def on_defs(node)
  check(node)

  super
end

Private Instance Methods

calculate_length(source) click to toggle source
# File lib/rubocop/cop/style/method_length.rb, line 43
def calculate_length(source)
  lines = source.lines.to_a[1...-1]

  return 0 unless lines

  lines.map!(&:strip).reject!(&:empty?)

  lines.reject! { |line| line =~ /^\s*#/ } unless count_comments?

  lines.size
end
check(node) click to toggle source
# File lib/rubocop/cop/style/method_length.rb, line 34
def check(node)
  method_length = calculate_length(node.loc.expression.source)

  if method_length > max_length
    message = sprintf(MSG, method_length, max_length)
    add_offence(:convention, node.loc.keyword, message)
  end
end