Class: Reek::Smells::SmellDetector Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Shared responsibilities of all smell detectors.

See

- {file:docs/Basic-Smell-Options.md}
- {file:docs/Code-Smells.md}
- {file:docs/Configuration-Files.md}

for details.

Constant Summary collapse

EXCLUDE_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SmellDetector.



74
75
76
77
78
# File 'lib/reek/smells/smell_detector.rb', line 74

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

Instance Attribute Details

#smells_foundObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

SMELL: only published for tests



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

def smells_found
  @smells_found
end

#sourceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def source
  @source
end

Class Method Details

.contextsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def contexts
  [:def, :defs]
end

.default_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.default_smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.descendantsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/reek/smells/smell_detector.rb', line 45

def descendants
  @subclasses
end

.smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def smell_category
  @smell_category ||= default_smell_category
end

.smell_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def smell_type
  @smell_type ||= default_smell_category
end

Instance Method Details

#config_for(ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



118
119
120
# File 'lib/reek/smells/smell_detector.rb', line 118

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

#configure_with(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
# File 'lib/reek/smells/smell_detector.rb', line 90

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

#enabled?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

SMELL: Getter (only used in 1 test)

Returns:

  • (Boolean)


86
87
88
# File 'lib/reek/smells/smell_detector.rb', line 86

def enabled?
  @config.enabled?
end

#enabled_for?(context) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

#examine(context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
100
# File 'lib/reek/smells/smell_detector.rb', line 94

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

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

#exception?(context) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

#register(hooks) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#report_on(report) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def smell_category
  self.class.smell_category
end

#smell_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def smell_type
  self.class.smell_type
end

#value(key, ctx, fall_back) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



114
115
116
# File 'lib/reek/smells/smell_detector.rb', line 114

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