Skip to content

Commit 57d97e4

Browse files
author
ABaldwinHunter
committed
Fix duplication issues
The validation criteria and any related calculations and messaging belong to each validation class, but the interface with the underlying issue object should remain consistent. Since that's the case, we can reduce duplication by moving some commonly accessed attributes into the parent class.
1 parent 92e27a9 commit 57d97e4

10 files changed

+26
-63
lines changed

lib/cc/analyzer/formatters/formatter.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ def initialize(filesystem, output = $stdout)
77
@output = output
88
end
99

10-
def write(_data)
10+
def write(data)
11+
json = JSON.parse(data)
12+
json["engine_name"] = current_engine.name
13+
14+
case json["type"].downcase
15+
when "issue"
16+
issues << json
17+
when "warning"
18+
warnings << json
19+
else
20+
raise "Invalid type found: #{json["type"]}"
21+
end
1122
end
1223

1324
def started

lib/cc/analyzer/formatters/html_formatter.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@ def pluralize(number, noun)
2626
end
2727
end
2828

29-
def write(data)
30-
json = JSON.parse(data)
31-
json["engine_name"] = current_engine.name
32-
33-
case json["type"].downcase
34-
when "issue"
35-
issues << json
36-
when "warning"
37-
warnings << json
38-
else
39-
raise "Invalid type found: #{json["type"]}"
40-
end
41-
end
42-
4329
def finished
4430
template = ReportTemplate.new(issues.length, issues_by_path)
4531
puts template.render

lib/cc/analyzer/formatters/plain_text_formatter.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ def started
1010
puts colorize("Starting analysis", :green)
1111
end
1212

13-
def write(data)
14-
json = JSON.parse(data)
15-
json["engine_name"] = current_engine.name
16-
17-
case json["type"].downcase
18-
when "issue"
19-
issues << json
20-
when "warning"
21-
warnings << json
22-
else
23-
raise "Invalid type found: #{json["type"]}"
24-
end
25-
end
26-
2713
def finished
2814
puts
2915

lib/cc/analyzer/issue_validations/content_validation.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ def message
1515
def has_content?
1616
object.key?("content")
1717
end
18-
19-
def content
20-
object["content"]
21-
end
2218
end
2319
end
2420
end

lib/cc/analyzer/issue_validations/path_existence_validation.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ module Analyzer
33
module IssueValidations
44
class PathExistenceValidation < Validation
55
def valid?
6-
File.exist?(path)
6+
path && File.exist?(path)
77
end
88

99
def message
1010
"File does not exist: '#{path}'"
1111
end
12-
13-
private
14-
15-
def path
16-
object.fetch("location", {}).fetch("path", "")
17-
end
1812
end
1913
end
2014
end

lib/cc/analyzer/issue_validations/path_is_file_validation.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ module Analyzer
33
module IssueValidations
44
class PathIsFileValidation < Validation
55
def valid?
6-
File.file?(path)
6+
path && File.file?(path)
77
end
88

99
def message
1010
"Path is not a file: '#{path}'"
1111
end
12-
13-
private
14-
15-
def path
16-
object.fetch("location", {}).fetch("path", "")
17-
end
1812
end
1913
end
2014
end

lib/cc/analyzer/issue_validations/path_presence_validation.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ def valid?
99
def message
1010
"Path must be present"
1111
end
12-
13-
private
14-
15-
def path
16-
object.fetch("location", {})["path"]
17-
end
1812
end
1913
end
2014
end

lib/cc/analyzer/issue_validations/relative_path_validation.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ def relative_to?(directory)
2222

2323
expanded_path.start_with?(expanded_base)
2424
end
25-
26-
def path
27-
object.fetch("location", {})["path"]
28-
end
2925
end
3026
end
3127
end

lib/cc/analyzer/issue_validations/type_validation.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ def valid?
99
def message
1010
"Type must be 'issue' but was '#{type}'"
1111
end
12-
13-
private
14-
15-
def type
16-
object["type"]
17-
end
1812
end
1913
end
2014
end

lib/cc/analyzer/issue_validations/validation.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ def message
1717
private
1818

1919
attr_reader :object
20+
21+
def path
22+
object.fetch("location", {})["path"]
23+
end
24+
25+
def type
26+
object["type"]
27+
end
28+
29+
def content
30+
object["content"]
31+
end
2032
end
2133
end
2234
end

0 commit comments

Comments
 (0)