Class: Flog
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- Flog
- Defined in:
- lib/flog.rb
Constant Summary collapse
- VERSION =
:nodoc:
"4.1.2"- THRESHOLD =
Cut off point where the report should stop unless –all given.
0.60
- SCORES =
The scoring system hash. Maps node type to score.
Hash.new 1
- BRANCHING =
Names of nodes that branch.
[ :and, :case, :else, :if, :or, :rescue, :until, :when, :while ]
- OTHER_SCORES =
Various non-call constructs
{ :alias => 2, :assignment => 1, :block => 1, :block_pass => 1, :branch => 1, :lit_fixnum => 0.25, :sclass => 5, :super => 1, :to_proc_icky! => 10, :to_proc_lasgn => 15, :to_proc_normal => case RUBY_VERSION when /^1\.8\.7/ then 2 when /^1\.9/ then 1.5 when /^2\.[01]/ then 1 else 5 end, :yield => 1, }
- @@no_class =
:main- @@no_method =
:none
Instance Attribute Summary collapse
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
-
#class_stack ⇒ Object
readonly
Returns the value of attribute class_stack.
-
#mass ⇒ Object
readonly
Returns the value of attribute mass.
-
#method_locations ⇒ Object
readonly
Returns the value of attribute method_locations.
-
#method_scores ⇒ Object
readonly
Returns the value of attribute method_scores.
-
#method_stack ⇒ Object
readonly
Returns the value of attribute method_stack.
-
#multiplier ⇒ Object
:stopdoc:.
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#sclass ⇒ Object
readonly
Returns the value of attribute sclass.
-
#scores ⇒ Object
readonly
Returns the value of attribute scores.
-
#total_score ⇒ Object
readonly
Returns the value of attribute total_score.
-
#totals ⇒ Object
readonly
Returns the value of attribute totals.
Instance Method Summary collapse
-
#add_to_score(name, score = ) ⇒ Object
Add a score to the tally.
-
#alias_method ⇒ Object
Various “magic” usually used for “clever code”.
-
#average ⇒ Object
really?.
-
#calculate ⇒ Object
Calculates classes and methods scores.
-
#calculate_total_scores ⇒ Object
Calculates the total score and populates @totals.
-
#define_method ⇒ Object
Eval forms.
-
#dsl_name?(args) ⇒ Boolean
Returns true if the form looks like a “DSL” construct.
-
#each_by_score(max = nil) ⇒ Object
Iterate over the calls sorted (descending) by score.
-
#flog(*files) ⇒ Object
Flog the given files.
-
#flog_ruby(ruby, file = "-", timeout = 10) ⇒ Object
Flog the given ruby source, optionally using file to provide paths for methods.
-
#flog_ruby!(ruby, file = "-", timeout = 10) ⇒ Object
Flog the given ruby source, optionally using file to provide paths for methods.
-
#in_klass(name) ⇒ Object
Adds name to the class stack, for the duration of the block.
-
#in_method(name, file, line) ⇒ Object
Adds name to the method stack, for the duration of the block.
-
#initialize(option = {}) ⇒ Flog
constructor
Creates a new Flog instance with
options. -
#inject ⇒ Object
Calls that are ALMOST ALWAYS ABUSED!.
-
#klass_name ⇒ Object
Returns the first class in the list, or @@no_class if there are none.
-
#max_method ⇒ Object
Returns the method/score pair of the maximum score.
-
#max_score ⇒ Object
Returns the maximum score for a single method.
-
#method_name ⇒ Object
Returns the first method in the list, or “#none” if there are none.
-
#no_method ⇒ Object
:nodoc:.
-
#penalize_by(bonus) ⇒ Object
For the duration of the block the complexity factor is increased by #bonus This allows the complexity of sub-expressions to be influenced by the expressions in which they are found.
-
#process_alias(exp) ⇒ Object
:stopdoc:.
- #process_and(exp) ⇒ Object (also: #process_or)
- #process_attrasgn(exp) ⇒ Object
- #process_block(exp) ⇒ Object
- #process_block_pass(exp) ⇒ Object
- #process_call(exp) ⇒ Object
- #process_case(exp) ⇒ Object
- #process_class(exp) ⇒ Object
-
#process_dasgn_curr(exp) ⇒ Object
(also: #process_iasgn, #process_lasgn)
FIX: remove.
- #process_defn(exp) ⇒ Object
- #process_defs(exp) ⇒ Object
-
#process_else(exp) ⇒ Object
(also: #process_rescue, #process_when)
TODO: it’s not clear to me whether this can be generated at all.
- #process_if(exp) ⇒ Object
- #process_iter(exp) ⇒ Object
- #process_lit(exp) ⇒ Object
- #process_masgn(exp) ⇒ Object
- #process_module(exp) ⇒ Object
- #process_sclass(exp) ⇒ Object
- #process_super(exp) ⇒ Object
-
#process_until_empty(exp) ⇒ Object
Process each element of #exp in turn.
- #process_while(exp) ⇒ Object (also: #process_until)
- #process_yield(exp) ⇒ Object
-
#reset ⇒ Object
Reset score data.
-
#score_method(tally) ⇒ Object
Compute the distance formula for a given tally.
-
#signature ⇒ Object
Returns the method signature for the current method.
-
#threshold ⇒ Object
Final threshold that is used for report.
Constructor Details
#initialize(option = {}) ⇒ Flog
Creates a new Flog instance with options.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/flog.rb', line 267 def initialize option = {} super() @option = option @sclass = [] @class_stack = [] @method_stack = [] @method_locations = {} @mass = {} @parser = nil self.auto_shift_type = true self.reset end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
101 102 103 |
# File 'lib/flog.rb', line 101 def calls @calls end |
#class_stack ⇒ Object (readonly)
Returns the value of attribute class_stack.
101 102 103 |
# File 'lib/flog.rb', line 101 def class_stack @class_stack end |
#mass ⇒ Object (readonly)
Returns the value of attribute mass.
101 102 103 |
# File 'lib/flog.rb', line 101 def mass @mass end |
#method_locations ⇒ Object (readonly)
Returns the value of attribute method_locations.
102 103 104 |
# File 'lib/flog.rb', line 102 def method_locations @method_locations end |
#method_scores ⇒ Object (readonly)
Returns the value of attribute method_scores.
102 103 104 |
# File 'lib/flog.rb', line 102 def method_scores @method_scores end |
#method_stack ⇒ Object (readonly)
Returns the value of attribute method_stack.
101 102 103 |
# File 'lib/flog.rb', line 101 def method_stack @method_stack end |
#multiplier ⇒ Object
:stopdoc:
100 101 102 |
# File 'lib/flog.rb', line 100 def multiplier @multiplier end |
#option ⇒ Object (readonly)
Returns the value of attribute option.
101 102 103 |
# File 'lib/flog.rb', line 101 def option @option end |
#sclass ⇒ Object (readonly)
Returns the value of attribute sclass.
101 102 103 |
# File 'lib/flog.rb', line 101 def sclass @sclass end |
#scores ⇒ Object (readonly)
Returns the value of attribute scores.
102 103 104 |
# File 'lib/flog.rb', line 102 def scores @scores end |
#total_score ⇒ Object (readonly)
Returns the value of attribute total_score.
103 104 105 |
# File 'lib/flog.rb', line 103 def total_score @total_score end |
#totals ⇒ Object (readonly)
Returns the value of attribute totals.
103 104 105 |
# File 'lib/flog.rb', line 103 def totals @totals end |
Instance Method Details
#add_to_score(name, score = ) ⇒ Object
Add a score to the tally. Score can be predetermined or looked up automatically. Uses multiplier for additional spankings. Spankings!
112 113 114 115 |
# File 'lib/flog.rb', line 112 def add_to_score name, score = OTHER_SCORES[name] return if option[:methods] and method_stack.empty? @calls[signature][name] += score * @multiplier end |
#alias_method ⇒ Object
Various “magic” usually used for “clever code”
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/flog.rb', line 70 SCORES.merge!(:alias_method => 2, :extend => 2, :include => 2, :instance_method => 2, :instance_methods => 2, :method_added => 2, :method_defined? => 2, :method_removed => 2, :method_undefined => 2, :private_class_method => 2, :private_instance_methods => 2, :private_method_defined? => 2, :protected_instance_methods => 2, :protected_method_defined? => 2, :public_class_method => 2, :public_instance_methods => 2, :public_method_defined? => 2, :remove_method => 2, :send => 3, :undef_method => 2) |
#average ⇒ Object
really?
120 121 122 123 |
# File 'lib/flog.rb', line 120 def average return 0 if calls.size == 0 total_score / calls.size end |
#calculate ⇒ Object
Calculates classes and methods scores.
128 129 130 131 132 133 134 135 |
# File 'lib/flog.rb', line 128 def calculate each_by_score threshold do |class_method, score, call_list| klass = class_method.split(/#|::/).first method_scores[klass] << [class_method, score] scores[klass] += score end end |
#calculate_total_scores ⇒ Object
Calculates the total score and populates @totals.
383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/flog.rb', line 383 def calculate_total_scores return if @totals @total_score = 0 @totals = Hash.new(0) calls.each do |meth, tally| score = score_method(tally) @totals[meth] = score @total_score += score end end |
#define_method ⇒ Object
Eval forms
61 62 63 64 65 |
# File 'lib/flog.rb', line 61 SCORES.merge!(:define_method => 5, :eval => 5, :module_eval => 5, :class_eval => 5, :instance_eval => 5) |
#dsl_name?(args) ⇒ Boolean
Returns true if the form looks like a “DSL” construct.
task :blah do ... end
=> s(:iter, s(:call, nil, :task, s(:lit, :blah)), ...)
143 144 145 146 147 148 149 150 |
# File 'lib/flog.rb', line 143 def dsl_name? args return false unless args and not args.empty? first_arg = args.first first_arg = first_arg[1] if first_arg[0] == :hash [:lit, :str].include? first_arg[0] and first_arg[1] end |
#each_by_score(max = nil) ⇒ Object
Iterate over the calls sorted (descending) by score.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/flog.rb', line 155 def each_by_score max = nil current = 0 calls.sort_by { |k,v| -totals[k] }.each do |class_method, call_list| score = totals[class_method] yield class_method, score, call_list current += score break if max and current >= max end end |
#flog(*files) ⇒ Object
Flog the given files. Deals with “-”, and syntax errors.
Not as smart as FlogCLI’s #flog method as it doesn’t traverse dirs. Use FlogCLI.expand_dirs_to_files or see FlogCLI#flog.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/flog.rb', line 174 def flog(*files) files.each do |file| next unless file == '-' or File.readable? file ruby = file == '-' ? $stdin.read : File.binread(file) flog_ruby ruby, file end calculate_total_scores end |
#flog_ruby(ruby, file = "-", timeout = 10) ⇒ Object
Flog the given ruby source, optionally using file to provide paths for methods. Smart. Handles syntax errors and timeouts so you don’t have to.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/flog.rb', line 191 def flog_ruby ruby, file="-", timeout = 10 flog_ruby! ruby, file, timeout rescue Timeout::Error warn "TIMEOUT parsing #{file}. Skipping." rescue RubyParser::SyntaxError, Racc::ParseError => e q = option[:quiet] if e.inspect =~ /<\%|%\>/ or ruby =~ /<\%|%\>/ then return if q warn "#{e.inspect} at #{e.backtrace.first(5).join(', ')}" warn "\n...stupid lemmings and their bad erb templates... skipping" else warn "ERROR: parsing ruby file #{file}" unless q unless option[:continue] then warn "ERROR! Aborting. You may want to run with --continue." raise e end return if q warn "%s: %s at:\n %s" % [e.class, e..strip, e.backtrace.first(5).join("\n ")] end end |
#flog_ruby!(ruby, file = "-", timeout = 10) ⇒ Object
Flog the given ruby source, optionally using file to provide paths for methods. Does not handle timeouts or syntax errors. See #flog_ruby.
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/flog.rb', line 217 def flog_ruby! ruby, file="-", timeout = 10 @parser = (option[:parser] || RubyParser).new warn "** flogging #{file}" if option[:verbose] ast = @parser.process ruby, file, timeout return unless ast mass[file] = ast.mass process ast end |
#in_klass(name) ⇒ Object
Adds name to the class stack, for the duration of the block
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/flog.rb', line 233 def in_klass name if Sexp === name then name = case name.first when :colon2 then name = name.flatten name.delete :const name.delete :colon2 name.join("::") when :colon3 then name.last.to_s else raise "unknown type #{name.inspect}" end end @class_stack.unshift name yield @class_stack.shift end |
#in_method(name, file, line) ⇒ Object
Adds name to the method stack, for the duration of the block
256 257 258 259 260 261 262 |
# File 'lib/flog.rb', line 256 def in_method(name, file, line) method_name = Regexp === name ? name.inspect : name.to_s @method_stack.unshift method_name @method_locations[signature] = "#{file}:#{line}" yield @method_stack.shift end |
#inject ⇒ Object
Calls that are ALMOST ALWAYS ABUSED!
94 |
# File 'lib/flog.rb', line 94 SCORES.merge!(:inject => 2) |
#klass_name ⇒ Object
Returns the first class in the list, or @@no_class if there are none.
284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/flog.rb', line 284 def klass_name name = @class_stack.first if Sexp === name then raise "you shouldn't see me" elsif @class_stack.any? @class_stack.reverse.join("::").sub(/\([^\)]+\)$/, '') else @@no_class end end |
#max_method ⇒ Object
Returns the method/score pair of the maximum score.
299 300 301 |
# File 'lib/flog.rb', line 299 def max_method totals.max_by { |_, score| score } end |
#max_score ⇒ Object
Returns the maximum score for a single method. Used for FlogTask.
306 307 308 |
# File 'lib/flog.rb', line 306 def max_score max_method.last end |
#method_name ⇒ Object
Returns the first method in the list, or “#none” if there are none.
314 315 316 317 318 |
# File 'lib/flog.rb', line 314 def method_name m = @method_stack.first || @@no_method m = "##{m}" unless m =~ /::/ m end |
#no_method ⇒ Object
:nodoc:
397 398 399 |
# File 'lib/flog.rb', line 397 def no_method # :nodoc: @@no_method end |
#penalize_by(bonus) ⇒ Object
For the duration of the block the complexity factor is increased by #bonus This allows the complexity of sub-expressions to be influenced by the expressions in which they are found. Yields 42 to the supplied block.
326 327 328 329 330 |
# File 'lib/flog.rb', line 326 def penalize_by bonus @multiplier += bonus yield @multiplier -= bonus end |
#process_alias(exp) ⇒ Object
:stopdoc:
405 406 407 408 409 410 |
# File 'lib/flog.rb', line 405 def process_alias(exp) process exp.shift process exp.shift add_to_score :alias s() end |
#process_and(exp) ⇒ Object Also known as: process_or
412 413 414 415 416 417 418 419 |
# File 'lib/flog.rb', line 412 def process_and(exp) add_to_score :branch penalize_by 0.1 do process exp.shift # lhs process exp.shift # rhs end s() end |
#process_attrasgn(exp) ⇒ Object
422 423 424 425 426 427 428 |
# File 'lib/flog.rb', line 422 def process_attrasgn(exp) add_to_score :assignment process exp.shift # lhs exp.shift # name process_until_empty exp # rhs s() end |
#process_block(exp) ⇒ Object
430 431 432 433 434 435 |
# File 'lib/flog.rb', line 430 def process_block(exp) penalize_by 0.1 do process_until_empty exp end s() end |
#process_block_pass(exp) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/flog.rb', line 437 def process_block_pass(exp) arg = exp.shift add_to_score :block_pass case arg.first when :lvar, :dvar, :ivar, :cvar, :self, :const, :colon2, :nil then # f(&b) # do nothing when :lit, :call then # f(&:b) add_to_score :to_proc_normal when :lasgn then # f(&l=b) add_to_score :to_proc_lasgn when :iter, :dsym, :dstr, *BRANCHING then # below # f(&proc { ... }) # f(&"#{...}") # f(&:"#{...}") # f(&if ... then ... end") and all other branching forms add_to_score :to_proc_icky! else raise({:block_pass_even_ickier! => arg}.inspect) end process arg s() end |
#process_call(exp) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/flog.rb', line 464 def process_call(exp) penalize_by 0.2 do process exp.shift # recv end name = exp.shift penalize_by 0.2 do process_until_empty exp end add_to_score name, SCORES[name] s() end |
#process_case(exp) ⇒ Object
480 481 482 483 484 485 486 487 |
# File 'lib/flog.rb', line 480 def process_case(exp) add_to_score :branch process exp.shift # recv penalize_by 0.1 do process_until_empty exp end s() end |
#process_class(exp) ⇒ Object
489 490 491 492 493 494 495 496 497 |
# File 'lib/flog.rb', line 489 def process_class(exp) in_klass exp.shift do penalize_by 1.0 do process exp.shift # superclass expression end process_until_empty exp end s() end |
#process_dasgn_curr(exp) ⇒ Object Also known as: process_iasgn, process_lasgn
FIX: remove
499 500 501 502 503 504 |
# File 'lib/flog.rb', line 499 def process_dasgn_curr(exp) # FIX: remove add_to_score :assignment exp.shift # name process exp.shift # assigment, if any s() end |
#process_defn(exp) ⇒ Object
508 509 510 511 512 513 514 |
# File 'lib/flog.rb', line 508 def process_defn(exp) name = @sclass.empty? ? exp.shift : "::#{exp.shift}" in_method name, exp.file, exp.line do process_until_empty exp end s() end |
#process_defs(exp) ⇒ Object
516 517 518 519 520 521 522 |
# File 'lib/flog.rb', line 516 def process_defs(exp) process exp.shift # recv in_method "::#{exp.shift}", exp.file, exp.line do process_until_empty exp end s() end |
#process_else(exp) ⇒ Object Also known as: process_rescue, process_when
TODO: it’s not clear to me whether this can be generated at all.
525 526 527 528 529 530 531 |
# File 'lib/flog.rb', line 525 def process_else(exp) add_to_score :branch penalize_by 0.1 do process_until_empty exp end s() end |
#process_if(exp) ⇒ Object
535 536 537 538 539 540 541 542 543 |
# File 'lib/flog.rb', line 535 def process_if(exp) add_to_score :branch process exp.shift # cond penalize_by 0.1 do process exp.shift # true process exp.shift # false end s() end |
#process_iter(exp) ⇒ Object
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/flog.rb', line 545 def process_iter(exp) context = (self.context - [:class, :module, :scope]) context = context.uniq.sort_by { |s| s.to_s } exp.delete 0 # { || ... } has 0 in arg slot if context == [:block, :iter] or context == [:iter] then recv = exp.first # DSL w/ names. eg task :name do ... end # looks like s(:call, nil, :task, s(:lit, :name)) # or s(:call, nil, :task, s(:str, "name")) # or s(:call, nil, :task, s(:hash, s(:lit, :name) ...)) t, r, m, *a = recv if t == :call and r == nil and submsg = dsl_name?(a) then m = "#{m}(#{submsg})" if m and [String, Symbol].include?(submsg.class) in_klass m do # :task/namespace in_method submsg, exp.file, exp.line do # :name process_until_empty exp end end return s() end end add_to_score :branch process exp.shift # no penalty for LHS penalize_by 0.1 do process_until_empty exp end s() end |
#process_lit(exp) ⇒ Object
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/flog.rb', line 583 def process_lit(exp) value = exp.shift case value when 0, -1 then # ignore those because they're used as array indicies instead of # first/last when Integer then add_to_score :lit_fixnum when Float, Symbol, Regexp, Range then # do nothing else raise value.inspect end s() end |
#process_masgn(exp) ⇒ Object
599 600 601 602 603 604 605 606 |
# File 'lib/flog.rb', line 599 def process_masgn(exp) add_to_score :assignment exp.map! { |s| Sexp === s ? s : s(:lasgn, s) } process_until_empty exp s() end |
#process_module(exp) ⇒ Object
608 609 610 611 612 613 |
# File 'lib/flog.rb', line 608 def process_module(exp) in_klass exp.shift do process_until_empty exp end s() end |
#process_sclass(exp) ⇒ Object
615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/flog.rb', line 615 def process_sclass(exp) @sclass.push(true) penalize_by 0.5 do process exp.shift # recv process_until_empty exp end @sclass.pop add_to_score :sclass s() end |
#process_super(exp) ⇒ Object
627 628 629 630 631 |
# File 'lib/flog.rb', line 627 def process_super(exp) add_to_score :super process_until_empty exp s() end |
#process_until_empty(exp) ⇒ Object
Process each element of #exp in turn.
335 336 337 |
# File 'lib/flog.rb', line 335 def process_until_empty exp process exp.shift until exp.empty? end |
#process_while(exp) ⇒ Object Also known as: process_until
633 634 635 636 637 638 639 640 641 |
# File 'lib/flog.rb', line 633 def process_while(exp) add_to_score :branch penalize_by 0.1 do process exp.shift # cond process exp.shift # body end exp.shift # pre/post s() end |
#process_yield(exp) ⇒ Object
644 645 646 647 648 |
# File 'lib/flog.rb', line 644 def process_yield(exp) add_to_score :yield process_until_empty exp s() end |
#reset ⇒ Object
Reset score data
342 343 344 345 346 347 348 349 |
# File 'lib/flog.rb', line 342 def reset @totals = @total_score = nil @multiplier = 1.0 @calls = Hash.new { |h,k| h[k] = Hash.new 0 } @method_scores = Hash.new { |h,k| h[k] = [] } @scores = Hash.new 0 @method_locations = {} end |
#score_method(tally) ⇒ Object
Compute the distance formula for a given tally
354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/flog.rb', line 354 def score_method(tally) a, b, c = 0, 0, 0 tally.each do |cat, score| case cat when :assignment then a += score when :branch then b += score else c += score end end Math.sqrt(a*a + b*b + c*c) end |
#signature ⇒ Object
Returns the method signature for the current method.
369 370 371 |
# File 'lib/flog.rb', line 369 def signature "#{klass_name}#{method_name}" end |
#threshold ⇒ Object
Final threshold that is used for report
376 377 378 |
# File 'lib/flog.rb', line 376 def threshold option[:all] ? nil : total_score * THRESHOLD end |