Sha256: 96b206be56ecd798f2a4c74e44e40899b7807aca18d22c7368f8d48e9b5b9d34

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require_relative 'smell_detector'
require_relative 'smell_warning'

module Reek
  module Smells
    #
    # A variant on LongParameterList that checks the number of items
    # passed to a block by a +yield+ call.
    #
    # See {file:docs/Long-Yield-List.md} for details.
    class LongYieldList < 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'.freeze
      DEFAULT_MAX_ALLOWED_PARAMS = 3

      def self.smell_category
        'LongParameterList'
      end

      def self.default_config
        super.merge MAX_ALLOWED_PARAMS_KEY => DEFAULT_MAX_ALLOWED_PARAMS
      end

      #
      # Checks the number of parameters in the given scope.
      #
      # @return [Array<SmellWarning>]
      #
      # :reek:FeatureEnvy
      # :reek:DuplicateMethodCall: { max_calls: 2 }
      def inspect(ctx)
        max_allowed_params = value(MAX_ALLOWED_PARAMS_KEY,
                                   ctx,
                                   DEFAULT_MAX_ALLOWED_PARAMS)
        ctx.local_nodes(:yield).select do |yield_node|
          yield_node.args.length > max_allowed_params
        end.map do |yield_node|
          count = yield_node.args.length
          smell_warning(
            context: ctx,
            lines: [yield_node.line],
            message: "yields #{count} parameters",
            parameters: { count: count })
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reek-3.11 lib/reek/smells/long_yield_list.rb
reek-3.10.2 lib/reek/smells/long_yield_list.rb