Skip to content

Commit

Permalink
Merge pull request #94 from codeclimate/abh-update-algorithm
Browse files Browse the repository at this point in the history
Update penalty algorithm for PHP and JavaScript
  • Loading branch information
Ashley Baldwin-Hunter committed Feb 2, 2016
2 parents 97b6ba1 + 90dfde1 commit d934f7c
Showing 7 changed files with 17 additions and 24 deletions.
13 changes: 12 additions & 1 deletion lib/cc/engine/analyzers/analyzer_base.rb
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ class Base
::RuntimeError,
].freeze

BASE_POINTS = 1_500_000

def initialize(engine_config:)
@engine_config = engine_config
end
@@ -37,13 +39,22 @@ def mass_threshold
end

def calculate_points(mass)
self.class::BASE_POINTS * mass
overage = mass - mass_threshold
base_points + (overage * points_per_overage)
end

private

attr_reader :engine_config

def base_points
self.class::BASE_POINTS
end

def points_per_overage
self.class::POINTS_PER_OVERAGE
end

def process_file(path)
raise NoMethodError.new("Subclass must implement `process_file`")
end
2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/javascript/main.rb
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class Main < CC::Engine::Analyzers::Base
]
LANGUAGE = "javascript"
DEFAULT_MASS_THRESHOLD = 40
BASE_POINTS = 3000
POINTS_PER_OVERAGE = 30_000

private

2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/php/main.rb
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ class Main < CC::Engine::Analyzers::Base
"**/*.module"
]
DEFAULT_MASS_THRESHOLD = 10
BASE_POINTS = 4_000
POINTS_PER_OVERAGE = 100_000

private

9 changes: 0 additions & 9 deletions lib/cc/engine/analyzers/python/main.rb
Original file line number Diff line number Diff line change
@@ -13,19 +13,10 @@ class Main < CC::Engine::Analyzers::Base
LANGUAGE = "python"
DEFAULT_PATHS = ["**/*.py"]
DEFAULT_MASS_THRESHOLD = 32
BASE_POINTS = 1_500_000
POINTS_PER_OVERAGE = 50_000

def calculate_points(mass)
BASE_POINTS + (overage(mass) * POINTS_PER_OVERAGE)
end

private

def overage(mass)
mass - mass_threshold
end

def process_file(path)
Node.new(::CC::Engine::Analyzers::Python::Parser.new(File.binread(path), path).parse.syntax_tree, path).format
end
9 changes: 0 additions & 9 deletions lib/cc/engine/analyzers/ruby/main.rb
Original file line number Diff line number Diff line change
@@ -18,20 +18,11 @@ class Main < CC::Engine::Analyzers::Base

]
DEFAULT_MASS_THRESHOLD = 18
BASE_POINTS = 1_500_000
POINTS_PER_OVERAGE = 100_000
TIMEOUT = 300

def calculate_points(mass)
BASE_POINTS + (overage(mass) * POINTS_PER_OVERAGE)
end

private

def overage(mass)
mass - mass_threshold
end

def process_file(file)
RubyParser.new.process(File.binread(file), file, TIMEOUT)
end
4 changes: 2 additions & 2 deletions spec/cc/engine/analyzers/javascript/main_spec.rb
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
"path" => "foo.js",
"lines" => { "begin" => 1, "end" => 1 },
})
expect(json["remediation_points"]).to eq(33_000)
expect(json["remediation_points"]).to eq(1_800_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
@@ -55,7 +55,7 @@
"path" => "foo.js",
"lines" => { "begin" => 1, "end" => 1 },
})
expect(json["remediation_points"]).to eq(33_000)
expect(json["remediation_points"]).to eq(1_800_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
2 changes: 1 addition & 1 deletion spec/cc/engine/analyzers/php/main_spec.rb
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
"path" => "foo.php",
"lines" => { "begin" => 2, "end" => 6 },
})
expect(json["remediation_points"]).to eq(44_000)
expect(json["remediation_points"]).to eq(2_100_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.php", "lines" => { "begin" => 10, "end" => 14} },
])

0 comments on commit d934f7c

Please sign in to comment.