Sha256: e67a578347c48ed0f320744c8d79b51425ab0029b46d5fe2e4d9f48bec6ba851

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 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]'

        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| !%i[kwoptarg kwarg].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

15 entries across 15 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.93.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.93.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.92.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.91.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.91.0 lib/rubocop/cop/metrics/parameter_lists.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.90.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.89.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.89.0 lib/rubocop/cop/metrics/parameter_lists.rb