Class: Necropsy::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/necropsy/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, graph:, findings:, reachability: nil, report_include_paths: [], report_exclude_paths: []) ⇒ Report

Returns a new instance of Report.



10
11
12
13
14
15
16
17
# File 'lib/necropsy/report.rb', line 10

def initialize(root:, graph:, findings:, reachability: nil, report_include_paths: [], report_exclude_paths: [])
  @root = root
  @graph = graph
  @findings = findings.sort_by { |finding| [finding.node.file, finding.node.line, finding.node.id] }
  @reachability = reachability
  @report_include_paths = report_include_paths
  @report_exclude_paths = report_exclude_paths
end

Instance Attribute Details

#findingsObject (readonly)

Returns the value of attribute findings.



8
9
10
# File 'lib/necropsy/report.rb', line 8

def findings
  @findings
end

#graphObject (readonly)

Returns the value of attribute graph.



8
9
10
# File 'lib/necropsy/report.rb', line 8

def graph
  @graph
end

#reachabilityObject (readonly)

Returns the value of attribute reachability.



8
9
10
# File 'lib/necropsy/report.rb', line 8

def reachability
  @reachability
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/necropsy/report.rb', line 8

def root
  @root
end

Instance Method Details

#dead_methods(min_confidence: :low) ⇒ Object



19
20
21
# File 'lib/necropsy/report.rb', line 19

def dead_methods(min_confidence: :low)
  reported_findings.select { |finding| finding.at_least?(min_confidence) }
end

#summaryObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/necropsy/report.rb', line 44

def summary
  grouped = reported_findings.group_by(&:classification)
  {
    'nodes' => graph.nodes.length,
    'edges' => graph.edges.length,
    'entry_points' => graph.entry_points.length,
    'findings' => reported_findings.length,
    'unreachable' => grouped.fetch(:unreachable, []).length,
    'unused' => grouped.fetch(:unused, []).length,
    'test_only_reachable' => grouped.fetch(:test_only_reachable, []).length
  }
end

#to_h(include_graph: false) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/necropsy/report.rb', line 23

def to_h(include_graph: false)
  payload = {
    'root' => root,
    'summary' => summary,
    'findings' => reported_findings.map(&:to_h)
  }
  payload['graph'] = graph.to_h if include_graph
  payload
end

#to_json(state = nil, include_graph: false) ⇒ Object



33
34
35
36
37
38
# File 'lib/necropsy/report.rb', line 33

def to_json(state = nil, include_graph: false)
  payload = to_h(include_graph: include_graph)
  return JSON.pretty_generate(payload) unless state

  payload.to_json(state)
end

#to_yaml(include_graph: false) ⇒ Object



40
41
42
# File 'lib/necropsy/report.rb', line 40

def to_yaml(include_graph: false)
  to_h(include_graph: include_graph).to_yaml
end