Sha256: 1d209e4bdfce11c68ea02663567b652208cec586be77b27e12f85c45977b8996

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')

module Reek
  module Smells

    #
    # A variant on LongParameterList that checks the number of items
    # passed to a block by a +yield+ call.
    #
    class LongYieldList < SmellDetector

      SMELL_SUBCLASS = self.name.split(/::/)[-1]
      SMELL_CLASS = 'LongParameterList'

      # 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'

      # The default value of the +MAX_ALLOWED_PARAMS_KEY+ configuration
      # value.
      DEFAULT_MAX_ALLOWED_PARAMS = 3

      PARAMETER_COUNT_KEY = 'parameter_count'

      def self.default_config
        super.adopt(
                MAX_ALLOWED_PARAMS_KEY => DEFAULT_MAX_ALLOWED_PARAMS
        )
      end

      def initialize(source, config = LongYieldList.default_config)
        super(source, config)
      end

      #
      # Checks the number of parameters in the given scope.
      #
      # @return [Array<SmellWarning>]
      #
      def examine_context(method_ctx)
        @max_allowed_params = value(MAX_ALLOWED_PARAMS_KEY, method_ctx, DEFAULT_MAX_ALLOWED_PARAMS)
        method_ctx.local_nodes(:yield).select do |yield_node|
          yield_node.args.length > @max_allowed_params
        end.map do |yield_node|
          num_params = yield_node.args.length
          SmellWarning.new(SMELL_CLASS, method_ctx.full_name, [yield_node.line],
                           "yields #{num_params} parameters",
                           @source, SMELL_SUBCLASS, {PARAMETER_COUNT_KEY => num_params})
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reek-1.3.1 lib/reek/smells/long_yield_list.rb
reek-1.3 lib/reek/smells/long_yield_list.rb
reek-1.2.13 lib/reek/smells/long_yield_list.rb
reek-1.2.12 lib/reek/smells/long_yield_list.rb
reek-1.2.11 lib/reek/smells/long_yield_list.rb
reek-1.2.10 lib/reek/smells/long_yield_list.rb
reek-1.2.9 lib/reek/smells/long_yield_list.rb
reek-1.2.8 lib/reek/smells/long_yield_list.rb