Sha256: 07c7c3a79f87a5bb75d891c5c90f70a74d25c12d6c2ed3036fbff42c6eea1c41

Contents?: true

Size: 1.49 KB

Versions: 11

Compression:

Stored size: 1.49 KB

Contents

require_relative 'smell_configuration'
require_relative 'smell_detector'
require_relative 'smell_warning'

module Reek
  module Smells
    #
    # A Long Parameter List occurs when a method has more than one
    # or two parameters, or when a method yields more than one or
    # two objects to an associated block.
    #
    # Currently +LongParameterList+ reports any method or block with too
    # many parameters.
    #
    # See {file:docs/Long-Parameter-List.md} for details.
    class LongParameterList < SmellDetector
      # The name of the config field that sets the maximum number of
      # parameters permitted in any method or block.
      MAX_ALLOWED_PARAMS_KEY = 'max_params'
      DEFAULT_MAX_ALLOWED_PARAMS = 3

      def self.default_config
        super.merge(
          MAX_ALLOWED_PARAMS_KEY => DEFAULT_MAX_ALLOWED_PARAMS,
          SmellConfiguration::OVERRIDES_KEY => {
            'initialize' => { MAX_ALLOWED_PARAMS_KEY => 5 }
          }
        )
      end

      #
      # Checks the number of parameters in the given method.
      #
      # @return [Array<SmellWarning>]
      #
      def inspect(ctx)
        max_allowed_params = value(MAX_ALLOWED_PARAMS_KEY, ctx, DEFAULT_MAX_ALLOWED_PARAMS)
        exp = ctx.exp
        count = exp.arg_names.length
        return [] if count <= max_allowed_params
        [smell_warning(
          context: ctx,
          lines: [exp.line],
          message: "has #{count} parameters",
          parameters: { count: count })]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
reek-3.10.1 lib/reek/smells/long_parameter_list.rb
reek-3.10.0 lib/reek/smells/long_parameter_list.rb
reek-3.9.1 lib/reek/smells/long_parameter_list.rb
reek-3.9.0 lib/reek/smells/long_parameter_list.rb
reek-3.8.3 lib/reek/smells/long_parameter_list.rb
reek-3.8.2 lib/reek/smells/long_parameter_list.rb
reek-3.8.1 lib/reek/smells/long_parameter_list.rb
reek-3.8.0 lib/reek/smells/long_parameter_list.rb
reek-3.7.1 lib/reek/smells/long_parameter_list.rb
reek-3.7.0 lib/reek/smells/long_parameter_list.rb
reek-3.6.1 lib/reek/smells/long_parameter_list.rb