class RubyCritic::Analyser::FlaySmells

Public Class Methods

new(analysed_modules) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 11
def initialize(analysed_modules)
  @analysed_modules = paths_to_analysed_modules(analysed_modules)
  @flay = Flay.new(@analysed_modules.keys)
end

Public Instance Methods

run() click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 16
def run
  @flay.hashes.each do |structural_hash, nodes|
    analyze_modules(structural_hash, nodes)
    print green '.'
  end
  puts ''
end
to_s() click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 24
def to_s
  'flay smells'
end

Private Instance Methods

analyze_modules(structural_hash, nodes) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 69
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
cost(mass) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 65
def cost(mass)
  mass / 25
end
create_smell(structural_hash, nodes) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 38
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
paths_to_analysed_modules(analysed_modules) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 30
def paths_to_analysed_modules(analysed_modules)
  paths = {}
  analysed_modules.each do |analysed_module|
    paths[analysed_module.path] = analysed_module
  end
  paths
end
similarity(structural_hash) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 57
def similarity(structural_hash)
  if @flay.identical[structural_hash]
    'Identical code'
  else
    'Similar code'
  end
end
smell_locations(nodes) click to toggle source
# File lib/rubycritic/analysers/smells/flay.rb, line 51
def smell_locations(nodes)
  nodes.map do |node|
    Location.new(node.file, node.line)
  end.sort
end