Updated

lib/simplecov_small_badge / formatter.rb

B
75 lines of codes
8 methods
6.5 complexity/method
7 churn
52.26 complexity
0 duplications
# frozen_string_literal: true require 'repo_small_badge/image' require 'simplecov_small_badge/configuration' module SimpleCovSmallBadge # Basic Badge Formater Class that creates the badges. class Formatter
  1. SimpleCovSmallBadge::Formatter assumes too much for instance variable '@image'
def initialize(output = nil) @output = output || $stdout
  1. SimpleCovSmallBadge::Formatter#initialize is controlled by argument 'output'
@config = SimpleCovSmallBadge.config end def format(result) percent = result.source_files.covered_percent.round(0) @image = RepoSmallBadge::Image.new(map_image_config(state(percent))) badge('total', 'total', percent) group_percent_from_result(result) do |name, title, cov_percent| badge(name, title, cov_percent.round(0)) end end private def badge(name, title, percent) percent_txt = percent_text(percent) @image.config_merge(map_image_config(state(percent))) @image.badge(name, title, percent_txt) end def percent_text(percent) "#{percent}#{@config.percent_sign}" end def state(covered_percent) if line_coverage_minimum&.positive? if covered_percent >= line_coverage_minimum 'good' else 'bad' end else 'unknown' end end def line_coverage_minimum
  1. SimpleCovSmallBadge::Formatter#line_coverage_minimum doesn't depend on instance state (maybe move it to another class?)
minimums = SimpleCov.minimum_coverage minimums[SimpleCov.primary_coverage] end def map_image_config(state)
  1. SimpleCovSmallBadge::Formatter#map_image_config has approx 6 statements
hash = {} @config.to_hash.map do |key, value| key = key
  1. SimpleCovSmallBadge::Formatter#map_image_config calls 'key; .to_s' 2 times Locations: 0 1
  2. SimpleCovSmallBadge::Formatter#map_image_config refers to 'key' more than self (maybe move it to another class?) Locations: 0 1
.to_s.sub(/^coverage_background_#{state}/, 'value_background') .to_sym key = key.to_s.sub(/^coverage_/, 'value_').to_sym
  1. SimpleCovSmallBadge::Formatter#map_image_config calls 'key; .to_s' 2 times Locations: 0 1
  2. SimpleCovSmallBadge::Formatter#map_image_config refers to 'key' more than self (maybe move it to another class?) Locations: 0 1
hash[key] = value end hash end # converts the result to a hash consisting of the groupname # array of percentage (integer in percent), strength float # and the state [ 'good', 'bad', 'unknown' ] # consolidated for each group. def group_percent_from_result(result)
  1. SimpleCovSmallBadge::Formatter#group_percent_from_result doesn't depend on instance state (maybe move it to another class?)
result.groups.each do |name, files| covered = files.covered_percent.round(0) yield name, name, covered, covered
  1. SimpleCovSmallBadge::Formatter#group_percent_from_result yields 4 parameters
end end end end