Sha256: 5478262966ea7e095672c85295faf915f043938573f9bbb400f15caf7d4ea04b

Contents?: true

Size: 1.87 KB

Versions: 25

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'rubycritic/analysers/helpers/flay'
require 'rubycritic/core/smell'
require 'rubycritic/colorize'

module RubyCritic
  module Analyser
    class FlaySmells
      include Colorize
      def initialize(analysed_modules)
        @analysed_modules = paths_to_analysed_modules(analysed_modules)
        @flay = Flay.new(@analysed_modules.keys)
      end

      def run
        @flay.hashes.each do |structural_hash, nodes|
          analyze_modules(structural_hash, nodes)
          print green '.'
        end
        puts ''
      end

      def to_s
        'flay smells'
      end

      private

      def paths_to_analysed_modules(analysed_modules)
        paths = {}
        analysed_modules.each do |analysed_module|
          paths[analysed_module.path] = analysed_module
        end
        paths
      end

      def create_smell(structural_hash, nodes)
        mass = @flay.masses[structural_hash]
        Smell.new(
          locations: smell_locations(nodes),
          context: similarity(structural_hash),
          message: "found in #{nodes.size} nodes",
          score: mass,
          type: 'DuplicateCode',
          analyser: 'flay',
          cost: cost(mass)
        )
      end

      def smell_locations(nodes)
        nodes.map do |node|
          Location.new(node.file, node.line)
        end.sort
      end

      def similarity(structural_hash)
        if @flay.identical[structural_hash]
          'Identical code'
        else
          'Similar code'
        end
      end

      def cost(mass)
        mass / 25
      end

      def analyze_modules(structural_hash, nodes)
        nodes.map(&:file).uniq.each do |file|
          @analysed_modules[file].smells << create_smell(structural_hash, nodes)
        end
        nodes.each do |node|
          @analysed_modules[node.file].duplication += node.mass
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
rubycritic-4.9.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.9.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.8.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.8.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.7.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.6.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.6.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.5.2 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.5.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.5.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.4.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.4.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.3.3 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.3.2 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.3.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.3.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.2.2 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.2.1 lib/rubycritic/analysers/smells/flay.rb
rubycritic-4.2.0 lib/rubycritic/analysers/smells/flay.rb
rubycritic-simplecov-4.1.1 lib/rubycritic/analysers/smells/flay.rb