<commit_before>require 'forwardable'
module DebtCeiling
  class Debt
    extend Forwardable
    DoNotWhitelistAndBlacklistSimulateneously = Class.new(StandardError)

    attr_reader :file_attributes
    def_delegators :file_attributes, :path, :analysed_module, :module_name, :linecount, :source_code
    def_delegator :analysed_module, :rating
    attr_accessor :debt_amount
    def_delegator :debt_amount, :to_i

    def initialize(file_attributes)
      @file_attributes  = file_attributes
      default_measure_debt if valid_debt?
    end

    def default_measure_debt
      cost = public_send(:measure_debt) if self.respond_to?(:measure_debt)

      unless cost
        cost = public_send(:augment_debt) if respond_to?(:augment_debt)
        cost = cost.to_i
        letter_grade = rating.to_s.downcase
        cost_per_line = DebtCeiling.public_send("#{letter_grade}_current_cost_per_line")
        cost += file_attributes.linecount * cost_per_line
        cost += debt_from_source_code_rules
      end
      self.debt_amount = cost
    end

    def debt_from_source_code_rules
      manual_callout_debt +
      text_match_debt('TODO', DebtCeiling.current_cost_per_todo) +
      DebtCeiling.deprecated_reference_pairs
        .reduce(0) {|accum, (string, value)| accum + text_match_debt(string, value.to_i) }
    end

    def text_match_debt(string, cost)
      source_code.scan(string).count * cost
    end

    def manual_callout_debt
      DebtCeiling.manual_callouts.reduce(0) do |memo, callout|
        memo + source_code.each_line.reduce(0) do |accum, line|
          match_data = line.match(Regexp.new(callout + '.*'))
          string = match_data.to_s.split(callout).last
          amount = string.match(/\d+/).to_s if string
          accum + amount.to_i
        end
      end
    end

    def valid_debt?
      black_empty = DebtCeiling.blacklist.empty?
      white_empty = DebtCeiling.whitelist.empty?
      fail DoNotWhitelistAndBlacklistSimulateneously if !black_empty && !white_empty<commit_msg>Putting the unless back that was causing the error before. I threw it in too quickly before and used the wrong operator.<commit_after>require 'forwardable'
module DebtCeiling
  class Debt
    extend Forwardable
    DoNotWhitelistAndBlacklistSimulateneously = Class.new(StandardError)

    attr_reader :file_attributes
    def_delegators :file_attributes, :path, :analysed_module, :module_name, :linecount, :source_code
    def_delegator :analysed_module, :rating
    attr_accessor :debt_amount
    def_delegator :debt_amount, :to_i

    def initialize(file_attributes)
      @file_attributes  = file_attributes
      default_measure_debt if valid_debt?
    end

    def default_measure_debt
      cost = public_send(:measure_debt) if self.respond_to?(:measure_debt)

      unless cost
        cost = public_send(:augment_debt) if respond_to?(:augment_debt)
        cost = cost.to_i
        letter_grade = rating.to_s.downcase
        cost_per_line = DebtCeiling.public_send("#{letter_grade}_current_cost_per_line")
        cost += file_attributes.linecount * cost_per_line
        cost += debt_from_source_code_rules
      end
      self.debt_amount = cost
    end

    def debt_from_source_code_rules
      manual_callout_debt +
      text_match_debt('TODO', DebtCeiling.current_cost_per_todo) +
      DebtCeiling.deprecated_reference_pairs
        .reduce(0) {|accum, (string, value)| accum + text_match_debt(string, value.to_i) }
    end

    def text_match_debt(string, cost)
      source_code.scan(string).count * cost
    end

    def manual_callout_debt
      DebtCeiling.manual_callouts.reduce(0) do |memo, callout|
        memo + source_code.each_line.reduce(0) do |accum, line|
          match_data = line.match(Regexp.new(callout + '.*'))
          string = match_data.to_s.split(callout).last
          amount = string.match(/\d+/).to_s if string
          accum + amount.to_i
        end
      end
    end

    def valid_debt?
      black_empty = DebtCeiling.blacklist.empty?
      white_empty = DebtCeiling.whitelist.empty?
      fail DoNotWhitelistAndBlacklistSimulateneously unless black_empty && white_empty
{'fraction_non_alphanumeric': 0.07827977772408794, 'fraction_numerical': 0.001449625513409036, 'mean_word_length': 3.376321353065539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4789502', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1309, 'n_words': 254}