<commit_before>module Maximus
  # @since 0.1.0
  class Brakeman < Maximus::Lint

    # Brakeman (requires Rails)
    # @see Lint#initialize
    def result

      @task = 'brakeman'
      @path = @settings[:root_dir] if @path.blank?

      return unless is_rails? && temp_config(@task) && path_exists?(@path)

      tmp = Tempfile.new('brakeman')
      quietly { `brakeman #{@path} -f json -o #{tmp.path} -q` }
      brakeman = tmp.read
      tmp.close
      tmp.unlink

      unless brakeman.blank?
        bjson = JSON.parse(brakeman)
        @output[:ignored_warnings] = bjson['scan_info']['ignored_warnings']
        @output[:checks_performed] = bjson['scan_info']['checks_performed']
        @output[:number_of_controllers] = bjson['scan_info']['number_of_controllers']
        @output[:number_of_models] = bjson['scan_info']['number_of_models']
        @output[:number_of_templates] = bjson['scan_info']['number_of_templates']
        @output[:ruby_version] = bjson['scan_info']['ruby_version']
        @output[:rails_version] = bjson['scan_info']['rails_version']
        brakeman = {}
        ['warnings', 'errors'].each do |type|
          new_brakeman = bjson[type].group_by { |s| s['file'] }
          new_brakeman.each do |file, errors|
            if file
              brakeman[file.to_sym] = errors.map { |e| hash_for_brakeman(e, type) }
            end
          end
        end
        # The output of brakeman is a mix of strings and symbols
        #   but resetting the JSON like this standardizes everything.
        # @todo Better way to get around this?
        brakeman = JSON.parse(brakeman.to_json)
      end

      @output[:files_inspected] ||= files_inspected('rb', ' ')
      refine brakeman
    end


    private

      # Convert to {file:README.md Maximus format}
      # @param error [Hash] lint error
      # @return [Hash]
      def hash_for_brakeman(error, type)
        {
          linter: error['warning_type'],
          severity: type.chomp('s'),
          reason: error['message'],
          column: 0,
          line: error['line'].to_i,
          confidence: error['confidence']
        }
      end

  end<commit_msg>remove spaces in brakeman output<commit_after>module Maximus
  # @since 0.1.0
  class Brakeman < Maximus::Lint

    # Brakeman (requires Rails)
    # @see Lint#initialize
    def result

      @task = 'brakeman'
      @path = @settings[:root_dir] if @path.blank?

      return unless is_rails? && temp_config(@task) && path_exists?(@path)

      tmp = Tempfile.new('brakeman')
      quietly { `brakeman #{@path} -f json -o #{tmp.path} -q` }
      brakeman = tmp.read
      tmp.close
      tmp.unlink

      unless brakeman.blank?
        bjson = JSON.parse(brakeman)
        @output[:ignored_warnings] = bjson['scan_info']['ignored_warnings']
        @output[:checks_performed] = bjson['scan_info']['checks_performed']
        @output[:number_of_controllers] = bjson['scan_info']['number_of_controllers']
        @output[:number_of_models] = bjson['scan_info']['number_of_models']
        @output[:number_of_templates] = bjson['scan_info']['number_of_templates']
        @output[:ruby_version] = bjson['scan_info']['ruby_version']
        @output[:rails_version] = bjson['scan_info']['rails_version']
        brakeman = {}
        ['warnings', 'errors'].each do |type|
          new_brakeman = bjson[type].group_by { |s| s['file'] }
          new_brakeman.each do |file, errors|
            if file
              brakeman[file.to_sym] = errors.map { |e| hash_for_brakeman(e, type) }
            end
          end
        end
        # The output of brakeman is a mix of strings and symbols
        #   but resetting the JSON like this standardizes everything.
        # @todo Better way to get around this?
        brakeman = JSON.parse(brakeman.to_json)
      end

      @output[:files_inspected] ||= files_inspected('rb', ' ')
      refine brakeman
    end


    private

      # Convert to {file:README.md Maximus format}
      # @param error [Hash] lint error
      # @return [Hash]
      def hash_for_brakeman(error, type)
        {
          linter: error['warning_type'].delete(' '),
          severity: type.chomp('s'),
          reason: error['message'],
          column: 0,
          line: error['line'].to_i,
          confidence: error['confidence']
        }
      end

  end
{'fraction_non_alphanumeric': 0.1299746951920865, 'fraction_numerical': 0.001840349666436623, 'mean_word_length': 2.5378356387306753, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2242095', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1374, 'n_words': 291}