Class: Reek::DetectorRepository

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

Overview

Contains all the existing smell detectors and exposes operations on them.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smell_types: self.class.smell_types, configuration: {}) ⇒ DetectorRepository

Returns a new instance of DetectorRepository.



35
36
37
38
39
# File 'lib/reek/detector_repository.rb', line 35

def initialize(smell_types: self.class.smell_types,
               configuration: {})
  @configuration = configuration
  @smell_types   = smell_types
end

Instance Attribute Details

#configurationObject (readonly, private)

Returns the value of attribute configuration.



56
57
58
# File 'lib/reek/detector_repository.rb', line 56

def configuration
  @configuration
end

#smell_typesObject (readonly, private)

Returns the value of attribute smell_types.



56
57
58
# File 'lib/reek/detector_repository.rb', line 56

def smell_types
  @smell_types
end

Class Method Details

.available_detector_namesArray<String>

Returns The names of all known SmellDetectors e.g. [“BooleanParameter”, “ClassVariable”].

Returns:

  • (Array<String>)

    The names of all known SmellDetectors e.g. [“BooleanParameter”, “ClassVariable”].



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

def self.available_detector_names
  smell_types.map(&:smell_type)
end

.eligible_smell_types(filter_by_smells = []) ⇒ Array<Reek::SmellDetectors::BaseDetector>

Returns All SmellDetectors that we want to filter for e.g. [Reek::SmellDetectors::Attribute].

Parameters:

  • filter_by_smells (Array<String>) (defaults to: [])

    List of smell types to filter by, e.g. “DuplicateMethodCall”. More precisely it should be whatever is returned by ‘BaseDetector`.smell_type. This means that you can write the “DuplicateMethodCall” from above also like this:

    Reek::SmellDetectors::DuplicateMethodCall.smell_type
    

    if you want to make sure you do not fat-finger strings.

Returns:



27
28
29
30
31
32
33
# File 'lib/reek/detector_repository.rb', line 27

def self.eligible_smell_types(filter_by_smells = [])
  return smell_types if filter_by_smells.empty?

  smell_types.select do |klass|
    filter_by_smells.include? klass.smell_type
  end
end

.smell_typesArray<Reek::SmellDetectors::BaseDetector>

Returns All known SmellDetectors e.g. [Reek::SmellDetectors::BooleanParameter, Reek::SmellDetectors::ClassVariable].

Returns:



14
15
16
# File 'lib/reek/detector_repository.rb', line 14

def self.smell_types
  Reek::SmellDetectors::BaseDetector.descendants.sort_by(&:name)
end

Instance Method Details

#configuration_for(klass) ⇒ Object (private)



58
59
60
# File 'lib/reek/detector_repository.rb', line 58

def configuration_for(klass)
  configuration.fetch klass, {}
end

#examine(context) ⇒ Object



41
42
43
44
45
46
# File 'lib/reek/detector_repository.rb', line 41

def examine(context)
  smell_detectors_for(context.type).flat_map do |klass|
    detector = klass.new configuration: configuration_for(klass), context: context
    detector.run
  end
end

#smell_detectors_for(type) ⇒ Object (private)



62
63
64
# File 'lib/reek/detector_repository.rb', line 62

def smell_detectors_for(type)
  smell_types.select { |detector| detector.contexts.include? type }
end