Sha256: cc82173d9f55c9652ff20e5fc87b8668a5f6f3997801c68916d28aed30d933e3

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks if the length a method exceeds some maximum value.
      # Comment lines can optionally be ignored.
      # The maximum allowed length is configurable.
      class MethodLength < Cop
        MSG = 'Method has too many lines. [%d/%d]'

        def on_def(node)
          check(node)
        end

        def on_defs(node)
          check(node)
        end

        def max_length
          MethodLength.config['Max']
        end

        def count_comments?
          MethodLength.config['CountComments']
        end

        private

        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

        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/method_length.rb
rubocop-0.11.1 lib/rubocop/cop/style/method_length.rb
rubocop-0.11.0 lib/rubocop/cop/style/method_length.rb
rubocop-0.10.0 lib/rubocop/cop/style/method_length.rb