Class: Reek::Smells::LargeClass

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

Overview

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

Currently LargeClass only reports classes having more than a configurable number of methods or instance variables. 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'
MAX_ALLOWED_IVARS_KEY =

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

'max_instance_variables'

Constants inherited from SmellDetector

SmellDetector::ENABLED_KEY, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, #examine, #exception?, listen, #smell_name

Constructor Details

#initialize(config = LargeClass.default_config) ⇒ LargeClass

Returns a new instance of LargeClass.



39
40
41
42
43
# File 'lib/reek/smells/large_class.rb', line 39

def initialize(config = LargeClass.default_config)
  super
  @max_methods = config[MAX_ALLOWED_METHODS_KEY]
  @max_instance_variables = config[MAX_ALLOWED_IVARS_KEY]
end

Class Method Details

.contextsObject

:nodoc:



27
28
29
# File 'lib/reek/smells/large_class.rb', line 27

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

.default_configObject



31
32
33
34
35
36
37
# File 'lib/reek/smells/large_class.rb', line 31

def self.default_config
  super.adopt(
    MAX_ALLOWED_METHODS_KEY => 25,
    MAX_ALLOWED_IVARS_KEY => 9,
    EXCLUDE_KEY => []
    )
end

Instance Method Details

#check_num_ivars(klass, report) ⇒ Object

:nodoc:



52
53
54
55
56
57
# File 'lib/reek/smells/large_class.rb', line 52

def check_num_ivars(klass, report)  # :nodoc:
  count = klass.variable_names.length
  return if count <= @max_instance_variables
  report << SmellWarning.new(self, klass,
              "has at least #{count} instance variables")
end

#check_num_methods(klass, report) ⇒ Object

:nodoc:



45
46
47
48
49
50
# File 'lib/reek/smells/large_class.rb', line 45

def check_num_methods(klass, report)  # :nodoc:
  count = klass.num_methods
  return if count <= @max_methods
  report << SmellWarning.new(self, klass,
              "has at least #{count} methods")
end

#examine_context(klass, report) ⇒ Object

Checks klass for too many methods or too many instance variables. Any smells found are added to the report.



63
64
65
66
# File 'lib/reek/smells/large_class.rb', line 63

def examine_context(klass, report)
  check_num_methods(klass, report)
  check_num_ivars(klass, report)
end