Add a data structure that contains the data generated by flay.report
bf4 opened this issue · 7 comments
Given
- the code in igata
- running
flay app lib
yields the below report
Total score (lower is better) = 494
1) IDENTICAL code found in :defn (mass*2 = 196)
app/models/template_demo.rb:61
app/models/template_purchase.rb:58
2) IDENTICAL code found in :defn (mass*2 = 144)
app/models/template_demo.rb:16
app/models/template_purchase.rb:25
3) Similar code found in :resbody (mass = 58)
app/models/template.rb:80
app/models/template.rb:125
4) Similar code found in :defn (mass = 50)
app/controllers/my_templates_git_controller.rb:4
app/controllers/my_templates_git_controller.rb:10
5) Similar code found in :call (mass = 46)
app/inputs/checkbox_list_input.rb:25
app/inputs/screenshots_input.rb:22
metric_fu, for example, parses this text report to yield (admittedly, not the most rigorous analysis)
{:total_score=>"494",
:matches=>
[{:reason=>"1) IDENTICAL code found in :defn (mass*2 = 196)",
:matches=>
[{:name=>"app/models/template_demo.rb", :line=>"61"},
{:name=>"app/models/template_purchase.rb", :line=>"58"}]},
{:reason=>"2) IDENTICAL code found in :defn (mass*2 = 144)",
:matches=>
[{:name=>"app/models/template_demo.rb", :line=>"16"},
{:name=>"app/models/template_purchase.rb", :line=>"25"}]},
{:reason=>"3) Similar code found in :resbody (mass = 58)",
:matches=>
[{:name=>"app/models/template.rb", :line=>"80"},
{:name=>"app/models/template.rb", :line=>"125"}]},
{:reason=>"4) Similar code found in :defn (mass = 50)",
:matches=>
[{:name=>"app/controllers/my_templates_git_controller.rb", :line=>"4"},
{:name=>"app/controllers/my_templates_git_controller.rb", :line=>"10"}]},
{:reason=>"5) Similar code found in :call (mass = 46)",
:matches=>
[{:name=>"app/inputs/checkbox_list_input.rb", :line=>"25"},
{:name=>"app/inputs/screenshots_input.rb", :line=>"22"}]}]}
The problem, is that it is not easily possible to reproduce the results of this report without either reproducing most of the code in 'report' or changing the meaning of puts and warn on the flay instance.
Now, if I were to run ruby -rpp -rflay -e "flay = Flay.new(Flay.default_options); files = Flay.expand_dirs_to_files(%w[app lib]); flay.process(*files); flay.analyze; pp [flay.total, flay.summary, flay.masses, flay.identical]"
I get
[494,
{"app/controllers/my_templates_git_controller.rb"=>50.0,
"app/inputs/checkbox_list_input.rb"=>23.0,
"app/inputs/screenshots_input.rb"=>23.0,
"app/models/template.rb"=>58.0,
"app/models/template_demo.rb"=>170.0,
"app/models/template_purchase.rb"=>170.0},
{-3749969949219652095=>50,
1606873373262129863=>46,
1034993600789789894=>58,
-4553549444874076191=>144,
3143048742042231397=>196},
{-3749969949219652095=>false,
1606873373262129863=>false,
1034993600789789894=>false,
-4553549444874076191=>true,
3143048742042231397=>true}]
Per discussion in
- metricfu/metric_fu#231 (and following the bug in metricfu/metric_fu#229 )
- https://twitter.com/the_zenspider/status/466718576607379457
How's this? https://gist.github.com/5faab066e93cff1af161
flay.analyze
=> [[3800538705636364736, "Similar", :iter, "", 36, [["lib/flay.rb", 65], ["lib/flay.rb", 86]]]]
Looks good! What's the ""
for? would that be e.g. mass*2
OK. I'm not the most patient person in the world and I was having fun digging through this, so I made a bunch more changes and committed. Have at it: 461174f
Meets the need and cleans up the code-- I like. The only thing that concerns me about it is how the analyzed result might change, and if it might be better wrapped in an struct-like-object.
[hash, node.first, bonus, mass, locs]
-> datum.methods #=> hash, identical?, node_name, bonus, mass, locations
which would also document it a bit better, I think.
Meh. I'm not married to the struct idea, but it is easy enough to implement.
Done. Will be released "soon".
Great, thanks!