lib/reek/smells/smell_detector.rb in reek-3.3.1 vs lib/reek/smells/smell_detector.rb in reek-3.4.0

- old
+ new

@@ -10,14 +10,13 @@ # - {file:docs/Basic-Smell-Options.md} # - {file:docs/Code-Smells.md} # - {file:README.md} # for details. # - # @api private + # :reek:TooManyMethods: { max_methods: 19 } + # :reek:TooManyInstanceVariables: { max_instance_variables: 5 } class SmellDetector - attr_reader :source - # 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_KEY = 'exclude' @@ -28,10 +27,11 @@ class << self def contexts [:def, :defs] end + # :reek:UtilityFunction def default_config { SmellConfiguration::ENABLED_KEY => true, EXCLUDE_KEY => DEFAULT_EXCLUDE_SET.dup } @@ -72,64 +72,67 @@ def default_smell_category name.split(/::/)[-1] end end - attr_reader :smells_found # SMELL: only published for tests - - def initialize(source, config = self.class.default_config) - @source = source + def initialize(config = {}) + config = self.class.default_config.merge(config) @config = SmellConfiguration.new(config) @smells_found = [] end def register(hooks) return unless config.enabled? self.class.contexts.each { |ctx| hooks[ctx] << self } end - # SMELL: Getter (only used in 1 test) - def enabled? - config.enabled? - end - - def configure_with(new_config) - config.merge!(new_config) - end - def examine(context) return unless enabled_for? context return if exception?(context) sm = examine_context(context) self.smells_found += sm end - def enabled_for?(context) - enabled? && config_for(context)[SmellConfiguration::ENABLED_KEY] != false + def report_on(report) + smells_found.each { |smell| smell.report_on(report) } end def exception?(context) context.matches?(value(EXCLUDE_KEY, context, DEFAULT_EXCLUDE_SET)) end - def report_on(report) - smells_found.each { |smell| smell.report_on(report) } + protected + + # NOTE: Needs to be protected so += works for Ruby < 2.2 + attr_accessor :smells_found + + private + + def enabled_for?(context) + config.enabled? && config_for(context)[SmellConfiguration::ENABLED_KEY] != false end def value(key, ctx, fall_back) config_for(ctx)[key] || config.value(key, ctx, fall_back) end def config_for(ctx) ctx.config_for(self.class) end - protected - - attr_writer :smells_found - - private + # :reek:FeatureEnvy + def smell_warning(options = {}) + context = options.fetch(:context) + exp = context.exp + ctx_source = exp.loc.expression.source_buffer.name + SmellWarning.new(self, + source: ctx_source, + context: context.full_name, + lines: options.fetch(:lines), + message: options.fetch(:message), + parameters: options.fetch(:parameters, {})) + end private_attr_reader :config end end end