class Reek::SmellDetectors::BaseDetector
Shared responsibilities of all smell detectors.
See
- {file:docs/Basic-Smell-Options.md} - {file:docs/Code-Smells.md} - {file:README.md}
for details.
@quality :reek:UnusedPrivateMethod { exclude: [ smell_warning
] } @quality :reek:TooManyMethods { max_methods: 18 }
Constants
- DEFAULT_EXCLUDE_SET
-
The default value for the
EXCLUDE_KEY
if it isn’t specified in any configuration file. - 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.
Attributes
Public Class Methods
Source
# File lib/reek/smell_detectors/base_detector.rb, line 139 def configuration_keys Set.new(default_config.keys.map(&:to_sym)) end
@return [Set<Symbol>] all configuration keys that are available for this detector
Source
# File lib/reek/smell_detectors/base_detector.rb, line 96 def contexts [:def, :defs] end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 101 def default_config { SmellConfiguration::ENABLED_KEY => true, EXCLUDE_KEY => DEFAULT_EXCLUDE_SET.dup } end
@quality :reek:UtilityFunction
Source
# File lib/reek/smell_detectors/base_detector.rb, line 121 def descendants @descendants ||= [] end
Returns all descendants of BaseDetector
@return [Array<Constant>], e.g.:
[Reek::SmellDetectors::Attribute, Reek::SmellDetectors::BooleanParameter, Reek::SmellDetectors::ClassVariable, ...]
Source
# File lib/reek/smell_detectors/base_detector.rb, line 108 def inherited(subclass) descendants << subclass end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 32 def initialize(configuration: {}, context: nil) @config = SmellConfiguration.new self.class.default_config.merge(configuration) @context = context end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 92 def smell_type @smell_type ||= name.split('::').last end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 132 def to_detector(detector_name) SmellDetectors.const_get detector_name end
Transform a detector name to the corresponding constant. Note that we assume a valid name - exceptions are not handled here.
@param detector_name [String] the detector in question, e.g. ‘DuplicateMethodCall’ @return [SmellDetector] this will return the class, not an instance
Source
# File lib/reek/smell_detectors/base_detector.rb, line 48 def self.todo_configuration_for(smells) default_exclusions = default_config.fetch 'exclude' exclusions = default_exclusions + smells.map(&:context) { smell_type => { 'exclude' => exclusions.uniq } } end
Public Instance Methods
Source
# File lib/reek/smell_detectors/base_detector.rb, line 41 def run return [] unless enabled? return [] if exception? sniff end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 37 def smell_type self.class.smell_type end
Private Instance Methods
Source
# File lib/reek/smell_detectors/base_detector.rb, line 78 def config_for(ctx) ctx.config_for(self.class) end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 70 def enabled? config.enabled? && config_for(context)[SmellConfiguration::ENABLED_KEY] != false end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 66 def exception? context.matches?(value(EXCLUDE_KEY, context)) end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 58 def expression @expression ||= context.exp end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 82 def smell_warning(**options) SmellWarning.new(smell_type, source: expression.source, context: context.full_name, lines: options.fetch(:lines), message: options.fetch(:message), parameters: options.fetch(:parameters, {})) end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 62 def source_line @source_line ||= expression.line end
Source
# File lib/reek/smell_detectors/base_detector.rb, line 74 def value(key, ctx) config_for(ctx)[key] || config.value(key, ctx) end