Skip to content

Commit 90dfde1

Browse files
author
ABaldwinHunter
committed
Update penalty algorithm for PHP and JavaScript
Bug fix to make calculation harsher. Matches classic. Refactor to make use of base class.
1 parent 97b6ba1 commit 90dfde1

File tree

7 files changed

+17
-24
lines changed

7 files changed

+17
-24
lines changed

Diff for: lib/cc/engine/analyzers/analyzer_base.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Base
1313
::RuntimeError,
1414
].freeze
1515

16+
BASE_POINTS = 1_500_000
17+
1618
def initialize(engine_config:)
1719
@engine_config = engine_config
1820
end
@@ -37,13 +39,22 @@ def mass_threshold
3739
end
3840

3941
def calculate_points(mass)
40-
self.class::BASE_POINTS * mass
42+
overage = mass - mass_threshold
43+
base_points + (overage * points_per_overage)
4144
end
4245

4346
private
4447

4548
attr_reader :engine_config
4649

50+
def base_points
51+
self.class::BASE_POINTS
52+
end
53+
54+
def points_per_overage
55+
self.class::POINTS_PER_OVERAGE
56+
end
57+
4758
def process_file(path)
4859
raise NoMethodError.new("Subclass must implement `process_file`")
4960
end

Diff for: lib/cc/engine/analyzers/javascript/main.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Main < CC::Engine::Analyzers::Base
1616
]
1717
LANGUAGE = "javascript"
1818
DEFAULT_MASS_THRESHOLD = 40
19-
BASE_POINTS = 3000
19+
POINTS_PER_OVERAGE = 30_000
2020

2121
private
2222

Diff for: lib/cc/engine/analyzers/php/main.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Main < CC::Engine::Analyzers::Base
1515
"**/*.module"
1616
]
1717
DEFAULT_MASS_THRESHOLD = 10
18-
BASE_POINTS = 4_000
18+
POINTS_PER_OVERAGE = 100_000
1919

2020
private
2121

Diff for: lib/cc/engine/analyzers/python/main.rb

-9
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,10 @@ class Main < CC::Engine::Analyzers::Base
1313
LANGUAGE = "python"
1414
DEFAULT_PATHS = ["**/*.py"]
1515
DEFAULT_MASS_THRESHOLD = 32
16-
BASE_POINTS = 1_500_000
1716
POINTS_PER_OVERAGE = 50_000
1817

19-
def calculate_points(mass)
20-
BASE_POINTS + (overage(mass) * POINTS_PER_OVERAGE)
21-
end
22-
2318
private
2419

25-
def overage(mass)
26-
mass - mass_threshold
27-
end
28-
2920
def process_file(path)
3021
Node.new(::CC::Engine::Analyzers::Python::Parser.new(File.binread(path), path).parse.syntax_tree, path).format
3122
end

Diff for: lib/cc/engine/analyzers/ruby/main.rb

-9
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,11 @@ class Main < CC::Engine::Analyzers::Base
1818

1919
]
2020
DEFAULT_MASS_THRESHOLD = 18
21-
BASE_POINTS = 1_500_000
2221
POINTS_PER_OVERAGE = 100_000
2322
TIMEOUT = 300
2423

25-
def calculate_points(mass)
26-
BASE_POINTS + (overage(mass) * POINTS_PER_OVERAGE)
27-
end
28-
2924
private
3025

31-
def overage(mass)
32-
mass - mass_threshold
33-
end
34-
3526
def process_file(file)
3627
RubyParser.new.process(File.binread(file), file, TIMEOUT)
3728
end

Diff for: spec/cc/engine/analyzers/javascript/main_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"path" => "foo.js",
2828
"lines" => { "begin" => 1, "end" => 1 },
2929
})
30-
expect(json["remediation_points"]).to eq(33_000)
30+
expect(json["remediation_points"]).to eq(1_800_000)
3131
expect(json["other_locations"]).to eq([
3232
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
3333
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
@@ -55,7 +55,7 @@
5555
"path" => "foo.js",
5656
"lines" => { "begin" => 1, "end" => 1 },
5757
})
58-
expect(json["remediation_points"]).to eq(33_000)
58+
expect(json["remediation_points"]).to eq(1_800_000)
5959
expect(json["other_locations"]).to eq([
6060
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
6161
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }

Diff for: spec/cc/engine/analyzers/php/main_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"path" => "foo.php",
4141
"lines" => { "begin" => 2, "end" => 6 },
4242
})
43-
expect(json["remediation_points"]).to eq(44_000)
43+
expect(json["remediation_points"]).to eq(2_100_000)
4444
expect(json["other_locations"]).to eq([
4545
{"path" => "foo.php", "lines" => { "begin" => 10, "end" => 14} },
4646
])

0 commit comments

Comments
 (0)