Skip to content

Commit 3d18d90

Browse files
author
Ashley Baldwin-Hunter
committed
Merge pull request #84 from codeclimate/abh-mass-node-size
Use sexp node size for issue mass, not flay score
2 parents a9af216 + 13820c5 commit 3d18d90

File tree

10 files changed

+53
-43
lines changed

10 files changed

+53
-43
lines changed

config/contents/duplicated_code.md.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ When you violate DRY, bugs and maintenance problems are sure to follow. Duplicat
99
## Issue Mass
1010

1111
Duplicated code has a calculated mass, which can be thought of as a measure of how much logic has been duplicated.
12-
This issue has a mass of `<%= issue.mass %>`: if you would like to change the minimum mass that will be reported as an issue, please see the details in [`codeclimate-duplication`'s documentation](https://github.com/codeclimate/codeclimate-duplication).
12+
This issue has a mass of `<%= mass %>`: if you would like to change the minimum mass that will be reported as an issue, please see the details in [`codeclimate-duplication`'s documentation](https://github.com/codeclimate/codeclimate-duplication).
1313

1414
## Refactorings
1515

lib/cc/engine/analyzers/analyzer_base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def mass_threshold
3636
engine_config.mass_threshold_for(self.class::LANGUAGE) || self.class::DEFAULT_MASS_THRESHOLD
3737
end
3838

39-
def calculate_points(issue)
40-
self.class::BASE_POINTS * issue.mass
39+
def calculate_points(mass)
40+
self.class::BASE_POINTS * mass
4141
end
4242

4343
private

lib/cc/engine/analyzers/ruby/main.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class Main < CC::Engine::Analyzers::Base
2222
POINTS_PER_OVERAGE = 100_000
2323
TIMEOUT = 300
2424

25-
def calculate_points(issue)
26-
BASE_POINTS + (overage(issue) * POINTS_PER_OVERAGE)
25+
def calculate_points(mass)
26+
BASE_POINTS + (overage(mass) * POINTS_PER_OVERAGE)
2727
end
2828

2929
private
3030

31-
def overage(issue)
32-
issue.mass - mass_threshold
31+
def overage(mass)
32+
mass - mass_threshold
3333
end
3434

3535
def process_file(file)

lib/cc/engine/analyzers/violation.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ module CC
66
module Engine
77
module Analyzers
88
class Violation
9-
attr_reader :issue
10-
11-
def initialize(language_strategy:, issue:, current_sexp:, other_sexps:)
9+
def initialize(language_strategy:, identical:, current_sexp:, other_sexps:)
1210
@language_strategy = language_strategy
13-
@issue = issue
11+
@identical = identical
1412
@current_sexp = current_sexp
1513
@other_sexps = other_sexps
1614
end
@@ -33,20 +31,28 @@ def report_name
3331
"#{current_sexp.file}-#{current_sexp.line}"
3432
end
3533

34+
def mass
35+
current_sexp.mass
36+
end
37+
3638
private
3739

3840
attr_reader :language_strategy, :other_sexps, :current_sexp
3941

4042
def check_name
41-
if issue.identical?
43+
if identical?
4244
"Identical code"
4345
else
4446
"Similar code"
4547
end
4648
end
4749

50+
def identical?
51+
@identical
52+
end
53+
4854
def calculate_points
49-
language_strategy.calculate_points(issue)
55+
language_strategy.calculate_points(mass)
5056
end
5157

5258
def format_location
@@ -71,7 +77,7 @@ def format_sexp(sexp)
7177
end
7278

7379
def content_body
74-
@_content_body ||= { "body": ViolationReadUp.new(issue).contents }
80+
@_content_body ||= { "body": ViolationReadUp.new(mass).contents }
7581
end
7682

7783
def fingerprint

lib/cc/engine/analyzers/violation_read_up.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module CC
44
module Engine
55
module Analyzers
66
class ViolationReadUp
7-
def initialize(issue)
8-
@issue = issue
7+
def initialize(mass)
8+
@mass = mass
99
end
1010

1111
def contents
@@ -14,7 +14,7 @@ def contents
1414

1515
private
1616

17-
attr_reader :issue
17+
attr_reader :mass
1818

1919
TEMPLATE_REL_PATH = "../../../../config/contents/duplicated_code.md.erb"
2020

lib/cc/engine/analyzers/violations.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def each
1515
yield Violation.new(
1616
current_sexp: sexp,
1717
other_sexps: other_sexps(hashes.dup, i),
18-
issue: issue,
18+
identical: identical?,
1919
language_strategy: language_strategy,
2020
)
2121
end
@@ -29,6 +29,10 @@ def other_sexps(members, i)
2929
members.delete_at(i)
3030
members.sort_by(&:file)
3131
end
32+
33+
def identical?
34+
issue.identical?
35+
end
3236
end
3337
end
3438
end

spec/cc/engine/analyzers/javascript/main_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
"path" => "foo.js",
2929
"lines" => { "begin" => 1, "end" => 1 },
3030
})
31-
expect(json["remediation_points"]).to eq(297000)
31+
expect(json["remediation_points"]).to eq(33_000)
3232
expect(json["other_locations"]).to eq([
3333
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
3434
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
3535
])
36-
expect(json["content"]["body"]).to match /This issue has a mass of `99`/
36+
expect(json["content"]["body"]).to match /This issue has a mass of `11`/
3737
expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d")
3838
end
3939

@@ -56,12 +56,12 @@
5656
"path" => "foo.js",
5757
"lines" => { "begin" => 1, "end" => 1 },
5858
})
59-
expect(json["remediation_points"]).to eq(99000)
59+
expect(json["remediation_points"]).to eq(33_000)
6060
expect(json["other_locations"]).to eq([
6161
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
6262
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
6363
])
64-
expect(json["content"]["body"]).to match /This issue has a mass of `33`/
64+
expect(json["content"]["body"]).to match /This issue has a mass of `11`/
6565
expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d")
6666
end
6767

spec/cc/engine/analyzers/php/main_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
"path" => "foo.php",
4242
"lines" => { "begin" => 2, "end" => 6 },
4343
})
44-
expect(json["remediation_points"]).to eq(176000)
44+
expect(json["remediation_points"]).to eq(44_000)
4545
expect(json["other_locations"]).to eq([
4646
{"path" => "foo.php", "lines" => { "begin" => 10, "end" => 14} },
4747
])
48-
expect(json["content"]["body"]).to match /This issue has a mass of `44`/
48+
expect(json["content"]["body"]).to match /This issue has a mass of `11`/
4949
expect(json["fingerprint"]).to eq("667da0e2bab866aa2fe9d014a65d57d9")
5050
end
5151

spec/cc/engine/analyzers/python/main_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
"path" => "foo.py",
2929
"lines" => { "begin" => 1, "end" => 1 },
3030
})
31-
expect(json["remediation_points"]).to eq(54000)
31+
expect(json["remediation_points"]).to eq(6_000)
3232
expect(json["other_locations"]).to eq([
3333
{"path" => "foo.py", "lines" => { "begin" => 2, "end" => 2} },
3434
{"path" => "foo.py", "lines" => { "begin" => 3, "end" => 3} }
3535
])
36-
expect(json["content"]["body"]).to match /This issue has a mass of `54`/
36+
expect(json["content"]["body"]).to match /This issue has a mass of `6`/
3737
expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc")
3838
end
3939

@@ -56,12 +56,12 @@
5656
"path" => "foo.py",
5757
"lines" => { "begin" => 1, "end" => 1 },
5858
})
59-
expect(json["remediation_points"]).to eq(18000)
59+
expect(json["remediation_points"]).to eq(6_000)
6060
expect(json["other_locations"]).to eq([
6161
{"path" => "foo.py", "lines" => { "begin" => 2, "end" => 2} },
6262
{"path" => "foo.py", "lines" => { "begin" => 3, "end" => 3} }
6363
])
64-
expect(json["content"]["body"]).to match /This issue has a mass of `18`/
64+
expect(json["content"]["body"]).to match /This issue has a mass of `6`/
6565
expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc")
6666
end
6767

spec/cc/engine/analyzers/ruby/main_spec.rb

+15-15
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ module CC::Engine::Analyzers
4040
"path" => "foo.rb",
4141
"lines" => { "begin" => 1, "end" => 5 },
4242
})
43-
expect(json["remediation_points"]).to eq(3300000)
43+
expect(json["remediation_points"]).to eq(1_500_000)
4444
expect(json["other_locations"]).to eq([
4545
{"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} },
4646
])
47-
expect(json["content"]["body"]).to match /This issue has a mass of `36`/
47+
expect(json["content"]["body"]).to match /This issue has a mass of `18`/
4848
expect(json["fingerprint"]).to eq("f21b75bbd135ec3ae6638364d5c73762")
4949
end
5050

@@ -91,43 +91,43 @@ module CC::Engine::Analyzers
9191
end
9292
end
9393

94-
describe "#calculate_points(issue)" do
94+
describe "#calculate_points(mass)" do
9595
let(:analyzer) { Ruby::Main.new(engine_config: engine_conf) }
9696
let(:base_points) { Ruby::Main::BASE_POINTS }
9797
let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE }
9898
let(:threshold) { Ruby::Main::DEFAULT_MASS_THRESHOLD }
9999

100-
context "when issue mass exceeds threshold" do
100+
context "when mass exceeds threshold" do
101101
it "calculates mass overage points" do
102-
issue = double(:issue, mass: threshold + 10)
103-
overage = issue.mass - threshold
102+
mass = threshold + 10
103+
overage = mass - threshold
104104

105105
expected_points = base_points + overage * points_per
106-
points = analyzer.calculate_points(issue)
106+
points = analyzer.calculate_points(mass)
107107

108108
expect(points).to eq(expected_points)
109109
end
110110
end
111111

112-
context "when issue mass is less than threshold" do
112+
context "when mass is less than threshold" do
113113
it "calculates mass overage points" do
114-
issue = double(:issue, mass: threshold - 5)
115-
overage = issue.mass - threshold
114+
mass = threshold - 5
115+
overage = mass - threshold
116116

117117
expected_points = base_points + overage * points_per
118-
points = analyzer.calculate_points(issue)
118+
points = analyzer.calculate_points(mass)
119119

120120
expect(points).to eq(expected_points)
121121
end
122122
end
123123

124-
context "when issue mass equals threshold" do
124+
context "when mass equals threshold" do
125125
it "calculates mass overage points" do
126-
issue = double(:issue, mass: threshold)
127-
overage = issue.mass - threshold
126+
mass = threshold
127+
overage = mass - threshold
128128

129129
expected_points = base_points + overage * points_per
130-
points = analyzer.calculate_points(issue)
130+
points = analyzer.calculate_points(mass)
131131

132132
expect(points).to eq(expected_points)
133133
end

0 commit comments

Comments
 (0)