Class: Reek::Smells::TooManyMethods

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/too_many_methods.rb

Overview

A Large Class is a class or module that has a large number of instance variables, methods or lines of code.

TooManyMethods reports classes having more than a configurable number of methods. The method count includes public, protected and private methods, and excludes methods inherited from superclasses or included modules.

Constant Summary collapse

MAX_ALLOWED_METHODS_KEY =

The name of the config field that sets the maximum number of methods permitted in a class.

'max_methods'
DEFAULT_MAX_METHODS =
25

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smell_category, #smell_type, #smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.contextsObject

:nodoc:


25
26
27
# File 'lib/reek/smells/too_many_methods.rb', line 25

def self.contexts # :nodoc:
  [:class]
end

.default_configObject


29
30
31
32
33
34
# File 'lib/reek/smells/too_many_methods.rb', line 29

def self.default_config
  super.merge(
    MAX_ALLOWED_METHODS_KEY => DEFAULT_MAX_METHODS,
    EXCLUDE_KEY => []
  )
end

.smell_categoryObject


21
22
23
# File 'lib/reek/smells/too_many_methods.rb', line 21

def self.smell_category
  'LargeClass'
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

Checks ctx for too many methods

Returns:


41
42
43
44
45
46
47
48
49
50
# File 'lib/reek/smells/too_many_methods.rb', line 41

def examine_context(ctx)
  @max_allowed_methods = value(MAX_ALLOWED_METHODS_KEY, ctx, DEFAULT_MAX_METHODS)
  actual = ctx.node_instance_methods.length
  return [] if actual <= @max_allowed_methods
  [SmellWarning.new(self,
                    context: ctx.full_name,
                    lines: [ctx.exp.line],
                    message:  "has at least #{actual} methods",
                    parameters: { count: actual })]
end