Sha256: f371e97096ee7d381b12bd999bb793520b0f1712e1e489f015eefd3844214c7d

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for methods with too many parameters.
      # The maximum number of parameters in configurable.
      # On Ruby 2.0+ keyword arguments can optionally
      # be excluded from the total count.
      class ParameterLists < Cop
        include ConfigurableMax

        MSG = 'Avoid parameter lists longer than %d parameters.'

        def on_args(node)
          count = args_count(node)
          if count > max_params
            add_offense(node, :expression, format(MSG, max_params)) do
              self.max = count
            end
          end
        end

        private

        def args_count(node)
          if count_keyword_args?
            node.children.size
          else
            node.children.reject { |a| a.type == :kwoptarg }.size
          end
        end

        def max_params
          cop_config['Max']
        end

        def count_keyword_args?
          cop_config['CountKeywordArgs']
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/style/parameter_lists.rb
rubocop-0.20.1 lib/rubocop/cop/style/parameter_lists.rb
rubocop-0.20.0 lib/rubocop/cop/style/parameter_lists.rb
rubocop-0.19.1 lib/rubocop/cop/style/parameter_lists.rb
rubocop-0.19.0 lib/rubocop/cop/style/parameter_lists.rb