Class: Reek::Sniffer

Inherits:
Object show all
Defined in:
lib/reek/sniffer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ Sniffer

Returns a new instance of Sniffer.



66
67
68
69
70
71
72
73
# File 'lib/reek/sniffer.rb', line 66

def initialize(src)
  @already_checked_for_smells = false
  @typed_detectors = nil
  @detectors = Hash.new
  Sniffer.smell_classes.each { |klass| @detectors[klass] = DetectorStack.new(klass.new) }
  @source = src
  src.configure(self)
end

Class Method Details

.smell_classesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/reek/sniffer.rb', line 48

def self.smell_classes
  # SMELL: Duplication -- these should be loaded by listing the files
  [
    Smells::ControlCouple,
    Smells::DataClump,
    Smells::Duplication,
    Smells::FeatureEnvy,
    Smells::LargeClass,
    Smells::LongMethod,
    Smells::LongParameterList,
    Smells::LongYieldList,
    Smells::NestedIterators,
    Smells::SimulatedPolymorphism,
    Smells::UncommunicativeName,
    Smells::UtilityFunction,
  ]
end

Instance Method Details

#check_for_smellsObject



75
76
77
78
79
# File 'lib/reek/sniffer.rb', line 75

def check_for_smells
  return if @already_checked_for_smells
  CodeParser.new(self).process(@source.syntax_tree)
  @already_checked_for_smells = true
end

#configure(klass, config) ⇒ Object



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

def configure(klass, config)
  @detectors[klass].push(config)
end

#descObject



108
109
110
111
112
# File 'lib/reek/sniffer.rb', line 108

def desc
  # SMELL: Special Case
  # Only used in the Report tests, because they don't always create a Source.
  @source ? @source.desc : "unknown"
end

#examine(scope, type) ⇒ Object



90
91
92
93
# File 'lib/reek/sniffer.rb', line 90

def examine(scope, type)
  listeners = smell_listeners[type]
  listeners.each {|smell| smell.examine(scope) } if listeners
end

#has_smell?(smell_class, patterns = []) ⇒ Boolean

Checks for instances of smell_class, and returns true only if one of them has a report string matching all of the patterns.

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/reek/sniffer.rb', line 118

def has_smell?(smell_class, patterns=[])
  check_for_smells
  stack = @detectors[Reek::Smells.const_get(smell_class)]      # SMELL: Duplication of code in ConfigFile
  stack.has_smell?(patterns)
end

#num_smellsObject



101
102
103
104
105
106
# File 'lib/reek/sniffer.rb', line 101

def num_smells
  check_for_smells
  total = 0
  @detectors.each_value { |stack| total += stack.num_smells }
  total
end

#report_on(report) ⇒ Object



85
86
87
88
# File 'lib/reek/sniffer.rb', line 85

def report_on(report)
  check_for_smells
  @detectors.each_value { |stack| stack.report_on(report) }
end

#smells_only_of?(klass, patterns) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/reek/sniffer.rb', line 124

def smells_only_of?(klass, patterns)
  num_smells == 1 and has_smell?(klass, patterns)
end

#smelly?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/reek/sniffer.rb', line 95

def smelly?
  check_for_smells
  @detectors.each_value { |stack| return true if stack.smelly? }
  false
end

#sniffObject



128
129
130
# File 'lib/reek/sniffer.rb', line 128

def sniff
  self
end

#sniffersObject



132
133
134
# File 'lib/reek/sniffer.rb', line 132

def sniffers
  [self]
end