Class: Reek::CheckstyleFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/reek-checkstyle-formatter/version.rb,
lib/reek-checkstyle-formatter.rb

Constant Summary collapse

VERSION =
"0.0.2"
REXML_PRETTY_PRINT_INDENT =
0

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CheckstyleFormatter

Returns a new instance of CheckstyleFormatter.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :glob_pattern (String)

    Glob pattern (absolute path) for files to analyse

  • :output (String)

    File path for checkstyle xml output

  • :smell_doc_url (String)

    URL to code smell documentation (reek wiki)

  • :smell_severities (Hash)

    Mapping of smell subclasses to [checkstyle severity](checkstyle.sourceforge.net/property_types.html#severity)



23
24
25
26
27
28
# File 'lib/reek-checkstyle-formatter.rb', line 23

def initialize(opts = {})
  @glob_pattern = opts[:glob_pattern]
  @output = opts[:output]
  @smell_doc_url = opts[:smell_doc_url]
  @smell_severities = opts[:smell_severities] || {}
end

Class Attribute Details

.rake_task_optionsObject

Returns the value of attribute rake_task_options.



10
11
12
# File 'lib/reek-checkstyle-formatter.rb', line 10

def rake_task_options
  @rake_task_options
end

Instance Attribute Details

#glob_patternObject

Returns the value of attribute glob_pattern.



16
17
18
# File 'lib/reek-checkstyle-formatter.rb', line 16

def glob_pattern
  @glob_pattern
end

#outputObject

Returns the value of attribute output.



16
17
18
# File 'lib/reek-checkstyle-formatter.rb', line 16

def output
  @output
end

#smell_doc_urlObject

Returns the value of attribute smell_doc_url.



16
17
18
# File 'lib/reek-checkstyle-formatter.rb', line 16

def smell_doc_url
  @smell_doc_url
end

#smell_severitiesObject

Returns the value of attribute smell_severities.



16
17
18
# File 'lib/reek-checkstyle-formatter.rb', line 16

def smell_severities
  @smell_severities
end

Instance Method Details

#add_file(checkstyle_node, examined_file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/reek-checkstyle-formatter.rb', line 54

def add_file(checkstyle_node, examined_file)
  if examined_file.smells.length > 0
    REXML::Element.new('file', checkstyle_node).tap do |file_node|
      file_node.attributes['name'] = File.join(examined_file.description)
      examined_file.smells.each do |smell|
        if smell.status['is_active']
          [*smell.location['lines']].each do |line|
            add_smell(file_node, smell, line)
          end
        end
      end
    end
  end
end

#add_smell(file_node, smell, line) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/reek-checkstyle-formatter.rb', line 69

def add_smell(file_node, smell, line)
  REXML::Element.new('error', file_node).tap do |error|
    error.attributes['line'] = line
    error.attributes['column'] = 0
    error.attributes['severity'] = smell_severity(smell)
    error.attributes['message'] = smell_message(smell)
    #error.attributes['source'] = File.join(@project_dir, smell.source)
  end
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reek-checkstyle-formatter.rb', line 30

def run
  puts "Generating Reek Checkstyle XML #{self.inspect}"
  files = Dir.glob(@glob_pattern)
  sources = Reek::Source::SourceLocator.new(files).all_sources
  examined_files = sources.map {|src| Reek::Examiner.new(src, []) }
  document = to_checkstyle(examined_files)
  Pathname.new(@output).dirname.mkpath
  File.open(@output, 'w+') do |file|
    document.write(file, REXML_PRETTY_PRINT_INDENT)
  end
  puts "Reek Checkstyle XML written to #{self.output}"
end

#smell_anchor(smell) ⇒ Object



83
84
85
86
# File 'lib/reek-checkstyle-formatter.rb', line 83

def smell_anchor(smell)
  url = smell_url(smell)
  "<a href='#{url}'>#{smell.smell['subclass']}</a>"
end

#smell_message(smell) ⇒ Object



79
80
81
# File 'lib/reek-checkstyle-formatter.rb', line 79

def smell_message(smell)
  "<em>#{smell_anchor(smell)}</em> - #{smell.message}"
end

#smell_severity(smell) ⇒ Object



93
94
95
# File 'lib/reek-checkstyle-formatter.rb', line 93

def smell_severity(smell)
  @smell_severities[smell.smell['subclass']] || 'warning'
end

#smell_url(smell) ⇒ Object



88
89
90
91
# File 'lib/reek-checkstyle-formatter.rb', line 88

def smell_url(smell)
  smell_page_name = (smell.smell['subclass']).scan(/[A-Z][^A-Z]*/).join('-')
  [@smell_doc_url, smell_page_name].join("/")
end

#to_checkstyle(examined_files) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/reek-checkstyle-formatter.rb', line 43

def to_checkstyle(examined_files)
  REXML::Document.new.tap do |document_node|
    document_node << REXML::XMLDecl.new
    REXML::Element.new('checkstyle', document_node).tap do |checkstyle_node|
      examined_files.each do |examined_file|
        add_file(checkstyle_node, examined_file)
      end
    end
  end
end