Class: Reek::SmellDetectors::LongParameterList

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/long_parameter_list.rb

Overview

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 Long-Parameter-List for details.

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 BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, contexts, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, #smell_type, smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Class Method Details

.default_configObject



22
23
24
25
26
27
28
# File 'lib/reek/smell_detectors/long_parameter_list.rb', line 22

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

Instance Method Details

#max_allowed_paramsObject (private)



47
48
49
# File 'lib/reek/smell_detectors/long_parameter_list.rb', line 47

def max_allowed_params
  value(MAX_ALLOWED_PARAMS_KEY, context)
end

#sniffArray<SmellWarning>

Checks the number of parameters in the given method.

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/reek/smell_detectors/long_parameter_list.rb', line 35

def sniff
  count = expression.arg_names.length
  return [] if count <= max_allowed_params

  [smell_warning(
    lines: [source_line],
    message: "has #{count} parameters",
    parameters: { count: count })]
end