Class: Reek::Smells::SmellDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/smells/smell_detector.rb

Overview

Shared responsibilities of all smell detectors.

Constant Summary collapse

EXCLUDE_KEY =

The name of the config field that lists the names of code contexts that should not be checked. Add this field to the config for each smell that should ignore this code element.

'exclude'
DEFAULT_EXCLUDE_SET =

The default value for the EXCLUDE_KEY if it isn't specified in any configuration file.

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, config = self.class.default_config) ⇒ SmellDetector

Returns a new instance of SmellDetector


65
66
67
68
69
# File 'lib/reek/smells/smell_detector.rb', line 65

def initialize(source, config = self.class.default_config)
  @source = source
  @config = Core::SmellConfiguration.new(config)
  @smells_found = []
end

Instance Attribute Details

#smell_categoryObject (readonly)

Returns the value of attribute smell_category


17
18
19
# File 'lib/reek/smells/smell_detector.rb', line 17

def smell_category
  @smell_category
end

#smell_typeObject (readonly)

Returns the value of attribute smell_type


17
18
19
# File 'lib/reek/smells/smell_detector.rb', line 17

def smell_type
  @smell_type
end

#smells_foundObject (readonly)

SMELL: only published for tests


63
64
65
# File 'lib/reek/smells/smell_detector.rb', line 63

def smells_found
  @smells_found
end

#sourceObject (readonly)

Returns the value of attribute source


17
18
19
# File 'lib/reek/smells/smell_detector.rb', line 17

def source
  @source
end

Class Method Details

.contextsObject


29
30
31
# File 'lib/reek/smells/smell_detector.rb', line 29

def contexts
  [:def, :defs]
end

.default_configObject


33
34
35
36
37
38
# File 'lib/reek/smells/smell_detector.rb', line 33

def default_config
  {
    Core::SmellConfiguration::ENABLED_KEY => true,
    EXCLUDE_KEY => DEFAULT_EXCLUDE_SET.dup
  }
end

.default_smell_categoryObject


58
59
60
# File 'lib/reek/smells/smell_detector.rb', line 58

def default_smell_category
  name.split(/::/)[-1]
end

.smell_categoryObject


50
51
52
# File 'lib/reek/smells/smell_detector.rb', line 50

def smell_category
  @smell_category ||= default_smell_category
end

.smell_typeObject


54
55
56
# File 'lib/reek/smells/smell_detector.rb', line 54

def smell_type
  @smell_type ||= default_smell_category
end

Instance Method Details

#config_for(ctx) ⇒ Object


109
110
111
# File 'lib/reek/smells/smell_detector.rb', line 109

def config_for(ctx)
  ctx.config_for(self.class)
end

#configure_with(config) ⇒ Object


81
82
83
# File 'lib/reek/smells/smell_detector.rb', line 81

def configure_with(config)
  @config.merge!(config)
end

#enabled?Boolean

SMELL: Getter (only used in 1 test)

Returns:

  • (Boolean)

77
78
79
# File 'lib/reek/smells/smell_detector.rb', line 77

def enabled?
  @config.enabled?
end

#enabled_for?(context) ⇒ Boolean

Returns:

  • (Boolean)

93
94
95
# File 'lib/reek/smells/smell_detector.rb', line 93

def enabled_for?(context)
  enabled? && config_for(context)[Core::SmellConfiguration::ENABLED_KEY] != false
end

#examine(context) ⇒ Object


85
86
87
88
89
90
91
# File 'lib/reek/smells/smell_detector.rb', line 85

def examine(context)
  return unless enabled_for? context
  return if exception?(context)

  sm = examine_context(context)
  @smells_found += sm
end

#exception?(context) ⇒ Boolean

Returns:

  • (Boolean)

97
98
99
# File 'lib/reek/smells/smell_detector.rb', line 97

def exception?(context)
  context.matches?(value(EXCLUDE_KEY, context, DEFAULT_EXCLUDE_SET))
end

#register(hooks) ⇒ Object


71
72
73
74
# File 'lib/reek/smells/smell_detector.rb', line 71

def register(hooks)
  return unless @config.enabled?
  self.class.contexts.each { |ctx| hooks[ctx] << self }
end

#report_on(report) ⇒ Object


101
102
103
# File 'lib/reek/smells/smell_detector.rb', line 101

def report_on(report)
  @smells_found.each { |smell| smell.report_on(report) }
end

#value(key, ctx, fall_back) ⇒ Object


105
106
107
# File 'lib/reek/smells/smell_detector.rb', line 105

def value(key, ctx, fall_back)
  config_for(ctx)[key] || @config.value(key, ctx, fall_back)
end