0.0
No commit activity in last 3 years
No release in over 3 years
Provides a common interface for using metrics generated by tools such as Flog, Flay, and Reek.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

MetricAdapter

At LiquidPlanner, we wanted to build some tools to help us analyze our code. There are a ton of static analyzers for ruby, each providing different insightful metrics, so there's no need to write a new analyzer. But what do you do when you want to use more than one of them?

Each analyzer takes a different configuration, and provides results in different formats. MetricAdapter solves the latter problem by converting the results of each analyzer into a common format.

Now you can take all of these wonderful libraries and combine their results without worrying about how they internally represent their metrics.

Metrics

Each metric represents a single statement about one line of code. Flay for instance, generates reports that looks like this:

1) IDENTICAL code found in :lasgn (mass*2 = 24)
  lib/adapters/flay_adapter.rb:26
  lib/adapters/flog_adapter.rb:23

In this case, two metrics would be generated, one per line. This makes it easier to annotate source files, or integrate these libraries with your favorite editor (sed, I kid, I know we all use notepad these days).

Each metric contains:

  • Metric#location – An object representing a file path and line number
  • Metric#signature – The associated method signature (not all metrics currently have this)
  • Metric#message – A message explaining the metric, for instance "Has no descriptive comment"
  • Metric#score – When applicable, a numeric score or rating indicating the severity

Examples

To get normalized metrics, instantiate the static analysis library of your choice, and pass it to the appropriate adapter:

# Instantiate and configure your analyzer
flay = Flay.new :mass => 4 
flay.process(*files)
flay.analyze

# Report on the adapted metrics
adapter = MetricAdapter::FlayAdapter.new(flay)
adapter.metrics.each do |m|
  puts "#{m.location} - #{m.message}"
end

For a full example, see examples/report.rb and examples/annotate.rb. You can use these examples to build tools that are appropriate for your team.

Supported Libraries

  • Flay – Code duplication
  • Flog – Code complexity
  • Reek – Code smells

Contributing

Please do! Take a look at the existing adapters and tests (lib/adapters and test/*_adapter_test.rb) for examples.


Adam Sanderson for LiquidPlanner