reek icon indicating copy to clipboard operation
reek copied to clipboard

TooManyMethods false positive with nested classes

Open eloyesp opened this issue 5 years ago • 5 comments

I'm having false positive with TooManyMethods, with a code that looks like this:

class WeeklyReminder
  attr_reader :user, :week, :result_set, :dates

  WeekPerformance = Struct.new :week, :submitted, :missing do
    def multiple_methods_here
    end
  end

  def other_methods_here
  end
end

# => TooManyMethods: WeeklyReminder has at least (multiple_methods + other_methods_here) methods.

Seems that reek does not catch the context switch when defining methods on the Struct.

Is it possible for it to detect it or should I add a magic comment (or may be just define the struct in other way).

eloyesp avatar May 13 '19 15:05 eloyesp

Hi @eloyesp, thanks for reporting this. We do intend to support such code in Reek, so this is a bug.

mvz avatar May 13 '19 15:05 mvz

I were checking a little bit, but I lack experience with the parser library.

It seems that the AST that should be detected is something like:

s(:casgn, nil, :WeekPerformance,
  s(:block,
    s(:send,
      s(:const, nil, :Struct), :new,
      # struct args to ignore
    s(:args),
    s(:begin,
      # everithing inside is another class (so defs should count towards other class)

But I'm not sure how to implement that.

eloyesp avatar May 13 '19 16:05 eloyesp

I'll take a look in the coming week. I think we already handle this construction elsewhere, so it shouldn't be too hard to fix this.

mvz avatar May 13 '19 17:05 mvz

Hi! I tried to reproduce it locally but seems to not raise anything.

Am I missing something? or can we close the issue?

image

JuanVqz avatar Oct 18 '23 20:10 JuanVqz

@JuanVqz check out the config for TooManyMethods. I think you just need to put more dummy methods in your example and then this should indeed be falsely reported (since we never fixed that bug)

troessner avatar Oct 19 '23 20:10 troessner