Class: Reek::Smells::LongYieldList

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/long_yield_list.rb

Overview

A variant on LongParameterList that checks the number of items passed to a block by a yield call.

Constant Summary collapse

MAX_ALLOWED_PARAMS_KEY =

The name of the config field that sets the maximum number of parameters permitted in any method or block.

'max_params'
DEFAULT_MAX_ALLOWED_PARAMS =
3

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smell_category, #smell_type, #smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, contexts, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.default_configObject


20
21
22
# File 'lib/reek/smells/long_yield_list.rb', line 20

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

.smell_categoryObject


16
17
18
# File 'lib/reek/smells/long_yield_list.rb', line 16

def self.smell_category
  'LongParameterList'
end

Instance Method Details

#examine_context(method_ctx) ⇒ Array<SmellWarning>

Checks the number of parameters in the given scope.

Returns:


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reek/smells/long_yield_list.rb', line 29

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|
    count = yield_node.args.length
    SmellWarning.new self,
                     context: method_ctx.full_name,
                     lines: [yield_node.line],
                     message: "yields #{count} parameters",
                     parameters: { count: count }
  end
end