Class: Reek::SmellDetectors::ClassVariable

Inherits:
BaseDetector show all
Defined in:
lib/reek/smell_detectors/class_variable.rb

Overview

Class variables form part of the global runtime state, and as such make it easy for one part of the system to accidentally or inadvertently depend on another part of the system. So the system becomes more prone to problems where changing something over here breaks something over there. In particular, class variables can make it hard to set up tests (because the context of the test includes all global state).

See Class-Variable for details.

Constant Summary

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, default_config, 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

:nodoc:



18
19
20
# File 'lib/reek/smell_detectors/class_variable.rb', line 18

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

Instance Method Details

#class_variables_in_contextObject

Collects the names of the class variables declared and/or used in the given module.



41
42
43
# File 'lib/reek/smell_detectors/class_variable.rb', line 41

def class_variables_in_context
  context.local_nodes([:cvar, :cvasgn, :cvdecl]).group_by(&:name)
end

#sniffArray<SmellWarning>

Checks whether the given class or module declares any class variables.

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/reek/smell_detectors/class_variable.rb', line 27

def sniff
  class_variables_in_context.map do |variable, occurrences|
    lines = occurrences.map(&:line)
    smell_warning(
      lines: lines,
      message: "declares the class variable '#{variable}'",
      parameters: { name: variable.to_s })
  end
end