Sha256: 836e128387aa2db72a89ad8466db17f5f561d251ea76d53a61a33913de44a820

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Metrics
      # This cop checks for methods with too many parameters.
      # The maximum number of parameters is configurable.
      # Keyword arguments can optionally be excluded from the total count.
      class ParameterLists < Base
        include ConfigurableMax

        MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
              '[%<count>d/%<max>d]'

        NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
        private_constant :NAMED_KEYWORD_TYPES

        def on_args(node)
          count = args_count(node)
          return unless count > max_params

          return if argument_to_lambda_or_proc?(node)

          add_offense(node, message: format(MSG, max: max_params, count: args_count(node))) do
            self.max = count
          end
        end

        private

        def_node_matcher :argument_to_lambda_or_proc?, <<~PATTERN
          ^lambda_or_proc?
        PATTERN

        def args_count(node)
          if count_keyword_args?
            node.children.size
          else
            node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
          end
        end

        def max_params
          cop_config['Max']
        end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-1.4.2 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.4.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.4.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.3.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.3.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.2.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.1.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-1.0.0 lib/rubocop/cop/metrics/parameter_lists.rb