lib/reek/smells/smell_detector.rb in reek-1.2.6 vs lib/reek/smells/smell_detector.rb in reek-1.2.7

- old
+ new

@@ -1,19 +1,24 @@ -require 'reek/configuration' +require 'set' +require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning') +require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'core', 'smell_configuration') module Reek module Smells module ExcludeInitialize def self.default_config super.adopt(EXCLUDE_KEY => ['initialize']) end - def initialize(config = self.class.default_config) - super + def initialize(source, config = self.class.default_config) + super(source, config) end end + # + # Shared responsibilities of all smell detectors. + # class SmellDetector # 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. @@ -28,18 +33,21 @@ [:defn, :defs] end def default_config { - SmellConfiguration::ENABLED_KEY => true, + Core::SmellConfiguration::ENABLED_KEY => true, EXCLUDE_KEY => DEFAULT_EXCLUDE_SET } end end - def initialize(config = self.class.default_config) - @config = SmellConfiguration.new(config) + attr_reader :smells_found # SMELL: only published for tests + + def initialize(source, config = self.class.default_config) + @source = source + @config = Core::SmellConfiguration.new(config) @smells_found = Set.new @masked = false end def listen_to(hooks) @@ -60,42 +68,46 @@ def configure_with(config) @config.adopt!(config) end def copy - self.class.new(@config.deep_copy) + self.class.new(@source, @config.deep_copy) end def supersede_with(config) clone = self.copy @masked = true clone.configure_with(config) clone end def examine(context) - before = @smells_found.size examine_context(context) if @config.enabled? and !exception?(context) - @smells_found.length > before end def examine_context(context) end - + def exception?(context) context.matches?(value(EXCLUDE_KEY, context, DEFAULT_EXCLUDE_SET)) end - def found(context, message) - smell = SmellWarning.new(self, context.full_name, context.exp.line, message, @masked) + def found(context, message, subclass = '', parameters = {}, lines = nil) + lines ||= [context.exp.line] # SMELL: nil?!?!?! Yuk + smell = SmellWarning.new(self.class.name.split(/::/)[-1], context.full_name, lines, message, @masked, + @source, subclass, parameters) @smells_found << smell smell end def has_smell?(patterns) return false if @masked @smells_found.each { |warning| return true if warning.contains_all?(patterns) } false + end + + def smell_type + self.class.name.split(/::/)[-1] end def report_on(report) @smells_found.each { |smell| smell.report_on(report) } end