Sha256: 73dcb3d890a81dcd46ba49ad0f6aad650693e8d8b9dcc9c8129c2da034894219

Contents?: true

Size: 985 Bytes

Versions: 4

Compression:

Stored size: 985 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class MethodLength < Cop
      MSG = 'Method has too many lines. [%d/%d]'

      def on_def(node)
        check(node)

        super
      end

      def on_defs(node)
        check(node)

        super
      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.line, 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/method_length.rb
rubocop-0.8.2 lib/rubocop/cop/method_length.rb
rubocop-0.8.1 lib/rubocop/cop/method_length.rb
rubocop-0.8.0 lib/rubocop/cop/method_length.rb