Class: Reek::SmellDetectors::TooManyConstants

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/too_many_constants.rb

Overview

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

+TooManyConstants’ reports classes having more than a configurable number of constants.

See Too-Many-Constants for details.

Constant Summary collapse

MAX_ALLOWED_CONSTANTS_KEY =

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

'max_constants'
DEFAULT_MAX_CONSTANTS =
5
IGNORED_NODES =
[:module, :class].freeze

Constants inherited from BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, smell_type, #smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Class Method Details

.contextsObject



22
23
24
# File 'lib/reek/smell_detectors/too_many_constants.rb', line 22

def self.contexts
  [:class, :module]
end

.default_configObject



26
27
28
29
30
# File 'lib/reek/smell_detectors/too_many_constants.rb', line 26

def self.default_config
  super.merge(
    MAX_ALLOWED_CONSTANTS_KEY => DEFAULT_MAX_CONSTANTS,
    EXCLUDE_KEY => [])
end

Instance Method Details

#build_smell_warning(count) ⇒ Object (private)



51
52
53
54
55
56
# File 'lib/reek/smell_detectors/too_many_constants.rb', line 51

def build_smell_warning(count)
  [smell_warning(
    lines: [source_line],
    message: "has #{count} constants",
    parameters: { count: count })]
end

#max_allowed_constantsObject (private)



47
48
49
# File 'lib/reek/smell_detectors/too_many_constants.rb', line 47

def max_allowed_constants
  value(MAX_ALLOWED_CONSTANTS_KEY, context)
end

#sniffArray<SmellWarning>

Checks klass for too many constants.

Returns:



37
38
39
40
41
42
43
# File 'lib/reek/smell_detectors/too_many_constants.rb', line 37

def sniff
  count = context.local_nodes(:casgn).count { |it| !it.defines_module? }

  return [] if count <= max_allowed_constants

  build_smell_warning(count)
end