Class: Reek::Examiner

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

Overview

Finds the active code smells in Ruby source code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, smell_types_to_filter_by = []) ⇒ Examiner

Creates an Examiner which scans the given source for code smells.

The smells reported against any source file can be “masked” by creating *.reek files. See TBS for details.

Parameters:

  • If (Source::SourceCode, Array<String>, #to_reek_source)

    source is a String it is assumed to be Ruby source code; if it is a File, the file is opened and parsed for Ruby source code; and if it is an Array, it is assumed to be a list of file paths, each of which is opened and parsed for source code.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reek/examiner.rb', line 28

def initialize(source, smell_types_to_filter_by = [])
  sources = Source::SourceRepository.parse(source)
  @description = sources.description
  @collector = Core::WarningCollector.new

  smell_types = Core::SmellRepository.smell_types

  if smell_types_to_filter_by.any?
    smell_types.select! { |klass| smell_types_to_filter_by.include? klass.smell_type }
  end

  sources.each do |src|
    repository = Core::SmellRepository.new(src.desc, smell_types)
    Core::Sniffer.new(src, repository).report_on(@collector)
  end
end

Instance Attribute Details

#descriptionObject

A simple description of the source being analysed for smells. If the source is a single File, this will be the file's path.


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

def description
  @description
end

Instance Method Details

#num_active_smellsObject

Deprecated.

Use #smells_count instead.

Returns the number of non-masked smells in the source.


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

def num_active_smells
  smells.length
end

#num_masked_smellsObject

Deprecated.

Masked smells are no longer reported; this method always returns 0.

Returns the number of masked smells in the source.


100
101
102
# File 'lib/reek/examiner.rb', line 100

def num_masked_smells
  0
end

#smellsArray<SmellWarning> Also known as: all_active_smells, all_smells

List the smells found in the source.

Returns:


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

def smells
  @smells ||= @collector.warnings
end

#smells_countObject

Returns the number of smells found in the source


57
58
59
# File 'lib/reek/examiner.rb', line 57

def smells_count
  smells.length
end

#smelly?Boolean

True if and only if there are code smells in the source.

Returns:

  • (Boolean)

64
65
66
# File 'lib/reek/examiner.rb', line 64

def smelly?
  !smells.empty?
end